示例#1
0
def process_mentions():
    """retrieves all mentions and generates captions for those who are fighting
        fit"""
    if not cfg('twitter:user_requests:bool'):
        return

    params = dict(count=200)
    sources_whitelist = cfg('twitter:sources_whitelist:list')
    mention_prefix = '@%s ' % twitter.me.screen_name.lower()

    try:
        with open('state_mentions_timeline.txt') as fp:
            since_id = int(fp.read())
        utils.logging.info('State: since_id=%d', since_id)
        params['since_id'] = since_id
    except Exception as exc:
        utils.logging.warning("There's no last id saved, so I will save the "
                              'last id I see and then quit.')
        since_id = None

    filtered_statuses = []
    statuses = [
        status for page in tweepy.Cursor(twitter.api.mentions_timeline, **
                                         params).pages() for status in page
    ]
    # they are in reverse chronological order, so put them straight
    statuses = statuses[::-1]
    if not since_id:
        since_id = statuses[-1].id
        with open('state_mentions_timeline.txt', 'wt') as fp:
            fp.write(str(since_id))
        utils.logging.info('New since_id=%d. Goodbye!', since_id)
        return

    for status in statuses:
        # ignore mentions that are not directed at me
        if not status.text.lower().startswith(mention_prefix):
            continue

        # ignore retweets
        if hasattr(status, 'retweeted_status'):
            continue

        # if the sources whitelist is enabled, ignore those who aren't on it
        if (sources_whitelist and status.source not in sources_whitelist):
            continue

        # store this status
        filtered_statuses.append(status)

    if filtered_statuses:
        utils.logging.info('Retrieved %d new mentions (from %d to %d).',
                           len(filtered_statuses), filtered_statuses[0].id,
                           filtered_statuses[-1].id)
        with open('state_mentions_timeline.txt', 'wt') as fp:
            fp.write(str(filtered_statuses[-1].id))

        Akari.warmup()

        parallel = Parallel(process_request, filtered_statuses,
                            cfg('twitter:process_threads:int') or 3)
        parallel.start()
    else:
        utils.logging.info('Retrieved no new mentions.')