Esempio n. 1
0
def view_tweets_about_keyword(keyword):
    period, start, end, cache_time = get_period(request, "week")
    params = {
        "start": start.strftime(time_format),
        "end": end.strftime(time_format)
    }
    keyword_data = cache(process_details,
                         keyword,
                         params,
                         cache_time=cache_time,
                         path=get_req_path(request))
    if isinstance(keyword_data, Response):
        return keyword_data

    num_tweets = keyword_data["num_tweets"]
    tweets = keyword_data["tweets"]
    retweets = keyword_data["retweets"]

    template_data = {
        "keyword": keyword,
        "num_tweets": num_tweets,
        "num_unique_tweets": len(tweets),
        "tweets": tweets,
        "retweets": retweets,
        "period": period,
        "start": display_datetime(start),
        "end": display_datetime(end)
    }
    return render_template("tweets.html",
                           title=make_title(keyword),
                           **template_data)
Esempio n. 2
0
def view_token_co_occurrences(keyword):
    period, start, end, cache_time = get_period(request, "week")
    params = {
        "start": start.strftime(time_format),
        "end": end.strftime(time_format)
    }
    keyword_data = cache(process_tokens,
                         keyword,
                         params,
                         cache_time=cache_time,
                         path=get_req_path(request))
    if isinstance(keyword_data, Response):
        return keyword_data

    occurrences = []
    for k in keyword_data["occurrences"]:
        if k["text"] != keyword:
            k["pos"] = parse_pos(k["pos"].split("(")[0])
            occurrences.append(k)

    nums = range(1, len(occurrences) + 1)
    template_data = {
        "keyword": keyword,
        "period": period,
        "start": display_datetime(start),
        "end": display_datetime(end),
        "occurrences": zip(nums, occurrences)
    }
    return render_template("occurrences.html",
                           title=make_title(keyword),
                           **template_data)
Esempio n. 3
0
def view_anomalies(group):
    keywords = cache(tweety.get_group,
                     group,
                     cache_time=60 * 60,
                     path=get_req_path(request))
    if isinstance(keywords, Response):
        return keywords

    keywords = [k["lemma"] for k in keywords]
    anomalies = json.loads(redis.get("anomalies"))
    anomalies = [a for a in anomalies if a[0] in keywords]
    start = datetime.strptime(json.loads(redis.get("anomalies_start")),
                              time_format)
    end = datetime.strptime(json.loads(redis.get("anomalies_end")),
                            time_format)

    template_data = {
        "peaks": anomalies,
        "start": display_datetime(start),
        "end": display_datetime(end),
        "num_peaks": len(anomalies)
    }
    return render_template("anomaly.html",
                           title=make_title("Piekdetectie"),
                           **template_data)
Esempio n. 4
0
def view_news(keyword):
    period, start, end, cache_time = get_period(request, "week")
    news_data = cache(process_news,
                      keyword,
                      start,
                      end,
                      cache_time=cache_time,
                      path=get_req_path(request))
    if isinstance(news_data, Response):
        return news_data

    period_name = {
        "day": "dag",
        "week": "week",
        "month": "maand"
    }.get(period, "dag")
    news = []
    for item in news_data:
        item["pubdate"] = display_datetime(item["pubdate"])
        del item["nid"]
        news.append(item)

    template_data = {
        "keyword": keyword,
        "start": display_datetime(start),
        "end": display_datetime(end),
        "period": period,
        "period_name": period_name,
        "news": news
    }
    return render_template("news.html",
                           title=make_title(keyword),
                           **template_data)
Esempio n. 5
0
def top(group):
    max_amount = request.args.get("k", 10, type=int)
    period, start, end, cache_time = get_period(request, "day")
    params = {
        "start": start.strftime(time_format),
        "end": end.strftime(time_format),
        "group": group
    }
    data = cache(process_top,
                 group,
                 max_amount,
                 params,
                 cache_time=cache_time,
                 path=get_req_path(request))

    if isinstance(data, Response):
        return data

    if len(data) < max_amount:
        max_amount = len(data)

    template_data = {
        "data": data,
        "group": group,
        "disp_group": display_group(group),
        "max_amount": str(max_amount),
        "period": period,
        "start": display_datetime(start),
        "end": display_datetime(end),
        "title": make_title("Top {} {}".format(max_amount,
                                               display_group(group)))
    }
    return render_template("top.html", **template_data)
