forked from Ichabond/Pythonbits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonbits.py
More file actions
executable file
·665 lines (569 loc) · 20.7 KB
/
pythonbits.py
File metadata and controls
executable file
·665 lines (569 loc) · 20.7 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#!/usr/bin/env python
# coding =<iso-8859-15>
# Copyright (c) 2010, scootypuffjr
# Copyright (c) 2010, Apollo
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the organization nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL SCOOTYPUFFJR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Python module to retrieve information from imdb.com and Media Info for baconbits"""
__version__ = 0.2
__author__ = "Apollo"
import urllib
import urllib2
import httplib
import mimetypes
import urlparse
import socket
import sys
import re
import subprocess
import tempfile
import os
import base64
import json
import MultipartPostHandler
from xml.dom.minidom import Document, parse
from optparse import OptionParser, SUPPRESS_HELP
__htmlparser = True
try:
from HTMLParser import HTMLParser
except:
__htmlparser = False
def __logerror(msg):
sys.stderr.write(msg)
if __htmlparser:
__converter = HTMLParser()
def tempdir():
return tempfile.gettempdir()+os.sep
def decode(text):
"""Takes a string and replaces any html entities it contains with their unicode
counterparts.
"""
if __htmlparser:
## HACK, HTMLParser() sucks @ utf-8
return __converter.unescape(text.decode('utf-8')).encode('utf-8')
else:
charrefpat = re.compile(r'&(#(\d+|x[\da-fA-F]+)|[\w.:-]+);?')
from htmlentitydefs import name2codepoint
if type(text) is unicode:
uchr = unichr
else:
uchr = lambda value: value > 255 and unichr(value) or chr(value)
def entitydecode(match, uchr=uchr):
entity = match.group(1)
if entity.startswith('#x'):
return uchr(int(entity[2:], 16))
elif entity.startswith('#'):
return uchr(int(entity[1:]))
elif entity in name2codepoint:
return uchr(name2codepoint[entity])
else:
return match.group(0)
return charrefpat.sub(entitydecode, text)
class FetchError(Exception):
def __init__(self, value):
self.parameter = value
def __str__(self):
return repr(self.parameter)
class URLError(Exception):
def __init__(self, value):
self.parameter = value
def __str__(self):
return repr(self.parameter)
class Error404(Exception):
def __init__(self, value):
self.parameter = value
def __str__(self):
return repr(self.parameter)
class _MyOpener(urllib.FancyURLopener):
version = 'Opera/9.80 (fX11; Linux i686; U; en) Presto/2.2.15 Version/10.00'
class pythonbits_config:
"""Class for holding pythonbits config strings. read() or create_dom() must be called before first use. Access strings through obj.strings[key]"""
def __init__(self):
self.strings={}
self.file=tempdir()+"config.xml"
if not os.path.exists(self.file):
update_url = "https://github.com/Ichabond/Pythonbits/raw/master/config.xml"
opener = _MyOpener()
nconf = opener.open(update_url)
if (nconf.info()["Status"]=="200 OK"):
open(tempdir()+"config.xml", "w").write(nconf.read())
else:
__logerror("Cannot update config file.")
def __del__(self):
self.file.close()
def read(self, file=0):
if(file==0):
file=self.file
self.xml = parse(file)
self.load_strings()
def write(self, file=0):
if(file==0):
file=self.file
location = self.file.name
file.close()
file = open(location, "w")
file.write(self.xml.toprettyxml())
file.close()
self.file = open(location, "r")
def set_location(self, location):
try:
self.file = open(location, "r")
except IOError:
self.file = open(location, "w")
def load_strings(self):
for node in self.xml.getElementsByTagName("string"):
self.strings[node.getAttribute("name")]=node.firstChild.data.replace('\n','').replace('\t','')
def add_string(self, name, data):
container = self.xml.getElementsByTagName("pythonbits")[0]
stringtag = self.xml.createElement("string")
stringtag.setAttribute("name", name)
stringtag.appendChild(self.xml.createTextNode(data))
container.appendChild(stringtag)
self.load_strings()
def del_string(self, name):
del self.strings[name]
###Horrible hack. Write real code.
self.create_dom()
for (name, entry) in self.strings.items():
self.add_string(name, entry)
def create_dom(self):
self.xml = Document()
self.xml.appendChild(self.xml.createElement("pythonbits"))
class search(object):
"""Takes a search string as an argument. Uses google's site search feature.
It save's the results as a list of tuples of title and url.
"""
def __init__(self, searchString):
self.searchString = searchString
self.results = []
self.opener = _MyOpener()
self.feed = self.opener.open(conf.strings["google_url"] % searchString.strip().replace(" ", "+")).read()
templist = re.findall(conf.strings["google_imdb_result_re"], self.feed, re.DOTALL)
for i in templist:
if len(i) > 1:
self.results.append((re.sub(r"<[^>]+>","",decode(i[1])),i[0]))
def __str__(self):
return self.searchString
def __unicode__(self):
return unicode(self.searchString)
def __repr__(self):
return repr(self.results)
def __getitem__(self, index):
return self.results[index]
def __len__(self):
return len(self.results)
def pop(self):
return self.results.pop()
def reverse(self):
self.results.reverse()
class imdb(object):
"""Takes an imdb url as a parameter and returns an object with attributes scraped
from the imdb page.
"""
def __init__(self,url):
self.url = url
self.data = {}
self.errors = False
self.director = ''
self.shortdescription = ''
self.tagline = ''
self.title = ''
self.genre = []
self.releasedate = ''
self.plotkeywords = []
self.plot = ''
self.seasons = []
self.awards = ''
self.writers = []
self.rating = ''
self.cast = []
self.runtime = ''
self.country = []
self.mpaa = ''
self.languages = []
self.alsoknownas = []
self.color = ''
self.aspectratio = ''
self.soundmix = []
self.filminglocations = []
self.company = []
self.summary = ''
self.trailerurl = ''
self.wikiurl = ''
self.mediainfo = ''
self.trivia = ''
if not re.match(conf.strings["imdb_url_re"] ,self.url):
raise URLError("Invalid URL")
if self.url.endswith("/"):
self.url = self.url[:-1]
if not self.url.startswith("http://"):
self.url = "http://" + self.url
self.opener = _MyOpener()
try:
self.feed = self.opener.open(self.url)
except:
raise FetchError("Error connecting to IMDB")
if self.feed.getcode() != 404:
self.feed = self.feed.read()
else:
raise Error404("IMDB returned 404")
self.__parsePage(self.feed)
def __str__(self):
return self.title
def __unicode__(self):
return unicode(self.title)
def __getitem__(self, key):
return self.data[key]
def __setitem__(self, key, value):
self.data[key] = value
def overview(self, bbcode=False):
"""Provides a short overview of the film or TV series. Optional argument turns on
BBCode formatting.
"""
overview = ""
if bbcode:
format = ("[b]","[/b]")
else:
format = ("","")
overview += "%sIMDB url:%s " % format + self.url
if self.title:
overview += "\n%sTitle:%s " % format + self.title
if self.rating:
overview += "\n%sRating:%s " % format + self.rating
if self.director:
if len(self.director) > 1:
overview += "\n%sDirectors:%s " % format + " | ".join(self.director)
else:
overview += "\n%sDirector:%s " % format + " | ".join(self.director)
if self.languages:
if len(self.languages) > 1:
overview += "\n%sLanguages:%s " % format + " | ".join(self.languages)
else:
overview += "\n%sLanguage:%s " % format + " | ".join(self.languages)
#if self.aspectratio:
# overview += "\n%sAspect Ratio:%s " % format + self.aspectratio
if self.releasedate:
overview += "\n%sRelease Date:%s " % format + self.releasedate
if self.seasons:
overview += "\n%sSeasons:%s " % format + " | ".join(self.seasons)
if self.genre:
overview += "\n%sGenre:%s " % format + " | ".join(self.genre)
if self.country:
if len(self.country) > 1:
overview += "\n%sCountries:%s " % format + " | ".join(self.country)
else:
overview += "\n%sCountry:%s " % format + " | ".join(self.country)
return overview
def __parsePage(self, page):
"""Scrapes html from IMDB for information."""
# director
self.director = re.findall(conf.strings["imdb_director_re"], page, re.MULTILINE)
self.__setitem__("director", self.director)
# tagline
match = re.findall(conf.strings["imdb_tagline_re"], page, re.DOTALL)
if match:
self.tagline = decode(match[0])
self.__setitem__("tagline", self.tagline)
# short description
match = re.findall(conf.strings["imdb_description_re"], page,re.MULTILINE)
if match:
self.shortdescription = decode(match[0])
self.__setitem__("shortdescription", self.shortdescription)
# plot
#match = re.findall(conf.strings["imdb_plot_re"], page, re.MULTILINE)
#i3f match:
# self.plot = match[0]
#self.__setitem__("plot", self.plot)
# title
match = re.findall(conf.strings["imdb_title_re"], page)
if match:
self.title = decode(match[0].strip())
self.__setitem__("title", self.title)
# genre
self.genre = re.findall(conf.strings["imdb_genre_re"], page, re.MULTILINE)
self.__setitem__("genre", self.genre)
# release date
match = re.findall(conf.strings["imdb_releasedate_re"], page, re.MULTILINE)
if match:
self.releasedate = match[0]
self.__setitem__("releasedate", self.releasedate)
# keywords
#self.plotkeywords = map(decode, re.findall(conf.strings["imdb_plotkeywords_re"],page, re.MULTILINE))
#self.__setitem__("plotkeywords", self.plotkeywords)
# awards
#match = re.findall(conf.strings["imdb_awards_re"], page, re.MULTILINE)
#if match:
# self.awards = decode(match[0].replace('\n',' ').strip())
#self.__setitem__("awards", self.awards)
# writers
self.writers = map(decode, re.findall(conf.strings["imdb_writers_re"], page, re.MULTILINE))
self.__setitem__("writers", self.writers)
# rating
match = re.findall(conf.strings["imdb_rating_re"], page, re.MULTILINE)
if match:
self.rating = match[0]
self.__setitem__("rating", self.rating)
# cast
#castsearch = re.compile(conf.strings["imdb_castsearch_re"], re.DOTALL)
#self.cast = re.findall( castsearch, page)
#x = 0
#for member in self.cast:
# if not member[2]:
# self.cast[x] = tuple(map(decode,member[0:2]))
# x=x+1
#self.__setitem__("cast", self.cast)
# runtime
match = re.findall(conf.strings["imdb_runtime_re"], page, re.MULTILINE)
if match:
self.runtime = decode(match[0].strip())
self.__setitem__("runtime", self.runtime)
# countries
self.country = map(decode, re.findall(conf.strings["imdb_country_re"], page, re.MULTILINE))
self.__setitem__("country", self.country)
# mpaa rating
#match = map(decode, re.findall(conf.strings["imdb_mpaa_re"], page, re.MULTILINE))
#if match:
# self.mpaa = decode(match[0])
#self.__setitem__("mpaa", self.mpaa)
# languages
self.languages = map(decode, re.findall(conf.strings["imdb_languages_re"], page, re.MULTILINE))
self.__setitem__("languages", self.languages)
# also known as
#match = re.findall(conf.strings["imdb_alsoknownas_re"], page, re.MULTILINE)
#if match:
# templist = decode(match[0]).split('<br>')
# for i in templist:
# if i.strip():
# self.alsoknownas.append(i.strip())
#self.__setitem__("alsoknownas", self.alsoknownas)
# color
#match = re.findall(conf.strings["imdb_color_re"], page, re.MULTILINE)
#if match:
# self.color = match[0]
#self.__setitem__("color", self.color)
# aspect ratio
#match = re.findall(conf.strings["imdb_aspectratio_re"], page, re.MULTILINE)
#if match:
# self.aspectratio = match[0].strip()
#self.__setitem__("aspectratio", self.aspectratio)
# sound mixes
#self.soundmix = re.findall(conf.strings["imdb_soundmix_re"], page, re.MULTILINE)
#self.__setitem__("soundmix", self.soundmix)
# filming locations
self.filminglocations = map(decode, re.findall(conf.strings["imdb_filminglocations_re"],page, re.MULTILINE))
self.__setitem__("filminglocations", self.filminglocations)
# companies
#self.company = map(decode, re.findall(conf.strings["imdb_company_re"], page, re.MULTILINE))
#self.__setitem__("company", self.company)
# trivia
#match = re.findall(conf.strings["imdb_trivia_re"], page, re.DOTALL)
#if match:
# self.trivia = decode(re.sub(conf.strings["imdb_trivia_more_re"],'', match[0], re.DOTALL).replace("\n","").strip())
#self.__setitem__("trivia", self.trivia)
# seasons
self.seasons = re.findall(conf.strings["imdb_seasons_re"], page )
self.__setitem__("seasons", self.seasons)
def getSummary(self):
"""returns the full summary for the movie"""
if self.summary:
return True
try:
synopsisPage = self.opener.open(self.url + "/plotsummary").read()
except:
return False
match = re.findall(conf.strings["imdb_summary_re"], synopsisPage, re.MULTILINE)
if match:
self.summary = match
return True
elif self.shortdescription:
self.summary = [self.shortdescription]
return True
def findTrailer(self):
"""searches for the trailer on youtube"""
if not self.title:
return False
results = self.opener.open(conf.strings["google_youtube_url"] +\
urllib.quote(self.title.strip().replace(" ","+"))).read()
results = re.findall(conf.strings["google_youtube_result_re"], results)
if results:
for result in results:
if re.search(self.title, result[1], re.IGNORECASE) and re.search(conf.strings["youtube_trailer_re"], result[1], re.IGNORECASE ):
self.trailerurl = result[0]
return True
return False
def findWiki(self):
"""Tries in a somewhat rudimentary way to find the wikipedia url. If successful, it sets the attribute
"self.wikiurl" and returns True. Otherwise, it returns False.
"""
searchstring = ""
if self.title:
searchstring += urllib.quote(self.title.strip().replace(" ","+"))
if self.director:
searchstring += urllib.quote("+" + self.director[0].strip().replace(" ","+"))
if self.releasedate:
match = re.findall(r"\d{4}", self.releasedate)
if match:
searchstring += "+" + match[0]
if searchstring:
results = self.opener.open(conf.strings["google_wikipedia_url"] +\
searchstring).read()
links = re.findall(conf.strings["wikipedia_url"], results)
if links:
self.wikiurl = urlparse.urljoin(links[0], urllib.quote(urlparse.urlparse(links[0]).path))
return True
return False
def findMediaInfo(self, path):
try:
if(os.name=="nt"):
##Must pass Shell=True on Windows, but this is a potential security issue
self.mediainfo = subprocess.Popen([r"mediainfo",path], shell=True, stdout=subprocess.PIPE).communicate()[0]
else:
self.mediainfo = subprocess.Popen([r"mediainfo",path], stdout=subprocess.PIPE).communicate()[0]
except OSError:
sys.stderr.write("Error: Media Info not installed, refer to http://mediainfo.sourceforge.net/en for installation")
exit(1)
return True
class Imgur(object):
def __init__(self, path, shots = 2):
self.path = path
self.imageurl = []
self.key = conf.strings["imgur_key"]
self.tries = 0
self.duration = ''
self.ffmpeg = ''
if shots < 2:
self.shots = 2
sys.stderr.write('Number of screenshots increased to 2\n')
elif shots > 7:
self.shots = 7
sys.stderr.write('Number of screenshots limited to 7\n')
else:
self.shots = shots
def getDuration(self):
try:
self.ffmpeg = subprocess.Popen([r"ffmpeg","-i",self.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except OSError:
sys.stderr.write("Error: Ffmpeg not installed, refer to http://www.ffmpeg.org/download.html for installation")
exit(1)
self.duration = re.findall(r'Duration:\D(\d{2}):(\d{2}):(\d{2})', self.ffmpeg.stdout.read())
self.duration = int(self.duration[0][0]) * 3600 + int(self.duration[0][1]) * 60 + int(self.duration[0][2])
def upload(self):
self.getDuration()
# Take screenshots at even increments between 20% and 80% of the duration
stops = range(20,81,60/(self.shots-1))
try:
count=0
imgs = []
for stop in stops:
imgs.append(tempdir()+"screen%d.png" % count)
subprocess.Popen([r"ffmpeg","-ss",str((self.duration * stop)/100), "-vframes", "1", "-i", self.path , "-y", "-sameq", "-f", "image2", imgs[-1] ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).wait()
count+=1
except OSError:
sys.stderr.write("Error: Ffmpeg not installed, refer to http://www.ffmpeg.org/download.html for installation")
exit(1)
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
try:
for img in imgs:
params = ({'key' : self.key.decode('utf-8').encode('utf-8'), 'image' : open(img, "rb")})
socket = opener.open("http://api.imgur.com/2/upload.json", params)
read = json.loads(socket.read())
self.imageurl.append(read['upload']['links']['original'])
socket.close()
os.remove(img)
return True
except urllib2.URLError as s:
if self.tries < 3:
self.tries += 1
sys.stderr.write('Connection timed out, retrying to upload screenshots to imgur. This is try: ')
sys.stderr.write(str(self.tries))
sys.stderr.write('\n')
self.upload()
return True
if __name__ == "__main__":
results = ''
usage = "%prog [OPTION]..."
epilog = "Standard Operation takes the movie name and the Filename"
parser = OptionParser(usage=usage, epilog=epilog, version="%%prog %0.2f" % __version__)
parser.add_option("-u", "--update", action="store_true", dest="update")
(options, args) = parser.parse_args()
if(options.update):
try:
conf = pythonbits_config()
conf.set_location(tempdir()+"config.xml")
conf.read()
update_url = conf.strings["update_url"]
except:
update_url = "https://github.com/Ichabond/Pythonbits/raw/master/config.xml"
del conf
opener = _MyOpener()
newconf = opener.open(update_url)
if (newconf.info()["Status"]=="200 OK"):
open(tempdir()+"config.xml", "w").write(newconf.read())
else:
__logerror("Cannot update config file.")
conf = pythonbits_config()
conf.set_location(tempdir()+"config.xml")
try:
conf.read()
except:
update_url = "https://github.com/Ichabond/Pythonbits/raw/master/config.xml"
opener = _MyOpener()
conf = opener.open(update_url)
if (conf.info()["Status"]=="200 OK"):
open(tempdir()+"config.xml", "w").write(conf.read())
else:
__logerror("Cannot update config file.")
if len(args) != 2:
__logerror("Not enough arguments, refer to --help for additional info")
exit(1)
filename = args[1]
results = search(args[0]).results
if results:
movie = imdb(results[0][1])
if conf.strings.has_key("screenshot_num"):
imgur = Imgur(filename, int(conf.strings["screenshot_num"]))
else:
imgur = Imgur(filename)
else:
__logerror("No films found.\n")
exit(1)
print "[b]Description:[/b]"
if movie.getSummary():
print "[quote]%s[/quote]\n" % movie.summary[0]
print "[b]Information:[/b]"
print "[quote]"
if movie.findWiki():
print "Wikipedia url: %s" % movie.wikiurl
if movie.findTrailer():
print "Trailer: %s" % movie.trailerurl
print movie.overview(False) + "[/quote]"
print "[b]Screenshots:[/b]"
if imgur.upload():
print "[quote][align=center]"
for url in imgur.imageurl:
print "[img=%s]" % url
print "[/align][/quote]"
if movie.findMediaInfo(filename):
print "[mediainfo] %s [/mediainfo]" % movie.mediainfo
exit(0)