Skip to content

Commit

Permalink
Merge pull request #422 from lechat/master
Browse files Browse the repository at this point in the history
IMDB Watchlist importer
  • Loading branch information
RuudBurger committed Jan 7, 2012
2 parents 2628189 + 3c4cfcc commit dd5704a
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -28,6 +28,7 @@ WindowsUpdater.py
*.wpr
/.idea/
*.komodoproject
*.sw?

# OS generated files #
######################
Expand All @@ -36,4 +37,4 @@ WindowsUpdater.py
desktop.ini
ehthumbs.db
Thumbs.db
.directory
.directory
4 changes: 4 additions & 0 deletions app/config/configApp.py
Expand Up @@ -173,6 +173,10 @@ def initConfig(self):
self.setDefault('Trakt', 'username', '')
self.setDefault('Trakt', 'password', '')

self.addSection('IMDBWatchlist')
self.setDefault('IMDBWatchlist', 'enabled', False)
self.setDefault('IMDBWatchlist', 'url', '')

self.addSection('XBMC')
self.setDefault('XBMC', 'enabled', False)
self.setDefault('XBMC', 'onSnatch', False)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/config.py
Expand Up @@ -90,6 +90,7 @@ def save(self, **data):
'Subtitles.enabled', 'Subtitles.addLanguage',
'MovieRSS.enabled',
'KinepolisRSS.enabled',
'IMDBWatchlist.enabled',
]
)
data.update(data.fromkeys(bools, False))
Expand Down
5 changes: 5 additions & 0 deletions app/lib/cron/__init__.py
Expand Up @@ -4,6 +4,7 @@
from app.lib.cron.movierss import startMovieRSSCron
from app.lib.cron.kinepolisrss import startKinepolisRSSCron
from app.lib.cron.traktwatchlist import startTraktCron
from app.lib.cron.imdbwatchlist import startImdbWlCron
from app.lib.cron.subtitle import subtitleQueue, startSubtitleCron
from app.lib.cron.trailer import startTrailerCron, trailerQueue
from app.lib.cron.yarr import startYarrCron
Expand Down Expand Up @@ -73,6 +74,10 @@ def start(self):
TraktCronJob = startTraktCron(config, self.searchers, self.debug)
self.threads['Trakt'] = TraktCronJob

#IMDB Watchlist cron
IMDBCronJob = startImdbWlCron(config, self.searchers, self.debug)
self.threads['IMDBWl'] = IMDBCronJob

#nzb cronjob
yarrCronJob = startYarrCron(config, self.debug, yarrSearch)
yarrCronJob.sabNzbd = sabNzbd(config)
Expand Down
98 changes: 98 additions & 0 deletions app/lib/cron/imdbwatchlist.py
@@ -0,0 +1,98 @@
from app.config.cplog import CPLog
from app.config.db import Session as Db, QualityTemplate, Movie
from app.controllers.movie import MovieController
from app.lib.cron.base import cronBase
from app.lib.library import Library
from app.lib.imdbwl import ImdbWl
import time
import traceback

log = CPLog(__name__)

class ImdbWlCron(cronBase, Library):
''' Cronjob for getting imdb watchlist '''

lastChecked = 0
intervalSec = 1800
config = {}

def conf(self, option):
return self.config.get('IMDBWatchlist', option)

def run(self):
log.info('IMDB watchlist thread is running.')

wait = 0.1 if self.debug else 5

time.sleep(10)
while True and not self.abort:
now = time.time()

if (self.lastChecked + self.intervalSec) < now:
try:
self.running = True
self.lastChecked = now
self.doCsvCheck()
self.running = False
except:
log.error("!!Uncaught exception in IMDB Watchlist thread: %s" % traceback.format_exc())

time.sleep(wait)

def isDisabled(self):
return not self.conf('enabled')

def doCsvCheck(self):
'''
Go find movies and add them!
'''

if self.isDisabled():
log.debug('IMDB Watchlist has been disabled')
return

log.info('Starting IMDB Watchlist check')
wl = ImdbWl()
watchlist = wl.getWatchlist()
if not watchlist:
log.info("Could not get IMDB watchlist.")
return

MyMovieController = MovieController()

for movie in watchlist:
if self.abort: #this loop takes a while, stop when the program needs to close
log.info('Aborting IMDB watchlist check')
return

time.sleep(5) # give the system some slack

