def save_in_database(filename, data, tracker): olds = Episode.find().filter_by(filename = filename).all() for key in ['files', 'links', 'previews']: if key not in data: data[key] = [] for old in olds: try: Link.find().filter_by(episode = old.id).delete() File.find().filter_by(episode = old.id).delete() Preview.find().filter_by(episode = old.id).delete() except Exception as e: print(style.red+"errör 1:"+style.default,e) updated = len(olds) if olds: episode = Episode.find().filter_by(id = olds.pop(0).id) episode.update(data['episode']) episode = episode.one() else: episode = Episode(filename=filename, **data['episode']) episode.save() for old in olds: try: Comment.find().filter_by(episode=old.id).update({'episode':episode.id}) Rating.find().filter_by(episode=old.id).update({'episode':episode.id}) Episode.find().filter_by(id = old.id).delete() except Exception as e: print(style.red+"errör 2:"+style.default,e) if updated: print(style.green+"* update db: ",filename,style.default) else: print(style.green+"* add to db: ",filename,style.default) list(map(lambda kwargs: File(episode=episode.id, **kwargs).add(), data['files'])) list(map(lambda kwargs: Preview(episode=episode.id, **kwargs).add(), data['previews'])) list(map(lambda kwargs: Link(episode=episode.id, **kwargs).add(), data['links'])) tracker.check_all(filename, episode, data['links']) session().commit()
def episode(web, site, id, mode, errors=[]): try: # FIXME wrap db queries into one episode = Episode.find().filter_by(link = id).one() files = File.find().filter_by(episode = episode.id).all() links = Link.find().filter_by(episode = episode.id).all() comments = Comment.find().filter_by(episode = episode.id).\ order_by(Comment.date).all() ratings = Rating.find().filter_by(episode = episode.id).all() trackbacks = Trackback.find().filter_by(episode = episode.id).\ order_by(Trackback.date).all() except Exception as e: return notfound(str(e)) if mode is None: mode = "" if len(mode): mode = mode[1:] opts = {} opts.update(create_session(web, mode)) opts.update(do_the_comments(web, mode, comments)) opts.update(do_the_ratings(web, mode, ratings)) return template("episode.tpl", errors = errors, css = "episode", episode = episode, episodes = {episode.id: episode}, site = site, trackbacks = trackbacks, files = files, links = links, csss = [ "../lib/audio-js/audio-js", "../lib/audio-js/skins/vim", "vim"], **opts )
def datenspuren(web, errors=[]): try: # FIXME wrap db queries into one episodes = Episode.find().filter(Episode.category.startswith("ds")).\ order_by(Episode.date).all() episodes.reverse() comments_count, ratings = [], [] for episode in episodes: ids = list(map(lambda e:e.id, Episode.find(Episode.id).\ filter_by(category = "file/{0}/{1}".\ format(episode.category, episode.link)).all())) f_rts = Rating.find().filter(Rating.episode.in_(ids)).all() e_rts = Rating.find().filter_by(episode = episode.id).all() ratings += [ do_the_ratings(web, "", e_rts + f_rts)['rating'] ] comments_count += [ Comment.find().filter(Comment.episode.in_(ids)).count() + Comment.find().filter_by( episode = episode.id).count() ] count = File.find().filter_by(episode = episode.id).count() episode.filescount = "// {0} File{1}".format(count, count != 1 and "s" or "") except Exception as e: return notfound(str(e)) return template("episodes.tpl", errors = errors, css = "episode", episodepage = zip(episodes, comments_count, ratings), site = "datenspuren" )
def datenspur(web, id, mode, errors=[]): try: # FIXME wrap db queries into one episodes = Episode.find().filter(Episode.category.endswith(id)).\ order_by(Episode.date).all() episodes.reverse() comments_count = [ Comment.find().filter_by(episode = e.id).count() for e in episodes ] ratings = [ do_the_ratings(web, "", Rating.find().\ filter_by(episode = e.id).all())['rating'] for e in episodes ] episode = Episode.find().filter_by(link = id).one() for ep in episodes: ep.has_screen = True ep.files = File.find().filter_by(episode = ep.id).all() ep.preview = get_preview(Preview.find().\ filter_by(episode = episode.id).all(), ep.files) comments = Comment.find().filter_by(episode = episode.id).all() rating = Rating.find().filter_by(episode = episode.id).all() except Exception as e: return notfound(str(e)) if mode is None: mode = "" if len(mode): mode = mode[1:] opts = {} opts.update(create_session(web, mode)) opts.update(do_the_comments(web, mode, comments)) opts.update(do_the_ratings(web, mode, rating)) return template("datenspuren.tpl", errors = errors, css = "episode", episodepage = zip(episodes, comments_count, ratings), site = "datenspuren", full_site = "datenspuren/" + id, episode = episode, **opts )
def save_recording_in_database(filename, data, tracker, debug=False): # datastructure: # xml-file as Episode (category='ds*') with files from resource-tags # files from resource-tags as Episodes (category='file/ds*/:link') # with self and alternatives as files dsfiles = deepcopy(data['files']) for f in data['files']: f.pop('alternatives') try: olds = Episode.find().filter_by(filename = filename).all() old_files = list(map(lambda f: f.link, sum(map(lambda o: File.find().\ filter_by(episode = o.id).all(), olds), []))) except Exception as e: print(style.red+"errör 0:"+style.default,e) save_in_database(filename, data, tracker) for dsfile in dsfiles: dsdata = deepcopy(data) alternatives = dsfile.pop('alternatives') dsdata['files'] = [dsfile] + alternatives dsepisode = dsdata['episode'] dsepisode['name'] = dsfile['name'] dsepisode['long'] = dsfile['info'] dsepisode['link'] = ospath.basename(dsfile['link']).\ replace(" ", "_").replace("%20", "_") dsepisode['short'] = dsfile['type'] dsepisode['category'] = "file/{0}/{1}".\ format(data['episode']['category'], data['episode']['link']) save_in_database(filename + dsfile['link'], dsdata, tracker) if dsfile['link'] in old_files: old_files.remove(dsfile['link']) for alternative in alternatives: if alternative['link'] in old_files: old_files.remove(alternative['link']) if old_files: episode = Episode.find().filter_by(filename = filename).one() for old_file in old_files: print(style.red + style.bold + "* found dangling file (link: {0})".\ format(old_file), "move comments and rating to", "[id:{0}, link:{1}, name:{2}]".\ format(episode.id,episode.link,episode.name), style.default) try: olds = Episode.find().filter_by(filename = filename + old_file).\ filter(Episode.category.startswith("file/")).all() if debug: print("* found",len(olds),"episodes to delete") for old in olds: Comment.find().filter_by(episode=old.id).update({'episode':episode.id}) Rating.find().filter_by(episode=old.id).update({'episode':episode.id}) print("* deleted {0} episodes".format( Episode.find().filter_by(filename = filename + old_file).delete())) except Exception as e: print(style.red+"errör 3:"+style.default,e) session().commit()
def datenspur_file(web, id, filename, mode, errors=[]): try: # FIXME wrap db queries into one episode = Episode.find().filter_by(link = filename).\ filter(Episode.category.endswith(id)).\ order_by(Episode.date).one() comments = Comment.find().filter_by(episode = episode.id).all() files = File.find().filter_by(episode = episode.id).all() ratings = Rating.find().filter_by(episode = episode.id).all() previews = Preview.find().filter_by(episode = episode.id).all() trackbacks = Trackback.find().filter_by(episode = episode.id).\ order_by(Trackback.date).all() except Exception as e: return notfound(str(e)) episode.files = files if mode is None: mode = "" if len(mode): mode = mode[1:] opts = {} opts.update(create_session(web, mode)) opts.update(do_the_comments(web, mode, comments)) opts.update(do_the_ratings(web, mode, ratings)) preview = get_preview(previews, files) csss = [ "../lib/video-js/video-js", "../lib/video-js/skins/vim", "vim"] if episode.isaudio(): csss = list(map(lambda s:s.replace("video", "audio"), csss)) return template("datenspur.tpl", errors = errors, css = "episode", episode = episode, episodes = {episode.id: episode}, site = "datenspuren", full_site = "datenspuren/" + id, trackbacks = trackbacks, files = files, preview = preview, csss = csss, **opts )