-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwit_crawl.py
50 lines (40 loc) · 1.83 KB
/
twit_crawl.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
from email_utils import connectSmtp, makeMsg, sendEmail
from requests_oauthlib import OAuth1
from credentials import twitter_credential
import requests, datetime, sys
def getTweets():
auth = OAuth1(twitter_credential['TWITTER_APP_KEY'],
twitter_credential['TWITTER_APP_SECRET'],
twitter_credential['TWITTER_OAUTH_TOKEN'],
twitter_credential['TWITTER_OAUTH_TOKEN_SECRET'])
response = requests.request(
"GET", 'https://api.twitter.com/1.1/statuses/user_timeline.json',
params = { 'count': 50 }, auth = auth)
tweets = list()
for tweet in response.json():
aggregated = dict()
aggregated['tweet_id'] = tweet['id']
aggregated['created_at_string'] = tweet['created_at']
aggregated['created_at'] = datetime.datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S %z %Y')
aggregated['text'] = tweet['text']
aggregated['urls'] = tweet['entities']['urls']
aggregated['user'] = tweet['user']['screen_name']
if len(aggregated['urls']) == 0:
continue
aggregated['url'] = aggregated['urls'][0]['expanded_url']
aggregated['tweet_url'] = f'https://twitter.com/{aggregated["user"]}/status/{aggregated["tweet_id"]}'
tweets.append(aggregated)
return tweets
def filterTweets(tweets):
today = datetime.datetime.today() \
.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=datetime.timezone.utc)
yesterday = today - datetime.timedelta(days=1)
return list(filter(lambda t: t['created_at'] >= yesterday and t['created_at'] <= today, tweets))
def crawlTweets():
tweets = getTweets()
filtered_tweets = filterTweets(tweets)
if len(filtered_tweets) == 0:
sys.exit()
smtp = connectSmtp()
msg = makeMsg(filtered_tweets)
sendEmail(smtp, msg)