Esempio n. 6
0
def view_group(group):
    period, start, end, cache_time = get_period(request, "day")
    params = {
        "start": start.strftime(time_format),
        "end": end.strftime(time_format),
        "group": group
    }
    keywords = cache(tweety.get_keywords,
                     cache_time=cache_time,
                     path=get_req_path(request),
                     **params)
    if isinstance(keywords, Response):
        return keywords
    total = sum([entry["count"] for entry in keywords])
    for keyword in keywords:
        keyword["percentage"] = "{:.2f}".format(keyword["count"] / total * 100)
        keyword["count"] = display_number(keyword["count"])
    nums = range(1, len(keywords) + 1)
    template_data = {
        "nums_keywords": zip(nums, keywords),
        "group": group,
        "disp_group": display_group(group),
        "nums": nums,
        "total": display_number(total),
        "period": period,
        "start": display_datetime(start),
        "end": display_datetime(end)
    }
    return render_template("group.html",
                           title=make_title(template_data["disp_group"]),
                           **template_data)
Esempio n. 7
0
def home():
    sync_time = redis.get("sync_time")
    if sync_time:
        sync_time = sync_time.decode("utf-8")

    max_amount = request.args.get("k", 10, type=int)
    period, start, end, cache_time = get_period(request, "day")
    params = {
        "start": start.strftime(time_format),
        "end": end.strftime(time_format),
        "group": "bloemen"
    }
    bloemen = cache(process_top,
                    "bloemen",
                    max_amount,
                    params,
                    cache_time=cache_time,
                    path=get_req_path(request))
    params["group"] = "groente_en_fruit"
    groente_en_fruit = cache(process_top,
                             "groente_en_fruit",
                             max_amount,
                             params,
                             cache_time=cache_time,
                             path=get_req_path(request))

    if isinstance(bloemen, Response):
        return bloemen
    if isinstance(groente_en_fruit, Response):
        return groente_en_fruit

    template_data = {
        "bloemen": bloemen,
        "groente_en_fruit": groente_en_fruit,
        "sync_time": sync_time,
        "start": display_datetime(start),
        "end": display_datetime(end),
        "period": period
    }
    return render_template("home.html",
                           title=make_title("BigTU research project"),
                           **template_data)
Esempio n. 8
0
def display_article_metadata(_article, sep='<br/>'):
    dates_labels = ['collection', 'aop', 'epub', 'epub-ppub']
    dates = [_article.collection_dateiso, _article.ahpdate_dateiso, _article.epub_dateiso, _article.epub_ppub_dateiso]

    r = ''
    r += html_reports.tag('p', html_reports.tag('strong', _article.xml_name), 'doi')
    r += html_reports.tag('p', _article.publisher_article_id, 'doi')
    r += html_reports.tag('p', html_reports.tag('strong', _article.order), 'fpage')
    for l, d in zip(dates_labels, dates):
        if d is not None:
            r += html_reports.display_label_value(_('date') + ' (' + l + ')', utils.display_datetime(d), 'p')
    r += html_reports.tag('p', html_reports.tag('strong', _article.title), 'article-title')
    r += html_reports.tag('p', display_authors(_article.article_contrib_items, sep))
    return r
Esempio n. 9
0
def home_page():
    '''
    Shows how many people are currently inside the building and how much traffic there is (for the day)
    '''
    db = dbh.DB_Handler(constants.DB_FILE)
    record = db.get_record(utils.iso_date())
    if not record:
        return render_template('dne.html', date=utils.display_date())

    data = {
        'datetime': utils.display_datetime(),
        'inside': record['ingress'] - record['egress'],
        'entrants': max(record['ingress'], record['egress'])
    }
    return render_template('home.html', data=data, base_url=request.url_root)
