def update_movie_list(self): """ Download the list of movies and saves them to the DB """ print "***** Starting to list movies... *****" # Don't run this if we're already done if (self.finished_listing_movies): print "All movie pages already processed... skipping..." return # Get the current last page if not already set if (self.last_page == 0): self.last_page = self.get_last_page() if (self.last_page == 0): raise Exception("Couldn't obtain a valid 'last page'!") else: print "Last Page is: %s" % self.last_page # Iterate movie pages try: url = None for this_page in xrange(self.current_page, self.last_page + 1): self.current_page = this_page print "Processing Page #%s" % this_page url = "%spage=%s" % (MEDIA_LIST_URL, this_page) self.browser.open(url) for link in self.browser.links(url_regex=MEDIA_PATTERN): if (link.text != SKIP_IMG_TEXT): # skip img links self.add_movie(link) if (self.current_page == self.last_page): self.finished_listing_movies = True except Exception, ex: new_ex = freevana.parse_exception(ex, url) if (isinstance(new_ex, freevana.FileNotFoundException)): # this one is ok to happen, we guess (for now) print new_ex else: raise new_ex # propagate the exception
def get_last_page(self): """ Get the current last page of movies """ last_page = 0 try: self.browser.open(MEDIA_LIST_URL) # find all links with urls containing 'page=' for link in self.browser.links(url_regex=r'page='): if link.text != '': try: page = int(link.text) if page > last_page: last_page = page except Exception, ex: print "Couldn't parse text: %s, Exception: %s" % ( link.text, ex) except Exception, ex: new_ex = freevana.parse_exception(ex, MEDIA_LIST_URL) if (isinstance(new_ex, freevana.FileNotFoundException)): # this one is ok to happen, we guess (for now) print new_ex else: raise new_ex # propagate the exception
raise ex # propagate the exception def download_subtitle(self, movie_id, lang): """ Download a Subtitle in a specific language """ try: url = SUBTITLES_URL_PATTERN % (movie_id, lang) self.browser.retrieve(url, filename="%s/%s_%s.srt" % ( "%s/%s" % (SUBTITLES_LOCATION, lang), movie_id, lang)) except KeyboardInterrupt, ex: print ex sys.exit(0) except Exception, ex: new_ex = freevana.parse_exception(ex, url) if (isinstance(new_ex, freevana.FileNotFoundException)): # this one is ok to happen, we guess (for now) print 'No subtitle in %s. %s' % (lang, new_ex) else: raise new_ex # propagate the exception def mark_subs_as_downloaded(self, movie_id): """ Update the DB marking the movie's subs as downloaded. """ try: query = 'UPDATE movies SET subs=1 WHERE id=%s' % movie_id self.execute_query(query) except Exception, ex: print "Couldn't mark subs as downloaded: %s" % ex