Пример #1
0
def action(context):
    user = context['user']
    subid = context['path_parameters'].get('feed')
    artid = context['path_parameters'].get('read')
    articleUrl = filters.decode_segment(artid[4:])
    sub = feeds.redis.hgetall(subid)
    art = feeds.redis.hgetall(artid)
    if not art:
        return context['request'].get_response(webob.exc.HTTPNotFound())
    art['subid'] = subid
    art['artid'] = artid
    art['feedName'] = sub['feedName'] if sub else ''
    art['articleDate'] = str(datetime.datetime.utcfromtimestamp(float(art['date'])))
    art['articleUrl'] = articleUrl
    articleGuid = art.get('guid') if art else None
    context['content'] = feeds.get_article_content(articleUrl, articleGuid, sub, [])
    context['article'] = feeds.makeUnicode(art)

    # prefetch the next article
    if not context['parameters'].get('prefetch'):
        params = dict()
        params['feed'] = context['parameters'].get('feed', '')
        params['show'] = context['parameters'].get('show', '')
        params['date'] = art['date']
        params['skip::'] = 1
        params['prefetch'] = 1
        cookie = context['request'].headers.get('Cookie', '')
        fetch = threading.Thread(target=prefetch, args=(params, cookie))
        fetch.daemon = True
        fetch.start()
Пример #2
0
def action(context):
    feeds.update_user_maybe(context["user"])

    # LATER: if empty redirect to welcome

    action = None
    subidLast = None
    artidLast = None
    if context["request"].method == "POST":
        for name in context["parameters"].keys():
            if (
                name.startswith("hide:")
                or name.startswith("next:")
                or name.startswith("show:")
                or name.startswith("skip:")
            ):
                parts = name.split(":")
                action = parts[0]
                subidLast = parts[1]
                artidLast = parts[2]
                break

    if action == "hide" or action == "next":
        scoreLast = feeds.redis.zscore(subidLast + "/unread", artidLast)
        if scoreLast:
            feeds.redis.zrem(subidLast + "/unread", artidLast)
            feeds.redis.zadd(subidLast + "/read", scoreLast, artidLast)
    elif action == "show":
        scoreLast = feeds.redis.zscore(subidLast + "/read", artidLast)
        if scoreLast:
            feeds.redis.zrem(subidLast + "/read", artidLast)
            feeds.redis.zadd(subidLast + "/unread", scoreLast, artidLast)

    feedFilter = context["parameters"].get("feed")
    showFilter = context["parameters"].get("show")

    ids = []
    feeders = []
    now = time.time()
    cutoff = now - (60 * 24 * 60 * 60)
    for subid in feeds.redis.sort(context["user"] + "/subs", None, None, "*->feedName", None, False, True):
        feedName = feeds.redis.hget(subid, "feedName")
        count = 0
        if feedFilter and feedFilter != subid:
            count = feeds.redis.zcard(subid + "/unread")
        else:
            before = len(ids)
            ids.extend(
                [
                    (subid, feedName, artid, True, score)
                    for artid, score in feeds.redis.zrangebyscore(subid + "/unread", "-inf", "+inf", None, None, True)
                ]
            )
            count = len(ids) - before
        feeders.append({"subid": subid, "feedName": feedName, "counter": count})
        feeds.redis.zremrangebyscore(subid + "/read", "-inf", cutoff)
        if showFilter == "all" and (feedFilter == subid or not feedFilter):
            ids.extend(
                [
                    (subid, feedName, artid, False, score)
                    for artid, score in feeds.redis.zrangebyscore(subid + "/read", "-inf", "+inf", None, None, True)
                ]
            )

    # sort by date descending
    ids.sort(filters.compare, lambda x: -float(x[4]))

    qs = ""
    if feedFilter:
        qs = "?feed=" + urllib.quote_plus(feedFilter)
    if showFilter:
        qs += "&" if qs else "?"
        qs += "show=" + urllib.quote_plus(showFilter)
    if context["parameters"].get("prefetch"):
        qs += "&" if qs else "?"
        qs += "prefetch=1"

    if action == "next" or action == "skip":
        scoreLast = float(context["parameters"].get("date", 0))
        subidNext = None
        artidNext = None
        for tup in ids:
            if tup[4] <= scoreLast:
                break
            subidNext = tup[0]
            artidNext = tup[2]
        if subidNext and artidNext:
            path = (
                context["root"]
                + "/feed/"
                + filters.encode_segment(subidNext)
                + "/read/"
                + filters.encode_segment(artidNext)
                + "/"
                + qs
            )
            return context["request"].get_response(webob.exc.HTTPFound(location=path))

    offset = 0
    try:
        offset = int(context["parameters"].get("offset", 0))
    except:
        pass
    if offset > 0:
        context["newer"] = qs + ("&" if qs else "?") + "offset=" + str(offset - 50)
    if len(ids) - offset > 50:
        context["older"] = qs + ("&" if qs else "?") + "offset=" + str(offset + 50)
    oldest = len(ids) - 50
    if oldest > 0 and oldest - offset > 50:
        context["oldest"] = qs + ("&" if qs else "?") + "offset=" + str(oldest)

    articles = []
    for tup in ids[offset : offset + 50]:
        art = feeds.redis.hgetall(tup[2])
        art["subid"] = tup[0]
        art["feedName"] = tup[1]
        art["artid"] = tup[2]
        art["unread"] = tup[3]
        art["articleDate"] = str(datetime.datetime.utcfromtimestamp(float(art["date"])))
        feeds.makeUnicode(art)
        articles.append(art)

    context["feeds"] = feeders
    context["articles"] = articles
    context["qs"] = qs