-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweepy_savefulltxt.py
51 lines (35 loc) · 1.15 KB
/
tweepy_savefulltxt.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
import tweepy
import datetime
import json
def readConfig():
json_file = open('config.json', 'r')
json_obj = json.load(json_file)
CK = json_obj['Consumer_key']
CS = json_obj['Consumer_secret']
AT = json_obj['Access_token']
AS = json_obj['Access_secret']
return [CK, CS, AT, AS]
def authTwitter():
config = readConfig()
auth = tweepy.OAuthHandler(config[0], config[1])
auth.set_access_token(config[2], config[3])
api = tweepy.API(auth, wait_on_rate_limit=True)
return (api)
def getTwitterData(keyword, dfile):
api = authTwitter()
q = keyword
tweets_data = []
for tweet in tweepy.Cursor(api.search_tweets, q=q, include_entities=True, tweet_mode='extended').items():
tweets_data.append(tweet.full_text + '\n')
fname = r"'" + dfile + "'"
fname = fname.replace("'", "")
with open(fname, "w", encoding="utf-8") as f:
f.writelines(tweets_data)
def main():
print('====== Enter Search KeyWord =====')
keyword = input('> ')
print('====== Enter Tweet Data file =====')
dfile = input('> ')
getTwitterData(keyword, dfile)
if __name__ == "__main__":
main()