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 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 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 get_episode_if_input_is_ok(web, filtr, exists = [], notempty = []): if captcha_is_ok(web) and \ all([ web.input(k) is not None for k in exists ]) and \ all([ web.input(k) != "" for k in notempty ]): try: return True, Episode.find().filter(filtr).one() except Exception as e: return False, str(e) return False, None
def feed_all_comments_counts(web): episodes = Episode.find().all() comments = {} for episode in episodes: label = episode.filename.split("/").pop() comments[label] = Comment.find().filter_by(episode = episode.id).count() return template_json(web, comments)
def feed_comments_by_slug(web, site, id, mode): try: # FIXME wrap db queries into one episode = Episode.find().filter_by(link = id).one() comments = Comment.find().filter_by(episode = episode.id).\ order_by(Comment.date).all() except: return notfound("Episode not found.") return comments_per_episode(web, episode, comments, site, mode)
def comments(web, site, id, mode, errors=[]): try: # FIXME wrap db queries into one episode = Episode.find().filter_by(link = id).one() comments = Comment.find().filter_by(episode = episode.id).\ order_by(Comment.date).all() except: return notfound("Episode not found.") return template_mode(web, site, episode, mode, errors = errors, comments = comments )
def ratings(web, site, id, mode, errors=[]): try: # FIXME wrap db queries into one episode = Episode.find().filter_by(link = id).one() ratings = Rating.find().filter_by(episode = episode.id).all() except: return notfound("Episode not found.") return template_mode(web, site, episode, (mode == '/' or mode is None) and "/rating" or mode, errors = errors, hide_rating_detail = True, ratings = ratings)
def rating_by_filename(web, filename, mode): filename = "content/news/{0}.xml".format(filename) try: # FIXME wrap db queries into one episode = Episode.find().filter_by(filename = filename).one() ratings = Rating.find().filter_by(episode = episode.id).all() if "radio" in filename: site = "pentaradio" elif "cast" in filename: site = "pentacast" elif "music" in filename: site = "pentamusic" else: site = 42 / 0 except: return notfound("Episode not found.") return rating_per_episode(web, episode, ratings, site, mode)
def feed_comments_by_filename(web, filename, mode): filename = "content/news/{0}.xml".format(filename) try: # FIXME wrap db queries into one episode = Episode.find().filter_by(filename = filename).one() comments = Comment.find().filter_by(episode = episode.id).\ order_by(Comment.date).all() if "radio" in filename: site = "pentaradio" elif "cast" in filename: site = "pentacast" elif "music" in filename: site = "pentamusic" else: site = 42 / 0 except: return notfound("Episode not found.") return comments_per_episode(web, episode, comments, site, mode)
def feed_episodes(web, mode): try: episodes = Episode.find().order_by(Episode.date).all() except: return notfound("No Episodes not found.") if mode == "atom": return template_episodes_atom( title = "Pentamedia-Portal // Episodes", episodes = episodes ) elif mode == "json": return template_json(web, { "episodes": list(map(episode_to_json, episodes)) }) else: return notfound("Type not supported.")
def feed_episodes_by_category(web, site, mode): try: episodes = Episode.find().filter_by(category = site).\ order_by(Episode.date).all() except: return notfound("Category not found.") if mode == "atom": return template_episodes_atom( title = "Pentamedia-Portal // P{0} // Episodes".format(site[1:]), episodes = episodes ) elif mode == "json": return template_json(web, { "episodes": list(map(episode_to_json, episodes)) }) else: return notfound("Type not supported.")
def comments_by_filename(web, filename, mode, errors=[]): filename = "content/news/{0}.xml".format(filename) try: # FIXME wrap db queries into one episode = Episode.find().filter_by(filename = filename).one() comments = Comment.find().filter_by(episode = episode.id).\ order_by(Comment.date).all() if "radio" in filename: site = "pentaradio" elif "cast" in filename: site = "pentacast" elif "music" in filename: site = "pentamusic" else: site = 42 / 0 except Exception as e: return notfound(str(e)) return template_mode(web, site, episode, mode, errors = errors, comments = comments )
def ratings_by_filename(web, filename, mode, errors=[]): filename = "content/news/{0}.xml".format(filename) try: # FIXME wrap db queries into one episode = Episode.find().filter_by(filename = filename).one() ratings = Rating.find().filter_by(episode = episode.id).all() if "radio" in filename: site = "pentaradio" elif "cast" in filename: site = "pentacast" elif "music" in filename: site = "pentamusic" else: site = 42 / 0 except Exception as e: return notfound(str(e)) return template_mode(web, site, episode, (mode == '/' or mode is None) and "/rating" or mode, errors = errors, hide_rating_detail = True, ratings = ratings)
def start(web): # FIXME wrap db queries into one episodes = {} for category in ['pentaradio','pentamusic','pentacast']: episode = list(reversed(Episode.find().\ filter_by(category=category).\ order_by(Episode.date).\ all() )) if len(episode) > 13: episode = episode[:13] episode.append({'name': "more…", 'link': ""}) episodes[category] = episode return template("start.html", episodes = episodes, css = "start" )
def main(web, site, errors=[]): try: # FIXME wrap db queries into one episodes = Episode.find().filter_by(category=site).\ 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 ] except Exception as e: return notfound(str(e)) return template("episodes.tpl", errors = errors, css = "episode", episodepage = zip(episodes, comments_count, ratings), site = site )
def trackback_server(web, id): try: episode = Episode.find().filter_by(id = int(id)).one() except: return errör("Episode not found.") if not web.input('url'): return errör("No URL given.") spam = is_spam(web.input('url'), episode) if spam == 2: return errör("Timeout.") elif spam: return errör("Spam.") if web.input("excerpt"): text = md.convert(web.input('excerpt')) else: text = None Trackback(episode = episode.id, title = web.input('title'), text = text, url = web.input('url'), date = datetime.now(), name = web.input('blog_name') ).save() return template_tb()
def feed_comments_by_category(web, site, mode): # FIXME wrap db queries into one episodes = Episode.find().filter_by(category = site).\ order_by(Episode.date).all() episodes.reverse() comments, id_episodes = [], {} for episode in episodes: comments.extend(Comment.find().filter_by(episode = episode.id).all()) id_episodes[episode.id] = episode comments.sort(key=lambda cmnt: cmnt.date) comments.reverse() if mode == "atom": return template_atom( title = "Pentamedia-Portal // P{0} // Comments".format(site[1:]), episodes = id_episodes, comments = comments ) elif mode == "json": return template_json(web, { "comments": list(map(comment_to_json,comments)), "new_link": "/{0}/{1}/comment#new".format(episode.category,episode.link) }) else: return notfound("Type not supported.")
def feed_all_comments(web, mode): # FIXME wrap db queries into one episodes = Episode.find().all() comments = Comment.find().all() trackbacks = Trackback.find().all() if mode == "atom": entries = comments + trackbacks entries.sort(key=lambda e: e.date) entries.reverse() id_episodes = {} for episode in episodes: id_episodes[episode.id] = episode return template_atom( title = "Pentamedia-Portal // Comments", episodes = id_episodes, comments = entries ) elif mode == "json": return template_json(web, { "comments": list(map(comment_to_json, comments)), "trackbacks": list(map(trackback_to_json, trackbacks)) }) else: return notfound("Type not supported.")
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 )
def rating_by_slug(web, site, id, mode): try: # FIXME wrap db queries into one episode = Episode.find().filter_by(link = id).one() ratings = Rating.find().filter_by(episode = episode.id).all() except: return notfound("Episode not found.") return rating_per_episode(web, episode, ratings, site, mode)