Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from lechat/master
IMDB Watchlist importer
- Loading branch information
Showing
8 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters