-
Notifications
You must be signed in to change notification settings - Fork 18
/
twitter.py
75 lines (61 loc) · 1.91 KB
/
twitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import io
from PIL import Image
import pytesseract
import requests
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
with open("config.json") as f:
config = json.load(f)
FOLLOW_IDS = config["TWITTER_FOLLOW_IDS"]
with open("secrets.json") as f:
keys = json.load(f)
CONSUMER_KEY = keys["TWITTER_CONSUMER_KEY"]
CONSUMER_SECRET = keys["TWITTER_CONSUMER_SECRET"]
ACCESS_KEY = keys["TWITTER_ACCESS_KEY"]
ACCESS_SECRET = keys["TWITTER_ACCESS_SECRET"]
class TwitterListener(StreamListener):
def __init__(self, callback):
self.callback = callback
def on_data(self, data):
tweet_json = json.loads(data)
try:
if tweet_json["user"]["id_str"] in FOLLOW_IDS:
print(tweet_json["text"])
self.callback(tweet_json)
except:
pass
class Twitter:
def __init__(self, tweet_callback=lambda x, y, z: x):
self.tweet_callback = tweet_callback
self.listener = TwitterListener(self.handle_tweet)
self.auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
self.auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
self.stream = Stream(self.auth, self.listener)
self.stream.filter(follow=FOLLOW_IDS)
def handle_tweet(self, tweet_json):
screen_name = tweet_json["user"]["screen_name"]
id = tweet_json["id_str"]
text = tweet_json["text"].replace("\\", "")
# Get media if present
try:
urls = [x["media_url"].replace("\\", "") for x in tweet_json["entities"]["media"] if x["type"] == "photo"]
for url in urls:
response = requests.get(url)
img = Image.open(io.BytesIO(response.content))
# Extract text from image
img_text = pytesseract.image_to_string(img)
text += f' . {img_text}'
except KeyError:
pass
link = f'https://twitter.com/{screen_name}/status/{id}'
try:
self.tweet_callback(text, screen_name, link)
except:
pass
if __name__ == "__main__":
import time
twitter = Twitter()
while True:
time.sleep(1)