Esempio n. 10
0
def view_stories(group):
    period, start, end, cache_time = get_period(request, "week")
    params = {
        "start": start.strftime(time_format),
        "end": end.strftime(time_format)
    }
    story_data = cache(process_stories,
                       group,
                       params,
                       cache_time=cache_time,
                       path=get_req_path(request))

    if isinstance(story_data, Response):
        return story_data

    active_stories, closed_stories = story_data

    storify_data = []
    timeline_data = []

    timeline_start = timegm(start.timetuple()) * 1000
    timeline_end = timegm(end.timetuple()) * 1000

    display_tweets = 11
    display_active_stories = 10
    display_closed_stories = 5

    for story in active_stories:
        if not (len(storify_data) < display_active_stories):
            break
        story = filter_story(story, display_tweets)
        timeline_info = {
            "label": len(storify_data),
            "times": story["cluster_details"]
        }
        del story["cluster_details"]

        storify_data.append(story)
        timeline_data.append(timeline_info)

    for story in closed_stories:
        if not (len(storify_data) <
                display_active_stories + display_closed_stories):
            break
        story = filter_story(story, display_tweets)
        timeline_info = {
            "label": len(storify_data),
            "times": story["cluster_details"]
        }
        del story["cluster_details"]

        storify_data.append(story)
        timeline_data.append(timeline_info)

    template_data = {
        "group":
        display_group(group),
        "storify_data":
        json.dumps(storify_data),
        "timeline_data":
        json.dumps(timeline_data),
        "timeline_start_ts":
        timeline_start,
        "timeline_end_ts":
        timeline_end,
        "display_tweets":
        display_tweets,
        "num_stories":
        min(display_active_stories + display_closed_stories,
            len(storify_data)),
        "start":
        display_datetime(start),
        "end":
        display_datetime(end),
        "period":
        period
    }
    return render_template("storify.html",
                           title=make_title(group),
                           **template_data)
Esempio n. 11
0
def view_keyword(keyword):
    deluxe = is_deluxe(
        current_user
    )  # users in the "deluxe" group can specify their own time period

    period, start, end, cache_time = get_period(request, "week")
    if period == "custom":
        if not deluxe:
            flash(
                "Deze functionaliteit is alleen beschikbaar voor goedgekeurde gebruikers.",
                "error")
            return redirect(url_for("horti.home"))
        if (end - start).days > 31:
            flash("Periode langer dan een maand is niet toegestaan", "error")
            return redirect(url_for("horti.home"))
        if start > end:
            flash("De einddatum moet na de begindatum zijn.", "error")
            return redirect(url_for("horti.home"))
    params = {
        "start": start.strftime(time_format),
        "end": end.strftime(time_format)
    }
    keyword_data = cache(process_details,
                         keyword,
                         params,
                         cache_time=cache_time,
                         path=get_req_path(request))
    if isinstance(keyword_data, Response):
        return keyword_data

    urls = keyword_data["URLs"][:16]
    for url in urls:
        url["display_url"] = shorten(url["link"], 80)
    del keyword_data["URLs"]

    keyword_data["tagCloud"] = keyword_data["tagCloud"][:200]

    photos = enumerate(keyword_data["photos"]
                       )  # number of photo's is limited in processing.py
    del keyword_data["photos"]

    display_tweets = 11
    max_tweets = 200
    keyword_data["tweets"] = keyword_data["tweets"][:max_tweets]
    keyword_data["retweets"] = keyword_data["retweets"][:display_tweets]
    keyword_data["interaction_tweets"] = keyword_data[
        "interaction_tweets"][:max_tweets]

    num_tweets = keyword_data["num_tweets"]
    del keyword_data["num_tweets"]

    graph = keyword_data["graph"]
    del keyword_data["graph"]

    polarity = keyword_data["polarity"]
    del keyword_data["polarity"]

    polarity_face = display_polarity(polarity)

    gtrends_period = {
        "day": "now 1-d",
        "week": "now 7-d",
        "month": "today 1-m"
    }.get(period, "now 1-d")
    period_name = {
        "day": "dag",
        "week": "week",
        "month": "maand"
    }.get(period, "dag")

    news = []
    for item in keyword_data["news"]:
        item["pubdate"] = display_datetime(item["pubdate"])
        del item["nid"]
        news.append(item)
    del keyword_data["news"]

    template_data = {
        "keyword": keyword,
        "keyword_data": json.dumps(keyword_data),
        "deluxe": deluxe,
        "num_tweets": display_number(num_tweets),
        "urls": urls,
        "graph": json.dumps(graph),
        "photos": photos,
        "display_tweets": display_tweets,
        "start": display_datetime(start),
        "end": display_datetime(end),
        "period": period,
        "period_name": period_name,
        "polarity": polarity,
        "polarity_face": polarity_face,
        "gtrends_period": gtrends_period,
        "news": news
    }
    return render_template("keyword.html",
                           title=make_title(keyword),
                           **template_data)