def save_episode(self, episode): ''' If the object has `show_id`, `number` and `path` is filled the episode will be saved. :param episode: `Parsed_episode()` ''' updated = 0 with new_session() as session: if not episode.show_id or not episode.number: return False ep = session.query( models.Episode, ).filter( models.Episode.show_id == episode.show_id, models.Episode.number == episode.number, ).first() modified_time = self.get_file_modified_time(episode.path) if ep and ep.modified_time == modified_time: return metadata = self.get_metadata(episode.path) e = models.Episode( show_id=episode.show_id, number=episode.number, path=episode.path, meta_data=metadata, modified_time=modified_time, ) session.merge(e) session.commit() return True
def delete_item(self, episode): ''' :param episode: `Parsed_episode()` :returns: bool ''' if not episode.show_id: if not self.episode_show_id_lookup(episode): return False if not episode.number: if not self.episode_number_lookup(episode): return False with new_session() as session: ep = session.query( models.Episode, ).filter( models.Episode.show_id == episode.show_id, models.Episode.number == episode.number, ).first() if ep: session.delete(ep) session.commit() logging.info('Deleted episode: {} {}'.format( episode.file_show_title, episode.number )) return True return False
def post(self): set_default_headers(self) with new_session() as session: with session.no_autoflush: show = session.query( models.Show_id_lookup, ).filter( models.Show_id_lookup.file_show_title == \ self.get_argument('file_show_title') ).first() if not show: raise tornado.web.HTTPError(404, 'show not found') old_show_id = show.show_id show.show_id = self.get_argument('show_id') show.show_title = self.get_argument('show_title') if old_show_id: count = session.query(models.Show_id_lookup, ).filter( models.Show_id_lookup.show_id == old_show_id).count() if count < 2: session.query(models.Episode_number_lookup, ).filter( models.Episode_number_lookup.show_id == old_show_id, ).update({ 'show_id': show.show_id, }) session.query(models.Episode, ).filter( models.Episode.show_id == old_show_id, ).update({ 'show_id': show.show_id, }) session.commit() self.write('{}')
def post(self): set_default_headers(self) with new_session() as session: show = session.query( models.Show_id_lookup, ).filter( models.Show_id_lookup.file_show_title == \ self.get_argument('file_show_title') ).first() if not show: raise tornado.web.HTTPError(404, 'show not found') old_show_id = show.show_id show.show_id = self.get_argument('show_id') show.show_title = self.get_argument('show_title') if old_show_id: session.query( models.Episode_number_lookup, ).filter( models.Episode_number_lookup.show_id == old_show_id, ).update({ 'show_id': show.id, }) session.query( models.Episode, ).filter( models.Episode.show_id == old_show_id, ).update({ 'show_id': show.id, }) session.commit() self.write('{}')
def get_metadata(play_id): data = decode_play_id(play_id) with new_session() as session: episode = session.query(models.Episode.meta_data, ).filter( models.Episode.show_id == data['show_id'], models.Episode.number == data['number'], ).first() return episode.meta_data if episode else None
def get(self): with new_session() as session: shows = session.query(models.Show_id_lookup).order_by( desc(models.Show_id_lookup.updated), ).all() self.render('play_shows.html', shows=shows, config=config, url_escape=tornado.escape.url_escape)
def get_metadata(play_id): data = decode_play_id(play_id) with new_session() as session: episode = session.query( models.Episode.meta_data, ).filter( models.Episode.show_id == data['show_id'], models.Episode.number == data['number'], ).first() return episode.meta_data if episode else None
def get_episode(play_id): play_info = decode_play_id(play_id, ) with new_session() as session: episode = session.query( models.Episode.path, models.Episode.meta_data, ).filter( models.Episode.show_id == play_info['show_id'], models.Episode.number == play_info['number'], ).first() return episode
def db_lookup(self, episode): with new_session() as session: value = self.get_lookup_value(episode) e = session.query( models.Episode_number_lookup.number ).filter( models.Episode_number_lookup.show_id == episode.show_id, models.Episode_number_lookup.lookup_type == episode.lookup_type, models.Episode_number_lookup.lookup_value == value, ).first() if not e: return return e.number
def get_episode(play_id): play_info = decode_play_id( play_id, ) with new_session() as session: episode = session.query( models.Episode.path, models.Episode.meta_data, ).filter( models.Episode.show_id == play_info['show_id'], models.Episode.number == play_info['number'], ).first() return episode
def get(self): with new_session() as session: shows = session.query( models.Show_id_lookup ).order_by( desc(models.Show_id_lookup.updated), ).all() self.render( 'play_shows.html', shows=shows, config=config, url_escape=tornado.escape.url_escape )
def cleanup_episodes(): with new_session() as session: logging.info('Episode cleanup started') episodes = session.query(models.Episode).all() deleted_count = 0 for e in episodes: if os.path.exists(e.path): continue deleted_count += 1 session.delete(e) session.commit() logging.info('{} episodes was deleted from the database'.format( deleted_count ))
def db_lookup(self, file_show_title): ''' :param file_show_title: str :returns: int ''' with new_session() as session: show = session.query( models.Show_id_lookup ).filter( models.Show_id_lookup.file_show_title == file_show_title, ).first() if not show or not show.show_id: return return show.show_id
def lookup(self, episode): if not episode.show_id: raise Exception('show_id must be defined in the episode object') if isinstance(episode, Parsed_episode_number): return episode.number number = self.db_lookup(episode) if number: return number number = self.web_lookup(episode) if not number: return with new_session() as session: e = models.Episode_number_lookup( show_id=episode.show_id, lookup_type=episode.lookup_type, lookup_value=self.get_lookup_value(episode), number=number, ) session.add(e) session.commit() return number
def lookup(self, file_show_title): ''' Tries to find the show on SEPLIS by it's title. :param file_show_title: str :returns: int ''' show_id = self.db_lookup(file_show_title) if show_id: return show_id shows = self.web_lookup(file_show_title) show_id = shows[0]['id'] if shows else None show_title = shows[0]['title'] if shows else None with new_session() as session: show = models.Show_id_lookup( file_show_title=file_show_title, show_title=show_title, show_id=show_id, updated=datetime.utcnow(), ) session.merge(show) session.commit() return show_id
def save_item(self, episode): ''' :param episode: `Parsed_episode()` :returns: bool ''' if not episode.show_id: if not self.episode_show_id_lookup(episode): return False if not episode.number: if not self.episode_number_lookup(episode): return False with new_session() as session: ep = session.query( models.Episode, ).filter( models.Episode.show_id == episode.show_id, models.Episode.number == episode.number, ).first() modified_time = self.get_file_modified_time(episode.path) if ep and (ep.modified_time == modified_time) and \ (ep.path == episode.path) and ep.meta_data: return metadata = self.get_metadata(episode.path) e = models.Episode( show_id=episode.show_id, number=episode.number, path=episode.path, meta_data=metadata, modified_time=modified_time, ) session.merge(e) session.commit() logging.info('Saved episode: {} {}'.format( episode.file_show_title, episode.number )) return True