Skip to content

Instantly share code, notes, and snippets.

@cdn
Created January 31, 2016 11:17
Show Gist options
  • Save cdn/f6e4b8bdc54b261fef17 to your computer and use it in GitHub Desktop.
Save cdn/f6e4b8bdc54b261fef17 to your computer and use it in GitHub Desktop.
current streaming.py detokened [ originally http://paste-app.net/m/kbb-4okq4 ]
#!/usr/bin/env python3
# https://github.com/tweepy/tweepy/raw/master/examples/streaming.py
from __future__ import absolute_import, print_function
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import simplejson as json
import html, requests
# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=""
consumer_secret=""
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=""
access_token_secret=""
base_url = 'https://api.app.net'
chanel = '00000'
token = 'AQAAAAA'
r = requests.session()
r.headers.update({'Authorization': 'Bearer %s' % (token)})
class StdOutListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
# print(data)
tw = json.loads(data)
# print(tw)
# if( text key exists )
# print(tw['text'])
# 'retweeted_status'
#
# if u'retweeted_status' in tw:
# print([u'retweeted_status'][u'text'])
# else:
# print(tw[u'text'])
k = tw.keys()
print('k ')
#
if not 'friends' in k:
# print('k ')
print(k)
# dict_keys(['filter_level', 'in_reply_to_screen_name', 'truncated', 'coordinates', 'id_str', 'lang', 'retweet_count', 'geo', 'in_reply_to_user_id_str', 'favorited', 'id', 'retweeted', 'user', 'text', 'favorite_count', 'place', 'timestamp_ms', 'in_reply_to_user_id', 'created_at', 'entities', 'contributors', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'is_quote_status', 'source'])
# k
# dict_keys(['user', 'retweeted_status', 'timestamp_ms', 'id', 'created_at', 'extended_entities', 'filter_level', 'in_reply_to_status_id', 'entities', 'contributors', 'favorite_count', 'in_reply_to_screen_name', 'retweet_count', 'in_reply_to_user_id_str', 'place', 'is_quote_status', 'text', 'in_reply_to_user_id', 'geo', 'truncated', 'possibly_sensitive', 'id_str', 'retweeted', 'in_reply_to_status_id_str', 'lang', 'coordinates', 'source', 'favorited'])
# if not 'retweeted_status' in k:
# print(tw['text'])
if 'id' in k:
print(tw['id'])
post = ''
if 'text' in k:
# with tw['entities']['urls'] as u:
# for l in iter
# substitute t.co-nonlink for expanded ? markdown: [display_url](expanded_url)
prefix = tw['user']['name'] + ' \n'
t = tw['text']
if 'retweeted_status' in k:
rs = tw['retweeted_status']
prefix += 'RT ' + rs['user']['name'] + ' \n'
t = rs['text']
k = rs.keys()
tw = rs
suffix = ''
k2 = []
if 'quoted_status' in k:
qs = tw['quoted_status']
suffix = '\n' + u"\u00AB" + qs['user']['name'] + ' \n'
s = qs['text']
k2 = qs.keys()
for u in qs['entities']['urls']:
s = s.replace(u['url'],'[' + u['display_url'] + '](' + u['expanded_url'] + ')')
s = html.unescape(s)
# https://github.com/drdrang/blackbirdpy/blob/master/blackbirdpy.py#l63
try:
media = qs['extended_entities']['media']
except (KeyError, AttributeError):
media = []
# For some reason, multiple photos have only one URL in the text of the tweet.
if len(media) > 0:
photolink = ''
for m in media:
if m['type'] == 'photo':
photolink += '' + m['media_url_https'] + ' '
else:
photolink += '[' + m['display_url'] + '](' + m['expanded_url'] + ')'
s = s.replace(m['url'], photolink)
suffix += s + u"\u00BB"
#omnipresent
# if 'entities' in k and 'urls' in tw['entities']:
# print('urls ')
# print(tw['entities']['urls'])
for u in tw['entities']['urls']:
t = t.replace(u['url'],'[' + u['display_url'] + '](' + u['expanded_url'] + ')')
# if 'entities' in k and 'media' in tw['entities']:
# print('media ')
# print(tw['entities']['media'])
# media
# [{'display_url': 'pic.twitter.com/nl5MKrwjWg', 'id': 662234060144508928, 'type': 'photo', 'media_url': 'http://pbs.twimg.com/media/CTC6XnkUAAA3QP0.jpg', 'sizes': {'large': {'w': 1024, 'resize': 'fit', 'h': 598}, 'small': {'w': 340, 'resize': 'fit', 'h': 198}, 'medium': {'w': 600, 'resize': 'fit', 'h': 350}, 'thumb': {'w': 150, 'resize': 'crop', 'h': 150}}, 'indices': [72, 95], 'id_str': '662234060144508928', 'url': 'https://t.co/nl5MKrwjWg', 'expanded_url': 'http://twitter.com/ARTniyet/status/662234077832040448/photo/1', 'media_url_https': 'https://pbs.twimg.com/media/CTC6XnkUAAA3QP0.jpg'}]
if 'extended_entities' in k:
print('x ')
print(tw['extended_entities'])
# x
# {'media': [{'display_url': 'pic.twitter.com/nl5MKrwjWg', 'id': 662234060144508928, 'type': 'photo', 'media_url': 'http://pbs.twimg.com/media/CTC6XnkUAAA3QP0.jpg', 'sizes': {'large': {'w': 1024, 'resize': 'fit', 'h': 598}, 'small': {'w': 340, 'resize': 'fit', 'h': 198}, 'medium': {'w': 600, 'resize': 'fit', 'h': 350}, 'thumb': {'w': 150, 'resize': 'crop', 'h': 150}}, 'indices': [72, 95], 'id_str': '662234060144508928', 'url': 'https://t.co/nl5MKrwjWg', 'expanded_url': 'http://twitter.com/ARTniyet/status/662234077832040448/photo/1', 'media_url_https': 'https://pbs.twimg.com/media/CTC6XnkUAAA3QP0.jpg'}]}
if 'entities' in k2 and 'media' in qs['entities']:
print('qm ')
print(qs['entities']['media'])
if 'extended_entities' in k2:
print('qx ')
print(qs['extended_entities'])
# https://github.com/drdrang/blackbirdpy/blob/master/blackbirdpy.py#l63
try:
media = tw['extended_entities']['media']
except (KeyError, AttributeError):
media = []
# For some reason, multiple photos have only one URL in the text of the tweet.
if len(media) > 0:
# print('media ') + str(len(media))
photolink = ''
for m in media:
if m['type'] == 'photo':
photolink += '\n[media](' + m['media_url_https'] + ')'
else:
photolink += '[' + m['display_url'] + '](' + m['expanded_url'] + ')'
t = t.replace(m['url'], photolink)
t = html.unescape(t)
post = {
'text': prefix + t + suffix,
'entities': { "parse_links": True, "parse_markdown_links": 1 },
'annotations': [
# {
# "type": "cx.cdn.lab.weather.forecast",
# "value": {
# "frequency": "daily"
# }
# },
# {
# "type": "cx.cdn.lab.birdfeed.op",
# "value": {
# "original": data,
# }
# },
{
"type": "net.app.core.crosspost",
"value": {
"canonical_url": 'https://twitter.com/' + tw['user']['screen_name'] + '/status/' + tw['id_str'],
}
}
]
}
# post_resp = r.post(base_url + '/channels/00000/messages?include_post_annotations=1', json.dumps(post), headers={'Content-Type': "application/json"})
# else:
# post = {
# 'text': data
# }
if not 'delete' in k and not 'friends' in k:
post_resp = r.post(base_url + '/channels/' + chanel + '/messages?include_post_annotations=1', json.dumps(post), headers={'Content-Type': "application/json"})
# print(post_resp.json())
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
# stream.filter(track=['basketball'])
#
stream.userstream('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment