-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutput.py
More file actions
265 lines (216 loc) · 8.67 KB
/
output.py
File metadata and controls
265 lines (216 loc) · 8.67 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import json, requests
from bs4 import BeautifulSoup
import re
import config
from mailUser import Receiver
from findDates import dateConverter,findNextEpisode,checkYear
class UserOutput(object):
def __init__(self,user):
self.address=user.email
self.TVshows=user.series
self.final_di={} #final output dictionary for a user
self.final_string="" #final output string for a user
def FindFinalOutput(self):
allseries=self.TVshows
if allseries[-1]==",":
allseries=allseries[:-1]
allShows=allseries.split(",")
for tvseries in allShows:
tvshow=TVSHOW(tvseries)
tvshow.StripTVShow()
tvshow.ApiDetails()
if tvshow.flag2:
tvshow.final=tvshow.ostring+" is not a tv show."
else:
tvshow.UseDetails()
if tvshow.flag2:
tvshow.final=tvshow.ostring+":There is no such tv show.Please write the tv show name properly."
self.final_di[tvshow.string]=tvshow.final
self.findFinalString()
#Mail to User
R=Receiver(self.final_string,self.address)
R.mailIt()
#merging all outputs of all tv series into one string for one user
def findFinalString(self):
final_li=[]
final_li.append("\n")
for v in self.final_di:
b=list(str(v).split(" "))
show=""
for s in b:
show+=s.capitalize()
show+=" "
s1="TV Series Name-"+show
s2="Status-"+str(self.final_di[v])
final_li.append(s1)
final_li.append("\n")
final_li.append(s2)
final_li.append('\n\n')
final_li="".join(final_li)
print(final_li)
self.final_string=final_li
#########################################################################################################
class TVSHOW():
def __init__(self,value):
self.tvseries=value #name of tv show
self.ostring="" #original tv show string
self.string="" #final tv show string
self.ID="" #tv show id
self.Year="" #year
self.TS=0 #total seasons
self.flag=False
self.flag1=False
self.flag2=False
self.final="" #final output for a particular tv show
def StripTVShow(self):
#stripped original string
for ch in self.tvseries:
ch=ch.lower()
if (ord(ch)>96 and ord(ch)<123) or(len(self.ostring)>=0):
self.ostring+=ch
#all possible strings of series
url="https://www.imdb.com/find?ref_=nv_sr_fn&q="+self.ostring+"&s=all"
r=requests.get(url)
soup=BeautifulSoup(r.text,"html.parser")
try:
s1=soup.find('table',{'class':"findList"})
a=s1.find('td',{'class':'result_text'}).find('a')
for t in a:
self.string=t
except:
print("\nPlease write only a tv show name.")
#print(self.string,self.ostring) #before finalising tv series
def findID(self,c):
url="http://www.omdbapi.com/?apikey=d77c7587&type=series&t="+self.ostring
r1 = requests.get(url)
di = json.loads(r1.text) #get json
try:
Id=di["imdbID"] #extract tv show id
Y=di["Year"] #extract year
tS=di["totalSeasons"]
self.ID=Id
self.Year=Y
self.string=self.ostring
self.TS=tS
if len(self.Year)==4: #it could or could not be a tv show
if int(tS)>=1:#it is a tv show
self.flag=True #the show started and ended in the same year
c=0
else:#not a tv show
c+=1
else: # it is definitely a tv show
c=0
except:
c+=1
return c
def ApiDetails(self):
url1 = "http://www.omdbapi.com/?apikey=d77c7587&type=series&t="+self.string
c=0
r1 = requests.get(url1)
di = json.loads(r1.text) #get json
try:
self.ID=di["imdbID"] #extract tv show id
self.Year=di["Year"] #extract year
tS=di["totalSeasons"] #total Seasons
self.TS=tS
if len(self.Year)==4: #it could or could not be a tv show
if int(tS)>=1:#it is a tv show
self.flag=True #the show started and ended in the same year
c=0
else:#not a tv show
c+=1
else: # it is definitely a tv show
c=0
except:
c+=1
if c==1:
c=self.findID(c)
if c>=2:#it is definitely not a tv show
self.flag2=True
#print(self.string,self.ostring,c) #after finalising the tv series
def UseDetails(self):
################### CASE 1 #####################
if len(self.Year)==9 and checkYear(self.Year):
print("\n"+self.ostring +" has seasons:"+str(self.TS))
self.final="The show has finished streaming all its episodes."
elif self.flag:
print("\n"+self.ostring +" has seasons:"+str(self.TS))
self.final="The show has finished streaming all its episodes."
################# Other CASES ########################
else:
url="https://www.imdb.com/title/"+self.ID+"/?ref_=ttep_ep_tt"
r=requests.get(url)
soup=BeautifulSoup(r.text,"html.parser")
#extracting season
try:
s1=soup.find('div',{'class':"seasons-and-year-nav"})
a=s1.find('a')
for t in a:
self.TS=t
except:
self.flag2=True
self.extractEpisodes()
def extractAirDates(self,soup,check,epli):
s=soup.find_all('div',{'class':'airdate'})
for tag in s:
for ep in tag:
e=re.sub('[^A-Za-z0-9]+', '', ep)
if len(e)<=4:#only year is there
check+=1
ep=e
elif len(e)==7:#only month and year mentioned
ep=e
epli.append(ep)
return [epli,check]
def nextEpisodeDate(self,epli,check):
self.flag1=False
l=len(epli)
if check==l:#all episodes have only year mentioned
for j in range(l):
if epli[j] is not "":
ans=epli[j]
self.flag1=True
break
if self.flag1==False:
self.final="There is no further information(on imdb) about next season's next episode."
else:
self.final="The next season begins in:"+str(ans)+"."
else:
for j in range(0,l-check):#some episodes have only year mentioned
epli[j]=dateConverter(epli[j])
FNE=findNextEpisode(epli[j])
if FNE[0]==True:
self.flag1=True
ans=str(epli[j])
if FNE[1]=="Today":
ans+="("+FNE[1]+")"
break
if self.flag1:#if next episode found
self.final="The next episode airs on:"+str(ans)+"."
else:#next episode not found
for j in range(l-1,-1,-1):
if epli[j] is not "":
ans=epli[j]
break
self.final="The previous episode of series was aired on->"+str(ans) \
+".There is no further information(on imdb) about next season or next episode."
#finding air date of next episode
def extractEpisodes(self):
n=self.TS #total seasons
n=int(n)
print("\n"+self.ostring +" has seasons:"+str(n))
for i in range(1,n+1):
url="https://www.imdb.com/title/"+self.ID+"/episodes?season="+str(i)+"&ref_=tt_eps_sn_"+str(i)
r=requests.get(url)
soup=BeautifulSoup(r.text,"html.parser")
try:
check=0
epli=[]
[epli,check]=self.extractAirDates(soup,check,epli)
#print(epli,check)
self.nextEpisodeDate(epli,check)
if self.flag1:
break
except:
self.flag2=True
##########################################################################################