/app/lib/provider/trailers/hdtrailers.py

https://github.com/ryanrdetzel/CouchPotato · Python · 128 lines · 96 code · 29 blank · 3 comment · 25 complexity · faf7bdedd49c2d98ac727b228c111a34 MD5 · raw file

  1. from app.config.cplog import CPLog
  2. from app.lib.provider.rss import rss
  3. from imdb.parser.http.bsouplxml._bsoup import SoupStrainer, BeautifulSoup
  4. from string import letters, digits
  5. from urllib import urlencode
  6. from urllib2 import URLError
  7. import re
  8. import urllib2
  9. log = CPLog(__name__)
  10. class HdTrailers(rss):
  11. apiUrl = 'http://www.hd-trailers.net/movie/%s/'
  12. backupUrl = 'http://www.hd-trailers.net/blog/'
  13. providers = ['apple.ico', 'yahoo.ico', 'moviefone.ico', 'myspace.ico', 'favicon.ico']
  14. def __init__(self, config):
  15. self.config = config
  16. def conf(self, value):
  17. return self.config.get('Trailer', value)
  18. def find(self, movie):
  19. url = self.apiUrl % self.movieUrlName(movie.name)
  20. log.debug('Searching %s' % url)
  21. try:
  22. data = urllib2.urlopen(url, timeout = self.timeout).read()
  23. except (IOError, URLError), e:
  24. log.debug('Failed to open %s. %s' % (url, e))
  25. return []
  26. p480 = []
  27. p720 = []
  28. p1080 = []
  29. didAlternative = False
  30. for provider in self.providers:
  31. results = self.findByProvider(data, provider)
  32. # Find alternative
  33. if results.get('404') and not didAlternative:
  34. results = self.findViaAlternative(movie.name)
  35. didAlternative = True
  36. p480.extend(results.get('480p'))
  37. p720.extend(results.get('720p'))
  38. p1080.extend(results.get('1080p'))
  39. return {'480p':p480, '720p':p720, '1080p':p1080}
  40. def findViaAlternative(self, movie):
  41. results = {'480p':[], '720p':[], '1080p':[]}
  42. arguments = urlencode({
  43. 's':movie
  44. })
  45. url = "%s?%s" % (self.backupUrl, arguments)
  46. log.debug('Searching %s' % url)
  47. try:
  48. data = urllib2.urlopen(url, timeout = self.timeout).read()
  49. except (IOError, URLError), e:
  50. log.debug('Failed to open %s. %s' % (url, e))
  51. return results
  52. try:
  53. tables = SoupStrainer('div')
  54. html = BeautifulSoup(data, parseOnlyThese = tables)
  55. resultTable = html.findAll('h2', text = re.compile(movie))
  56. for h2 in resultTable:
  57. if 'trailer' in h2.lower():
  58. parent = h2.parent.parent.parent
  59. trailerLinks = parent.findAll('a', text = re.compile('480p|720p|1080p'))
  60. try:
  61. for trailer in trailerLinks:
  62. results[trailer].insert(0, trailer.parent['href'])
  63. except:
  64. pass
  65. except AttributeError:
  66. log.debug('No trailers found in via alternative.')
  67. return results
  68. def findByProvider(self, data, provider):
  69. results = {'480p':[], '720p':[], '1080p':[]}
  70. try:
  71. tables = SoupStrainer('table')
  72. html = BeautifulSoup(data, parseOnlyThese = tables)
  73. resultTable = html.find('table', attrs = {'class':'bottomTable'})
  74. for tr in resultTable.findAll('tr'):
  75. trtext = str(tr).lower()
  76. if 'clips' in trtext:
  77. break
  78. if 'trailer' in trtext and not 'clip' in trtext and provider in trtext:
  79. nr = 0
  80. resolutions = tr.findAll('td', attrs = {'class':'bottomTableResolution'})
  81. #sizes = tr.findNext('tr').findAll('td', attrs = {'class':'bottomTableFileSize'})
  82. for res in resolutions:
  83. results[str(res.a.contents[0])].insert(0, res.a['href'])
  84. #int(sizes[nr].contents[0].replace('MB', ''))
  85. nr += 1
  86. return results
  87. except AttributeError:
  88. log.debug('No trailers found in provider %s.' % provider)
  89. results['404'] = True
  90. return results
  91. def movieUrlName(self, string):
  92. safe_chars = letters + digits + ' '
  93. r = ''.join([char if char in safe_chars else ' ' for char in string])
  94. name = re.sub('\s+' , '-', r).lower()
  95. try:
  96. int(name)
  97. return '-' + name
  98. except:
  99. return name