Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

IMDB Watchlist importer (Issue #146)

  • Loading branch information...
commit e514eb9c8c20bf63fcc36f15df73cf28438bee1a 1 parent 2628189
Aleksey Maksimov authored January 06, 2012
3  .gitignore
@@ -28,6 +28,7 @@ WindowsUpdater.py
28 28
 *.wpr
29 29
 /.idea/
30 30
 *.komodoproject
  31
+*.sw?
31 32
 
32 33
 # OS generated files #
33 34
 ######################
@@ -36,4 +37,4 @@ WindowsUpdater.py
36 37
 desktop.ini
37 38
 ehthumbs.db
38 39
 Thumbs.db
39  
-.directory
  40
+.directory
4  app/config/configApp.py
@@ -173,6 +173,10 @@ def initConfig(self):
173 173
         self.setDefault('Trakt', 'username', '')
174 174
         self.setDefault('Trakt', 'password', '')
175 175
 
  176
+        self.addSection('IMDBWatchlist')
  177
+        self.setDefault('IMDBWatchlist', 'enabled', False)
  178
+        self.setDefault('IMDBWatchlist', 'url', '')
  179
+
176 180
         self.addSection('XBMC')
177 181
         self.setDefault('XBMC', 'enabled', False)
178 182
         self.setDefault('XBMC', 'onSnatch', False)
5  app/lib/cron/__init__.py
@@ -4,6 +4,7 @@
4 4
 from app.lib.cron.movierss import startMovieRSSCron
5 5
 from app.lib.cron.kinepolisrss import startKinepolisRSSCron
6 6
 from app.lib.cron.traktwatchlist import startTraktCron
  7
+from app.lib.cron.imdbwatchlist import startImdbWlCron
7 8
 from app.lib.cron.subtitle import subtitleQueue, startSubtitleCron
8 9
 from app.lib.cron.trailer import startTrailerCron, trailerQueue
9 10
 from app.lib.cron.yarr import startYarrCron
@@ -73,6 +74,10 @@ def start(self):
73 74
         TraktCronJob = startTraktCron(config, self.searchers, self.debug)
74 75
         self.threads['Trakt'] = TraktCronJob
75 76
 
  77
+        #IMDB Watchlist cron
  78
+        IMDBCronJob = startImdbWlCron(config, self.searchers, self.debug)
  79
+        self.threads['IMDBWl'] = IMDBCronJob
  80
+
76 81
         #nzb cronjob
77 82
         yarrCronJob = startYarrCron(config, self.debug, yarrSearch)
78 83
         yarrCronJob.sabNzbd = sabNzbd(config)
98  app/lib/cron/imdbwatchlist.py
... ...
@@ -0,0 +1,98 @@
  1
+from app.config.cplog import CPLog
  2
+from app.config.db import Session as Db, QualityTemplate, Movie
  3
+from app.controllers.movie import MovieController
  4
+from app.lib.cron.base import cronBase
  5
+from app.lib.library import Library
  6
+from app.lib.imdbwl import ImdbWl
  7
+import time
  8
+import traceback
  9
+
  10
+log = CPLog(__name__)
  11
+
  12
+class ImdbWlCron(cronBase, Library):
  13
+    ''' Cronjob for getting imdb watchlist '''
  14
+
  15
+    lastChecked = 0
  16
+    intervalSec = 1800
  17
+    config = {}
  18
+
  19
+    def conf(self, option):
  20
+        return self.config.get('IMDBWatchlist', option)
  21
+
  22
+    def run(self):
  23
+        log.info('IMDB watchlist thread is running.')
  24
+
  25
+        wait = 0.1 if self.debug else 5
  26
+
  27
+        time.sleep(10)
  28
+        while True and not self.abort:
  29
+            now = time.time()
  30
+
  31
+            if (self.lastChecked + self.intervalSec) < now:
  32
+                try:
  33
+                    self.running = True
  34
+                    self.lastChecked = now
  35
+                    self.doCsvCheck()
  36
+                    self.running = False
  37
+                except:
  38
+                    log.error("!!Uncaught exception in IMDB Watchlist thread: %s" % traceback.format_exc())
  39
+
  40
+            time.sleep(wait)
  41
+
  42
+    def isDisabled(self):
  43
+        return not self.conf('enabled')
  44
+
  45
+    def doCsvCheck(self):
  46
+        '''
  47
+        Go find movies and add them!
  48
+        '''
  49
+
  50
+        if self.isDisabled():
  51
+            log.debug('IMDB Watchlist has been disabled')
  52
+            return
  53
+
  54
+        log.info('Starting IMDB Watchlist check')
  55
+        wl = ImdbWl()
  56
+        watchlist = wl.getWatchlist()
  57
+        if not watchlist:
  58
+            log.info("Could not get IMDB watchlist.")
  59
+            return
  60
+
  61
+        MyMovieController = MovieController()
  62
+
  63
+        for movie in watchlist:
  64
+            if self.abort: #this loop takes a while, stop when the program needs to close
  65
+                log.info('Aborting IMDB watchlist check')
  66
+                return
  67
+
  68
+            time.sleep(5) # give the system some slack
  69
+
  70
+            log.debug('Searching for movie: "%s".' % movie['title'])
  71
+            result = False
  72
+            try:
  73
+                result = self.searcher['movie'].findByImdbId(movie['imdb'])
  74
+            except Exception:
  75
+                result = False
  76
+            if not result:
  77
+                log.info('Movie not found: "%s".' % movie['title'])
  78
+                continue
  79
+            try:
  80
+                # Check and see if the movie is in CP already, if so, ignore it.
  81
+                cpMovie = Db.query(Movie).filter_by(imdb = movie['imdb']).first()
  82
+                if cpMovie:
  83
+                    log.info('IMDB Watchlist: Movie found in CP Database, ignore: "%s".' % movie['title'])
  84
+                    continue
  85
+                log.info('Adding movie to queue: %s.' % movie['title'])
  86
+                quality = Db.query(QualityTemplate).filter_by(name = self.config.get('Quality', 'default')).one()
  87
+                MyMovieController._addMovie(result, quality.id)
  88
+            except:
  89
+                log.info('MovieController unable to add this movie: "%s". %s' % (movie['title'], traceback.format_exc()))
  90
+
  91
+def startImdbWlCron(config, searcher, debug):
  92
+    cron = ImdbWlCron()
  93
+    cron.config = config
  94
+    cron.searcher = searcher
  95
+    cron.debug = debug
  96
+    cron.start()
  97
+
  98
+    return cron
49  app/lib/imdbwl.py
... ...
@@ -0,0 +1,49 @@
  1
+from app.config.cplog import CPLog
  2
+import cherrypy
  3
+import urllib
  4
+import csv
  5
+
  6
+log = CPLog(__name__)
  7
+
  8
+def conf(options):
  9
+    return cherrypy.config['config'].get('IMDBWatchlist', options)
  10
+
  11
+class ImdbWl:
  12
+
  13
+    def __init__(self):
  14
+        self.enabled = conf('enabled')
  15
+
  16
+    def call(self, url = None):
  17
+        log.debug("Call method")
  18
+
  19
+        if not url:
  20
+            url = conf('url')
  21
+
  22
+        watchlist = []
  23
+        try:
  24
+            log.info('Retrieving IMDB Watchlist CSV from %s' % url)
  25
+            urllib._urlopener = ImdbUrlOpener()
  26
+            tmp_csv, headers = urllib.urlretrieve(url)
  27
+            csvwl = csv.reader(open(tmp_csv, 'rb'))
  28
+            for row in csvwl:
  29
+                if row[0] != 'position':
  30
+                    #log.info('Row is %s' % row)
  31
+                    movie = {}
  32
+                    movie['imdb'] = row[1]
  33
+                    movie['title'] = '%s (%s)' % (row[5], row[10]) 
  34
+                    
  35
+                    watchlist.append(movie)
  36
+        except(IOError):
  37
+            log.info("Failed calling downloading/parsing IMDB Watchlist CSV")
  38
+            watchlist = None
  39
+        return watchlist
  40
+
  41
+    def test(self, url):
  42
+        wl = self.call('http://www.imdb.com/list/export?list_id=watchlist&author_id=ur0034213')
  43
+        print wl
  44
+
  45
+    def getWatchlist(self):
  46
+        return self.call()
  47
+    
  48
+class ImdbUrlOpener(urllib.FancyURLopener):
  49
+    version = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11'
27  app/views/config/index.html
@@ -409,7 +409,7 @@
409 409
 				</div>
410 410
 			</fieldset>
411 411
 
412  
-            		<fieldset class="inlineLabels">
  412
+           	<fieldset class="inlineLabels">
413 413
 				<h3>#alt.binaries.hdtv.x264@EFnet</h3>
414 414
 				<div class="ctrlHolder checkbox">
415 415
 					<label>Enable</label>
@@ -443,6 +443,14 @@
443 443
 			</fieldset>
444 444
 		</div>
445 445
 
  446
+		<div class="group imdb">
  447
+			<fieldset class="inlineLabels">
  448
+				<div>Got a jailbroken iPhone? Get the CouchPotato app on Cydia! <span>(search for "<strong>iCouchPotato</strong>")</span></div>
  449
+				<img src="${baseUrl}media/images/iphone1.png" /><img src="${baseUrl}media/images/iphone2.png" /><img src="${baseUrl}media/images/iphone3.png" />
  450
+				<div>Why isn't it in the normal Appstore you ask? Because Apple is being a bitch!</div>
  451
+			</fieldset>
  452
+		</div>
  453
+
446 454
 		<div class="group iphone">
447 455
 			<fieldset class="inlineLabels">
448 456
 				<div>Got a jailbroken iPhone? Get the CouchPotato app on Cydia! <span>(search for "<strong>iCouchPotato</strong>")</span></div>
@@ -730,6 +738,23 @@
730 738
 					<input type="checkbox" name="MovieETA.enabled" value="True" ${' checked="checked"' if config.get('MovieETA', 'enabled') else ''} />
731 739
 				</div>
732 740
 			</fieldset>
  741
+			<fieldset class="inlineLabels">
  742
+				<h3>IMDB Watchlist</h3>
  743
+				<div class="ctrlHolder checkbox">
  744
+					<label>Enable</label>
  745
+					<input type="checkbox" name="IMDBWatchlist.enabled" value="True" ${' checked="checked"' if config.get('IMDBWatchlist', 'enabled') else ''} />
  746
+					<p class="formHint">
  747
+						This will magically add movies to your wanted list that appear in public IMDB Watchlist specified below.
  748
+					</p>
  749
+				</div>
  750
+				<div class="ctrlHolder">
  751
+					<label>IMDB Watchlist URL</label>
  752
+					<input type="text" name="IMDBWatchlist.url" value="${config.get('IMDBWatchlist', 'url')}" class="textInput large"/>
  753
+					<p class="formHint">
  754
+						'Export this list' URL of the public IMDB Watchlist
  755
+					</p>
  756
+				</div>
  757
+			</fieldset>
733 758
 		</div>
734 759
 		<div class="group notifications">
735 760
 			<fieldset class="inlineLabels" id="xbmcFieldset">

0 notes on commit e514eb9

Please sign in to comment.
Something went wrong with that request. Please try again.