-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
145 lines (113 loc) · 3.89 KB
/
main.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
from dotenv import load_dotenv
import tweepy
import time
import spreadsheet
from locApi import loc
import datetime
load_dotenv()
google_map = loc() # Object of loc class
services = ['oxygen', 'beds', 'bed'] # List of services available
token = os.getenv("TOKEN") # API Key-Tokens
token_secret = os.getenv("TOKEN_SECRET")
consumer_key = os.getenv("CONSUMER_KEY")
consumer_secret = os.getenv("CONSUMER_SECRET")
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) # Connecting to bot using creds
auth.set_access_token(token, token_secret)
api = tweepy.API(auth)
print("\nTwitter Bot is Up!")
print("made with <3 by Covid Warriors")
def retrieve_id(file_name):
'''
Function to retrieve tweet id from file
and save it in a list of ids.
'''
ids = []
with open(file_name, 'r') as file:
f = file.readlines()
for i in range(len(f)):
if '\n' in f[i]:
ids.append(f[i][:-1].strip())
else:
ids.append(f[i].strip())
return ids
def store_id(id, file_name):
'''
Function to store tweet key into file.
'''
with open(file_name, 'a') as f:
f.write(str(id) + '\n')
return
def scrape(hashtag, date_since):
tweets = tweepy.Cursor(api.search, q=hashtag, lang="en", since=date_since, tweet_mode='extended').items()
tweets_list = [tweet for tweet in tweets]
for tweet in reversed(tweets_list):
id = tweet.id_str
try:
ids = retrieve_id('lastseen_id.txt')
if id not in ids:
text = tweet.full_text
state = find_state(text.lower())
service_need = find_service(text.lower())
print(str(id) + ' - ' + text, flush=True)
available = spreadsheet.get_data(text.split(), state, service_need)
if not available:
available = spreadsheet.get_data(text.split(), state, service_need, statewise=True)
tweet_toSend = spreadsheet.get_tweet(available, service_need)
api.update_status('@' + tweet.user.screen_name + " " + tweet_toSend, id)
store_id(id, 'lastseen_id.txt')
else:
print("Already replied to " + str(id))
except:
store_id(id, 'lastseen_id.txt')
def find_state(tweet):
'''
Find state from tweet
Iterate through tweet words and put them in google map API
fetch the state and return it.
'''
tweet_list = tweet.split()
for t in tweet_list:
try:
address = google_map.locate(t)
display_name = address['display_name'].split(',')
country = display_name[-1].strip()
except:
country = "NA"
if country == "India":
try:
state = display_name[-2].strip()
except IndexError:
continue
try:
temp = int(state)
state = display_name[-3].strip()
return state
except ValueError:
return state
def find_service(tweet):
'''
Find the service need from user's tweet
'''
tweet_list = tweet.split()
for t in tweet_list:
if t in services:
if t == "oxygen":
index = tweet_list.index(t)
if tweet_list[index + 1] == "bed" or tweet_list[index + 1] == 'beds':
t = "oxygen bed"
elif t == "beds":
t = "bed"
return t
def date():
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
return str(yesterday)
while True:
yesterday = date()
scrape("#hospitalbeds", yesterday)
#scrape("#CovidHelp", yesterday)
#scrape("#NeedOxygen", yesterday)
#scrape("#COVIDEmergency", yesterday)
#scrape("#covidwarriorbottesting", yesterday)
time.sleep(100)