This repository has been archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetshow.py
377 lines (313 loc) · 9.68 KB
/
getshow.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import urllib3
import subprocess
import requests
import time
import os
import sys
import sqlite3
import platform
#top
try:
input = raw_input
except NameError:
pass
global favtv
global favmve
global tvgenres
global moviegenres
favtv = []
favmve = []
tvgenres = []
moviegenres = []
MYDB = homedir + "myplex.db"
http = urllib3.PoolManager()
sql = sqlite3.connect(MYDB)
cur = sql.cursor()
FIXME = homedir + "fixme.txt"
PROBLEMS = homedir + "problems.txt"
with open (PROBLEMS, "w") as file:
file.write('')
file.close()
ostype = platform.system()
cur.execute('CREATE TABLE IF NOT EXISTS settings(item TEXT, setting TEXT)')
sql.commit()
command = 'SELECT setting FROM settings WHERE item LIKE \'TVPART\''
cur.execute(command)
if not cur.fetchone():
print ("Looks like you have never run the update DB script. I need some information to proceed.\n Enter the link to your metadata.\n Example: http://192.168.1.134:32400/library/metadata/\n")
TVPART = str(input('Link:'))
cur.execute('INSERT INTO settings VALUES(?, ?)', ("TVPART",TVPART.strip()))
sql.commit()
print (TVPART + " has been added to the settings table. Moving on.")
else:
cur.execute(command)
test2 = cur.fetchone()[0]
TVPART = test2
command = 'SELECT setting FROM settings WHERE item LIKE \'TVGET\''
cur.execute(command)
if not cur.fetchone():
print ("Enter the link to your TV show tree.\nExample: http://192.168.1.134:32400/library/sections/1/all/ \n")
TVGET = str(input('Link:'))
cur.execute('INSERT INTO settings VALUES(?, ?)', ("TVGET",TVGET.strip()))
sql.commit()
print (TVGET + " has been added to the settings table. Moving on.")
else:
cur.execute(command)
test1 = cur.fetchone()[0]
TVGET = test1
command = 'SELECT setting FROM settings WHERE item LIKE \'MOVIEGET\''
cur.execute(command)
if not cur.fetchone():
print ("Enter the link to your Movie tree.\nExample: http://192.168.1.134:32400/library/sections/2/all/ \n")
MOVIEGET = str(input('Link:'))
cur.execute('INSERT INTO settings VALUES(?, ?)', ("MOVIEGET",MOVIEGET.strip()))
sql.commit()
print (MOVIEGET + " has been added to the settings table. Moving on.")
else:
cur.execute(command)
test = cur.fetchone()[0]
MOVIEGET = test
print ("Database update starting...\n")
cur.execute('CREATE TABLE IF NOT EXISTS shows(TShow TEXT, Episode TEXT, Season INT, Enum INT, Tnum INT, Summary TEXT, Link TEXT)')
sql.commit()
cur.execute('CREATE TABLE IF NOT EXISTS Movies(Movie TEXT, Summary TEXT, Rating TEXT, Tagline TEXT, Genre TEXT, Director TEXT, Actors TEXT)')
sql.commit()
cur.execute('CREATE TABLE IF NOT EXISTS TVshowlist(TShow TEXT, Summary TEXT, Genre TEXT, Rating TEXT, Duration INT, Totalnum INT)')
sql.commit()
def getshow(findme):
print (findme)
response = http.urlopen('GET', TVGET, preload_content=False).read()
response = str(response)
#print (response)
shows = response.split('<Directory ratingKey=')
counter = 1
while counter <= int(len(shows)-1):
show = shows[counter]
genres = show
studio = show
title = show
title = title.split('title="')
title = title[1]
title = title.split('"')
title = title[0]
title = title.replace(''','\'')
title = title.replace('&','&')
title = title.replace('?','')
title = title.replace('/',' ')
#print (title)
if findme.lower().strip() == title.lower().strip():
print ("FOUND!!")
cur.execute("DELETE FROM shows WHERE Tshow LIKE \"" + findme + "\"")
sql.commit()
summary = show
rating = show
duration = show
totalnum = show
summary = show
summary = summary.split('summary="')
summary = summary[1]
summary = summary.split('"')
summary = summary[0]
summary = summary.replace('\'','')
rating = show
try:
rating = rating.split('contentRating="')
rating = rating[1]
rating = rating.split('"')
rating = rating[0]
except Exception:
rating = 'N\A'
duration = show
duration = duration.split('duration="')
duration = duration[1]
duration = duration.split('"')
duration = duration[0]
duration = int(duration)/60000
totalnum = show
totalnum = totalnum.split(' leafCount="')
totalnum = totalnum[1]
totalnum = totalnum.split('"')
totalnum = totalnum[0]
name = title
TShow = name
genres = genres.split("<Genre tag=\"")
try:
genre = genres[1]
except IndexError:
genre = "none"
try:
genre2 = genres[2]
genre2 = genre2.split('" />')
genre2 = genre2[0]
#print (genre2)
except IndexError:
genre2 = "none"
try:
genre3 = genres[3]
genre3 = genre3.split('" />')
genre3 = genre3[0]
#print (genre2)
except IndexError:
genre3 = "none"
genre = genre.split('" />')
genre = genre[0] + ";" + genre2 + ";" + genre3 + ";"
genre = genre.replace('none;','')
#print (genre)
studio = studio.split("studio=\"")
try:
studio = studio[1]
studio = studio.split("\"")
studio = studio[0]
except IndexError:
studio = "None"
TShow = TShow.replace("'","''")
summary = summary.replace("'","''")
summary = str(summary.encode('ascii','ignore')).strip()
cur.execute("SELECT * FROM TVshowlist WHERE TShow LIKE \"" + TShow + "\"")
try:
if not cur.fetchone():
cur.execute("INSERT INTO TVshowlist VALUES(?, ?, ?, ?, ?, ?)", (TShow, summary, genre, rating, int(duration), int(totalnum)))
sql.commit()
except Exception:
print ("Error adding " + TShow)
with open(PROBLEMS, 'a') as file:
file.write(TShow + "\n")
file.close()
counter = counter + 1
print ("TV entries checked.")
def gettvshows(findme):
response = http.urlopen('GET', TVGET, preload_content=False).read()
response = str(response)
#print (response)
shows = response.split('<Directory ratingKey=')
counter = 1
while counter <= int(len(shows)-1):
show = shows[counter]
genres = show
studio = show
title = show
title = title.split('title="')
title = title[1]
title = title.split('"')
title = title[0]
title = title.replace(''','\'')
title = title.replace('&','&')
title = title.replace(''','\'')
title = title.replace('?','')
title = title.replace('/',' ')
if title.lower().strip() == findme.lower().strip():
name = title
TShow = name
if (("'" in TShow) and ("''" not in TShow)):
TShow = TShow.replace("'","''")
print (TShow)
show = show.split('" key')
show = show[0]
show = show.replace("\"", "")
show = show.rstrip()
episode = show
link = TVPART + show + "/allLeaves"
print (link)
xresponse = http.urlopen('GET', link, preload_content=False).read()
xresponse = str(xresponse)
episodes = xresponse.split('type="episode" title="')
#print (episodes)
for episode in episodes:
Season = episode
Enum = episode
Summary = episode
Link = episode
episode = episode.split('"')
episode = episode[0]
episode = episode + "\n"
episode = episode.replace(''','\'')
episode = episode.replace('&','&')
episode = episode.replace('&','&')
episode = episode.replace(''','\'')
Episode = episode.strip()
if ("<?xml version=" in episode.strip()):
#print ("Pass")
Tnum = 0
else:
if ("(" in episode):
xepisode = name + " " + episode
with open(FIXME, 'a') as file:
file.write(xepisode)
file.close()
#episode = episode.rstrip()
#print (episode)
if episode != "Original":
#print ("Skipping")
#else:
#with open(title, "a") as file:
#file.write(episode)
#file.close()
try:
Tnum = Tnum + 1
except Exception:
Tnum = 0
#print (Season)
Season = Season.split('parentIndex="')
#print (Season)
Season = Season[1]
Season = Season.split('"')
Season = Season[0]
Enum = Enum.split('index="')
Enum = Enum[1]
Enum = Enum.split('"')
Enum = Enum[0]
Summary = Summary.split('summary="')
Summary = Summary[1]
Summary = Summary.split('" index')
Summary = Summary[0]
Summary = Summary.replace(",", "")
Summary = Summary.replace('\xe2',"")
Summary = Summary.replace(""","")
try:
Summary = Summary.decode("ascii", "ignore")
except Exception:
pass
#Summary = remove_accents(Summary)
Link = Link.split('<Part id=')
Link = Link[1]
Link = Link.split('key="')
Link = Link[1]
Link = Link.split('" duration')
Link = Link[0]
TShow = str(TShow)
#TShow = TShow.replace("'","''")
#print (TShow)
Episode = str(Episode)
Episode = Episode.replace("'","''")
#print (Episode)
Season = int(Season)
#print (str(Season))
Enum = int(Enum)
#print (str(Enum))
Tnum = int(Tnum)
#print (str(Tnum))
Summary = str(Summary.encode('ascii','ignore').strip())
Summary = Summary.replace("'","''")
#print (Summary)
Link = str(Link.strip().encode('ascii','replace'))
#print (Link)
cur.execute("SELECT * FROM shows WHERE TShow LIKE \"" + TShow + "\" AND Tnum LIKE \"" + str(Tnum) + "\"")
try:
if not cur.fetchone():
cur.execute("INSERT INTO shows VALUES(?, ?, ?, ?, ?, ?, ?)", (TShow, Episode, Season, Enum, Tnum, Summary, Link))
sql.commit()
print ("New Episode Found: " + TShow + " Episode: " + Episode)
except Exception:
print ("Error adding " + TShow)
with open(PROBLEMS, 'a') as file:
file.write(TShow + " " + Episode + "\n")
file.close()
counter = counter + 1
print ("TV entries checked.")
try:
findme = str(sys.argv[1])
getshow(findme)
#gettvshows(findme)
except IndexError:
print ("Error: You must specify a show to use this command.")
print ("Done")