-
Notifications
You must be signed in to change notification settings - Fork 14
/
twitter-step-1-daily-file.py
65 lines (53 loc) · 2.35 KB
/
twitter-step-1-daily-file.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
# -*- coding: utf-8 -*-import twitter_config
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
import json
import twitter_config
from datetime import datetime
import codecs
import pathlib
consumer_key = twitter_config.consumer_key
consumer_secret = twitter_config.consumer_secret
access_token = twitter_config.access_token
access_secret = twitter_config.access_secret
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
class TweetListener(StreamListener):
counter = 0
def on_data(self, data):
try:
json_data = json.loads(data)
# ایجاد پوشه و ذخیره توییت با آی دی در فایلهای متنی
Tweet_Directory = 'tweets/'+datetime.now().strftime("%Y-%m-%d")
pathlib.Path(Tweet_Directory).mkdir(parents=True, exist_ok=True)
if 'extended_tweet' in json_data:
if 'full_text' in json_data['extended_tweet']:
tweet_text = json_data['extended_tweet']['full_text']
else:
pass # i need to figure out what is possible here
elif 'text' in json_data:
tweet_text = json_data['text']
tweet_text = tweet_text.replace('\n', ' ').replace("\t"," ")
if "RT" not in tweet_text :
with codecs.open("tweets-daily/" + datetime.now().strftime("%Y-%m-%d") + '.txt', 'a',
encoding="utf-8") as f:
self.counter += 1
str_out = json_data["id_str"] + "\t"
tags = ""
for hashtag in json_data["entities"]["hashtags"]:
tags = tags + "," + hashtag["text"]
if tags != "":
tags = tags[1:]
str_out = str_out + "\t" + tags + "\t" + tweet_text + "\r\n"
f.write(str_out)
print(str(self.counter) + " : \t" + str_out)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
twitter_stream = Stream(auth, TweetListener())
twitter_stream.filter(languages=['fa'], track=['با' , 'از','به','در'])