AqoursTwitterStream.py 文件源码

python
阅读 15 收藏 0 点赞 0 评论 0

项目:AqoursTwitterStream 作者: MagaFun 项目源码 文件源码
def main():
    with open('output.log', 'a') as f:
        # api.GetStreamFilter will return a generator that yields one status
        # message (i.e., Tweet) at a time as a JSON dictionary.
        for line in api.GetStreamFilter(follow=AQOURS):

            if line.has_key('delete') == True: # If deleting a tweet
                bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>???????</b>', parse_mode="HTML")
                bot.send_message(chat_id=cfg.CHAT_ID, text=u'?????? ID ??' + '<b>' + line['delete']['status']['id_str'] + '</b>', parse_mode="HTML")

            if line.has_key('delete') == False: # If not deleting a tweet, an update can be confirmed 
                if line['user']['id_str'] in AQOURS:
                    print u'???? Aqours ??????'
                    f.write(json.dumps(line)) # log output
                    f.write('\n')
                    bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>????????</b><a href="https://twitter.com/{0}">{1}</a><b>??????</b>'.format(line['user']['screen_name'], line['user']['name']), parse_mode="HTML", disable_web_page_preview=True)

                    twitterUrl = 'https://twitter.com/{0}/status/{1}'.format(line['user']['screen_name'], line['id_str'])
                    button_list = [
                                [InlineKeyboardButton(u"???", url=twitterUrl)]
                                ]
                    reply_markup = InlineKeyboardMarkup(button_list)

                    if line['is_quote_status'] == True: 
                        print u'???? Aqours ????????'
                        if line['quoted_status']['truncated'] == False: # ??????????
                            print u'????????'
                            r = bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>?</b><a href="https://twitter.com/{0}">{1}</a><b>???????????</b>\n{2}'.format(line['quoted_status']['user']['screen_name'], line['quoted_status']['user']['name'], line['quoted_status']['text']), parse_mode="HTML", disable_web_page_preview=True)
                            getMediaForQuotedTweet(line, r)
                            bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>??????</b>\n' + line['text'], parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
                        if line['quoted_status']['truncated'] == True: # ??????????
                            print u'????????'
                            r = bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>?</b><a href="https://twitter.com/{0}">{1}</a><b>???????????</b>\n{2}'.format(line['quoted_status']['user']['screen_name'], line['quoted_status']['user']['name'], line['quoted_status']['extended_tweet']['full_text']), parse_mode="HTML", disable_web_page_preview=True)
                            getMediaForQuotedLongTweet(line, r)
                            bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>??????</b>\n' + line['text'], parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)

                    if line.has_key('retweeted_status') == True and line['is_quote_status'] == False: 
                        print u'???? Aqours ???????'
                        if line['retweeted_status']['truncated'] == False: # ???????????
                            r = bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>?</b><a href="https://twitter.com/{0}">{1}</a><b>????????</b>\n{2}'.format(line['retweeted_status']['user']['screen_name'], line['retweeted_status']['user']['name'], line['retweeted_status']['text']), parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
                            getMediaForRetweet(line, r)
                        if line['retweeted_status']['truncated'] == True: # ??????????????????
                            r = bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>?</b><a href="https://twitter.com/{0}">{1}</a><b>????????</b>\n{2}'.format(line['retweeted_status']['user']['screen_name'], line['retweeted_status']['user']['name'], line['retweeted_status']['extended_tweet']['full_text']), parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
                            getMediaForRetweetLongTweet(line, r)

                    if line['in_reply_to_status_id'] != None:
                        print u'???????' 
                        r = bot.send_message(chat_id=cfg.CHAT_ID, text=u'<a href="https://twitter.com/{0}">{1}</a><b>???</b><a href="https://twitter.com/{2}">{3}</a>'.format(line['user']['screen_name'], line['user']['name'], api.GetUser(user_id=line['in_reply_to_user_id_str']).screen_name, api.GetUser(user_id=line['in_reply_to_user_id_str']).name), parse_mode="HTML", disable_web_page_preview=True)
                        bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>???</b>\n{0}\n<b>??????</b>\n{1}'.format(line['text'], api.GetStatus(line['in_reply_to_status_id_str']).text), parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
                        getMediaForRepling(line, r)

                    if line['truncated'] == True: 
                        print u'???????'
                        r = bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>???????</b>\n' + line['extended_tweet']['full_text'], parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
                        getMediaForLongTweet(line, r)

                    elif line['in_reply_to_status_id'] == None and line.has_key('retweeted_status') == False and line['is_quote_status'] == False and line['truncated'] == False: # ??????????????????????????
                        print u'????????'
                        r = bot.send_message(chat_id=cfg.CHAT_ID, text=u'<b>???????</b>\n' + line['text'], parse_mode="HTML", disable_web_page_preview=True, reply_markup=reply_markup)
                        getMediaForPost(line, r)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号