-
Notifications
You must be signed in to change notification settings - Fork 0
/
replies.py
78 lines (58 loc) · 2.16 KB
/
replies.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
76
77
78
import csv
import pandas
import tweepy
import re
import credentials as crds
name = 'WordleGame_Bot'
def get_recent_tweet_id():
tweets_list= crds.api.user_timeline(screen_name=name, count=1, exclude_replies=True)
recent_tweet = tweets_list[0]
tweet_id = recent_tweet.id_str
return tweet_id
def get_replies():
tweet_id = get_recent_tweet_id()
replies = []
for tweet in tweepy.Cursor(crds.api.search_tweets,q='to:'+name, result_type='recent').items(1000):
if hasattr(tweet, 'in_reply_to_status_id_str'):
if (tweet.in_reply_to_status_id_str==tweet_id):
replies.append(tweet)
return replies
def filter_guesses_from_replies():
replies = get_replies()
with open('replies.csv', 'w', encoding = "utf-8") as f:
csv_writer = csv.DictWriter(f, fieldnames=('ID', 'TEXT', 'LIKES'))
csv_writer.writeheader()
for tweet in replies:
text = tweet.text.replace(f'@{name} ', '')
text = text.replace('\n', ' ')
text = text.lower()
row = {'ID': tweet.id_str, 'TEXT': text, 'LIKES': tweet.favorite_count}
if '\[' and ']' in text or len(text) == 5:
csv_writer.writerow(row)
def read_csv_as_dataframe():
return pandas.read_csv('replies.csv', encoding = "utf-8")
def remove_brackets():
df = read_csv_as_dataframe()
for i,content in enumerate(df['TEXT']):
if "\[" and "]" in content:
try:
bracketless = re.search(r"\[([A-Za-z0-9_]+)\]", content).group(1)
df.at[i,'TEXT'] = bracketless
except AttributeError:
pass
df.to_csv('replies.csv', index=False)
def sort_by_likes():
df = read_csv_as_dataframe()
df = df.sort_values('LIKES', ascending=False)
df.to_csv('replies.csv', index=False)
def get_guess(guess_list):
filter_guesses_from_replies()
remove_brackets()
sort_by_likes()
df = read_csv_as_dataframe()
for i,content in enumerate(df['TEXT']):
if content in guess_list and len(content) == 5:
guess = df.iat[i,1]
guess_id = df.iat[i,0]
break
return guess, guess_id