This repository was archived by the owner on Jun 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwttrBadPython.py
More file actions
105 lines (86 loc) · 3.19 KB
/
twttrBadPython.py
File metadata and controls
105 lines (86 loc) · 3.19 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
from config import *
import tweepy, re
import os
last_id_filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "lastid.txt")
class TwitterAPI:
def __init__(self):
consumer_key = APP_KEY
consumer_secret = APP_SECRET
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
access_token = OAUTH_TOKEN
access_token_secret = OAUTH_TOKEN_SECRET
auth.set_access_token(access_token, access_token_secret)
self.api = tweepy.API(auth)
def tweet(self, message, in_reply_to_status_id):
self.api.update_status(status=message, in_reply_to_status_id=in_reply_to_status_id)
def search(self, query, count, since_id):
return self.api.search(q=query, count=count, since_id=since_id, lang="es")
def send_direct_msg(self, user_screen_name, text):
return twitter.api.send_direct_message(user_screen_name, text=text)
def is_valid(tweet):
text = tweet.text.lower()
user = tweet.user.screen_name
if MYSELF==user:
return False, 'my own name'
if 'monty' in text:
return False, 'monty in text'
if 'monthy' in text:
return False, 'monthy in text'
if 'martens' in text:
return False, 'martens in text' # a model of shoes :)
if hasattr(tweet, 'retweeted_status'):
return False, 'retweet'
p = re.compile(ur'\bphyton', re.IGNORECASE)
if not re.findall(p, text):
return False, 'python not in text'
return True, ''
def read_last_id():
try:
file = open(last_id_filepath, "r")
last_id = file.readline()
return int(last_id)
except:
return -1
def save_last_id(id):
file = open(last_id_filepath, "w")
file.write(str(tweet.id))
def send_direct_message(twitter, tweet):
if not tweet or not DIRECT_MSG_USER:
return
url = "https://twitter.com/" + tweet.user.screen_name + "/status/" + str(tweet.id)
directmsg = url + " - " + tweet.text
twitter.send_direct_msg(DIRECT_MSG_USER, text=directmsg)
def send_response(wrong_tweet):
if not wrong_tweet:
return
username = wrong_tweet.user.screen_name
directmsg = "Recuerda @" + username + ": no se escribe 'phyton', sino 'python'!"
replay_to_status_id = wrong_tweet.id
twitter.tweet(message=directmsg, in_reply_to_status_id=replay_to_status_id)
if __name__ == "__main__":
twitter = TwitterAPI()
max_id = read_last_id()
print (max_id)
try:
valid_tweet = None
new_tweets = twitter.search(query="phyton", count=20, since_id=max_id + 1)
tweet = None
for tweet in reversed(new_tweets):
valid, cause = is_valid(tweet)
if valid:
valid_tweet = tweet
break
else:
print (cause, tweet.text)
if valid_tweet:
save_last_id(valid_tweet.id)
print ('selected tweet: ', tweet.text)
send_response(valid_tweet)
elif tweet:
save_last_id(tweet.id)
except tweepy.TweepError as e:
print (e)
# depending on TweepError.code, one may want to retry or wait
# to keep things simple, we will give up on an error
exit()