log.debug('Searching for movie: "%s".' % movie['title'])
result = False
try:
result = self.searcher['movie'].findByImdbId(movie['imdb'])
except Exception:
result = False
if not result:
log.info('Movie not found: "%s".' % movie['title'])
continue
try:
# Check and see if the movie is in CP already, if so, ignore it.
cpMovie = Db.query(Movie).filter_by(imdb = movie['imdb']).first()
if cpMovie:
log.info('IMDB Watchlist: Movie found in CP Database, ignore: "%s".' % movie['title'])
continue
log.info('Adding movie to queue: %s.' % movie['title'])
quality = Db.query(QualityTemplate).filter_by(name = self.config.get('Quality', 'default')).one()
MyMovieController._addMovie(result, quality.id)
except:
log.info('MovieController unable to add this movie: "%s". %s' % (movie['title'], traceback.format_exc()))

def startImdbWlCron(config, searcher, debug):
cron = ImdbWlCron()
cron.config = config
cron.searcher = searcher
cron.debug = debug
cron.start()

return cron
49 changes: 49 additions & 0 deletions app/lib/imdbwl.py
@@ -0,0 +1,49 @@
from app.config.cplog import CPLog
import cherrypy
import urllib
import csv

log = CPLog(__name__)

def conf(options):
return cherrypy.config['config'].get('IMDBWatchlist', options)

class ImdbWl:

def __init__(self):
self.enabled = conf('enabled')

def call(self, url = None):
log.debug("Call method")

if not url:
url = conf('url')

watchlist = []
try:
log.info('Retrieving IMDB Watchlist CSV from %s' % url)
urllib._urlopener = ImdbUrlOpener()
tmp_csv, headers = urllib.urlretrieve(url)
csvwl = csv.reader(open(tmp_csv, 'rb'))
for row in csvwl:
if row[0] != 'position':
#log.info('Row is %s' % row)
movie = {}
movie['imdb'] = row[1]
movie['title'] = '%s (%s)' % (row[5], row[10])

watchlist.append(movie)
except(IOError):
log.info("Failed calling downloading/parsing IMDB Watchlist CSV")
watchlist = None
return watchlist

def test(self, url):
wl = self.call('http://www.imdb.com/list/export?list_id=watchlist&author_id=ur0034213')
print wl

def getWatchlist(self):
return self.call()

class ImdbUrlOpener(urllib.FancyURLopener):
version = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11'
19 changes: 18 additions & 1 deletion app/views/config/index.html
Expand Up @@ -409,7 +409,7 @@ <h3>NZBsRus.com</h3>
</div>
</fieldset>

<fieldset class="inlineLabels">
<fieldset class="inlineLabels">
<h3>#alt.binaries.hdtv.x264@EFnet</h3>
<div class="ctrlHolder checkbox">
<label>Enable</label>
Expand Down Expand Up @@ -730,6 +730,23 @@ <h3>MovieETA</h3>
<input type="checkbox" name="MovieETA.enabled" value="True" ${' checked="checked"' if config.get('MovieETA', 'enabled') else ''} />
</div>
</fieldset>
<fieldset class="inlineLabels">
<h3>IMDB Watchlist</h3>
<div class="ctrlHolder checkbox">
<label>Enable</label>
<input type="checkbox" name="IMDBWatchlist.enabled" value="True" ${' checked="checked"' if config.get('IMDBWatchlist', 'enabled') else ''} />
<p class="formHint">
This will magically add movies to your wanted list that appear in public IMDB Watchlist specified below.
</p>
</div>
<div class="ctrlHolder">
<label>IMDB Watchlist URL</label>
<input type="text" name="IMDBWatchlist.url" value="${config.get('IMDBWatchlist', 'url')}" class="textInput large"/>
<p class="formHint">
'Export this list' URL of the public IMDB Watchlist
</p>
</div>
</fieldset>
</div>
<div class="group notifications">
<fieldset class="inlineLabels" id="xbmcFieldset">
Expand Down
4 changes: 3 additions & 1 deletion app/views/config/userscript.js
Expand Up @@ -223,7 +223,9 @@ imdb = (function(){
}

function getId(){
return 'tt' + location.href.replace(/[^\d+]+/g, '');
var regex = new RegExp(/tt(\d+)/);
var id = location.href.match(regex)[0];
return id;
}

function getYear(){
Expand Down

0 comments on commit dd5704a

Please sign in to comment.