This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomment_formatter.py
190 lines (159 loc) · 6.16 KB
/
comment_formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import omdb_requester
import post_parser
import commentparser
import netflix_requester
from json import load
from datetime import datetime
import logging
with open("info/subreddits.txt") as file:
subreddit_to_show = load(file) # dictionary [subreddit name] = tv show
bot_disclaimer = "------\n" \
"^^I'm ^^a ^^bot ^^that ^^gives ^^info ^^on ^^shows. " \
"^^| ^^[message](http://www.reddit.com/message/compose?to=the_episode_bot) ^^me " \
"^^if ^^there's ^^an ^^issue. ^^| ^^[about](https://github.com/Almenon/reddit_episode_bot)"
# formatting for bot_disclaimer thanks to wikipediacitationbot
limitedNetflixRelease = {
# no shows have limited release currently. if it did i would have a value like: 'mylittlepony': 7.13,
}
spoiler1 = '[{}](/spoiler)'
spoiler2 = '[](#s "{}")'
spoiler3 = '[{}](#spoiler)'
spoiler4 = '[Spoiler Alert](/s "{}")'
spoiler5 = '[spoiler](#s "{}")'
spoilers = {
"bojackhorseman":spoiler2,
"orangeisthenewblack":spoiler1,
"gravityfalls":spoiler3,
"mylittlepony":spoiler1,
"archerfx":spoiler1,
"stevenuniverse":spoiler2,
"blackmirror":spoiler4,
"episode_bot":spoiler1,
"shield":spoiler1,
"preacher":spoiler2,
"disenchantment":spoiler2,
"madmen":spoiler1
}
hulu_links = {
"archer":"https://www.hulu.com/archer",
"preacher":"https://www.hulu.com/preacher"
}
reply = "#####	 \n######	 \n####	 \n" \
"[**{title}**](http://www.imdb.com/title/{id}) {rating}|" \
" {watch_link}" \
"[imdb](http://www.imdb.com/title/{id})" \
"{plot}"
class NotEnoughInfoError(Exception):
"""
Raised when post does not have essential info (plot summary or watch link)
Raising this exception ensures that only high quality posts survive
"""
pass
def format_post_reply(request, subreddit):
"""
:param request: comment text or post title
:param subreddit: PRAW subreddit object or name of subreddit (without the /r/)
:return: a paragraph of info and links about the episode, formatted for Reddit
:except: omdb_requester.CustomError, post_parser.ParseError, comment_formatter.CustomError
"""
missingInfo = 0
released = True
season,episode = post_parser.parse(request)
subreddit = str(subreddit).lower() # clean input
show = subreddit_to_show[subreddit]
episode_info = omdb_requester.get_info(show, season, episode)
if episode_info['Year'].isdigit() and int(episode_info['Year']) > datetime.now().year\
or subreddit in limitedNetflixRelease and int(season)+int(episode)/100 > limitedNetflixRelease[subreddit]:
released = False
watch_link = ''
if released:
try:
if show in hulu_links:
link = hulu_links[show]
watch_site = 'Hulu'
else:
link = netflix_requester.get_netflix_link(show)
watch_site = 'Netflix'
watch_link = "[Watch on "+watch_site+"](" + link + ") | "
except KeyError as e:
logging.warning(e)
missingInfo += 1
else:
missingInfo += 1
plot = episode_info['Plot']
if plot == "N/A" or plot == '':
plot = ''
missingInfo += 1
else:
if subreddit in spoilers:
plot = spoilers[subreddit].format(plot)
plot = "\n\n> " + plot
if missingInfo == 2:
raise NotEnoughInfoError
if(episode_info['imdbRating'] == 'N/A'):
rating = ''
else:
rating = '[{} ★] '.format(episode_info['imdbRating'])
return reply.format(
title = episode_info['Title'],
id = episode_info['imdbID'],
rating = rating,
watch_link = watch_link,
plot = plot,
)
def format_comment_reply(request, subreddit):
"""
unlike format post reply, this formatter is more lenient if there's missing data
:param request: comment text or post title
:param subreddit: PRAW subreddit object or name of subreddit (without the /r/)
:return: a paragraph of info and links about the episode, formatted for Reddit
:except: omdb_requester.CustomError, commentparser.ParseError
"""
global subreddit_to_show
global bot_disclaimer
released = True
show,season,episode = commentparser.parse(request)
subreddit = str(subreddit).lower() # clean input
episode_info = omdb_requester.get_info(show, season, episode)
if show is None:
try:
show = subreddit_to_show[subreddit]
except IndexError:
raise commentparser.ParseError
if episode_info['Year'].isdigit() and int(episode_info['Year']) > datetime.now().year\
or subreddit in limitedNetflixRelease and int(season)+int(episode)/100 > limitedNetflixRelease[subreddit]:
released = False
watch_link = ''
if released:
try:
if show in hulu_links:
link = hulu_links[show]
watch_site = 'Hulu'
else:
link = netflix_requester.get_netflix_link(show)
watch_site = 'Netflix'
watch_link = "[Watch on "+watch_site+"](" + link + ") | "
except KeyError as e:
logging.warning(e)
plot = episode_info['Plot']
if plot == "N/A":
plot = ''
else:
if subreddit in spoilers:
plot = spoilers[subreddit].format(plot)
if(episode_info['imdbRating'] == 'N/A'):
rating = ''
else:
rating = '[{}] '.format(episode_info['imdbRating'])
return reply.format(
title = episode_info['Title'],
id = episode_info['imdbID'],
rating = rating,
watch_link = watch_link,
plot = plot,
)
# testing:
#print(format_post_reply("Hiatus weekly re-watch thread: Season 4 Episode 12 - Call of the Cutie","orangeisthenewblack"))
#print(format_post_reply("Hiatus weekly re-watch thread: Season 1 Episode 12 - Call of the Cutie","gravityfalls"))
#print(format_post_reply("Hiatus weekly re-watch thread: Season 1 Episode 9 - Call of the Cutie","bojackhorseman"))
# print(format_post_reply("Summoning /u/the_episode_bot arrow s01e03","episode_bot",post=False))