This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
74 lines (60 loc) · 2.18 KB
/
bot.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
# -*- coding: utf-8 -*-
# set credentials in credentials.py
from credentials import (
API_KEY,
API_SECRET,
CLIENT_TOKEN,
CLIENT_SECRET,
# user_list as ids (strings)
USER_LIST,
# user_list that should be ignored
USER_BLACK_LIST,
)
import tweepy
import random
import json
from replies import replies_list
class StdOutListener(tweepy.streaming.StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
last_own_tweet = None
def on_data(self, data):
data = json.loads(data)
print(data.get('user', {}).get('id')),
print(data.get('text'))
if '@schnitzel' in data.get('text', '').lower():
return True
# ignore some users
if data.get('user', {}).get('id') in USER_BLACK_LIST:
return True
try:
# FIXME: how to test if already faved?
api.create_favorite(data.get('id'))
except:
pass
if data.get('user', {}).get('id') in USER_LIST:
# for now don't reply.
# will be added later
return True
# reply to some of the tweets
if random.randint(0, 10) == 4:
# FIXME not the best way to to this:
new_status = random.choice(replies_list)
while new_status == self.last_own_tweet:
new_status = random.choice(replies_list)
last_own_tweet = new_status
api.update_status(status='@%s %s' % (data.get('user').get('screen_name'),
last_own_tweet),
in_reply_to_status_id=data.get('id')
)
return True
def on_error(self, status):
print(status)
l = StdOutListener()
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(CLIENT_TOKEN, CLIENT_SECRET)
api = tweepy.API(auth)
stream = tweepy.Stream(auth, l)
stream.filter(track=['schnitzel', '#schnitzel', '#schnitzelbot', '@schnitzelfollow', '#hackerschnitzelcloud', '#schnitzelmuc', '#schnitzels', '#schnitzelffm', '#schnitzelminga'],
follow=USER_LIST)