-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_user_tweets.py
41 lines (34 loc) · 1.15 KB
/
search_user_tweets.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
"""
A Twitter dev account is needed for this
https://developer.twitter.com/en/apply-for-access
Usage:
id = "joerodr"
search_tweets(api,id,items,search_term)
Parameters:
api – Authentication to the twitter API
id – twitter handle to search
search_term – String to search for in tweet
only_urls – Only returns URLs in a tweet if a tweet has a URL
Return type:
string of tweets
"""
import tweepy,re
auth = tweepy.OAuthHandler('', '')
auth.set_access_token('', '')
api = tweepy.API(auth)
def user_status_count(api,id):
# fetching the user
user = api.get_user(id)
# fetching the statuses_count attribute
return user.statuses_count
def search_tweets(api,id,items,search_term,only_urls=False):
for tweet in tweepy.Cursor(api.user_timeline, id=id).items(items):
if search_term in tweet.text:
if(only_urls == 'True'):
urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+', tweet.text)
if(len(urls) > 0):
print(urls[0])
else:
print(tweet.text)
id = "joerodr"
search_tweets(api,id,user_status_count(api,id),"Awesome")