Exemplo n.º 1
0
def parseTwitter(twitter_id, since_id=None, ignore_tag='@', no_trunc=False, rasterize=False):
    url = "http://api.twitter.com/1/statuses/user_timeline.json?trim_user=true&include_rts=true&include_entities=1&screen_name=%s&count=5"%twitter_id
    if since_id:
        url += "&since_id="+since_id

    proxy=config['twitter']['use_proxy'] and sync_proxy
    result = url_fetch(url,proxy=proxy)
    #result={'status_code':200,'content':'[{"id":99999999,"text":"test"}]'}
    if result['status_code'] == 200:
        content=result['content']
        tweets=json.loads(content)
        lastid=None
        for t in reversed(tweets):
            id=str(t['id'])
            rt=t.get('retweeted_status')
            text=(rt and t['truncated'] and no_trunc) and rt['text'] or t['text']
            geo=t.get('geo')
            coord=geo and geo['coordinates']
            if not (ignore_tag and text.startswith(ignore_tag)):
                logger.info('sync (%s) %s'%(id,text))
                if rasterize:
                    if send_pic_sina_msg(t):
                        lastid=id
                        time.sleep(1)
                elif send_sina_msgs(text,coord):
                    lastid=id
                    time.sleep(1)
            else:
                logger.debug('ignore (%s) %s'%(id,text))
                lastid=id
        return lastid
    else:
        logger.warning("get twitter data error: ("+str(result['status_code'])+")\n"+result['content'])
Exemplo n.º 2
0
def rasterize_msg(msg, coord, pic):
    params = {'text': msg.encode('utf-8')}
    if pic:
        params['pic'] = pic
    result = url_fetch(config['raster_url'], urllib.urlencode(params))
    return result['content']