This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twistream.py
94 lines (78 loc) · 3.31 KB
/
Twistream.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
import tweepy
import datetime
class twistream:
"""
機能:
・検索
・TLツイートリツイート&いいね
・トップアカウントのツイートリツイート&いいね
"""
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.access_token = access_token
self.access_token_secret = access_token_secret
auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
auth.set_access_token(self.access_token, self.access_token_secret)
self.api = tweepy.API(auth)
self.Top_User = ''
def search(self, keyword): #検索
search_words = '({0}) -peing -com -jp -net'.format(keyword)
count = 10
search_results = self.api.search(q = search_words, lang = 'ja', result_type = 'recent', count = count, include_entities = True, tweet_mode = 'extended')
return search_results
def timeline(self): #タイムラインの取得
return self.api.home_timeline()
def top_user_timeline(self): #あるユーザーのタイムラインの取得
return self.api.user_timeline(id = self.Top_User)
def likes(self, tweets): #いいね!
for tweet in tweets:
tweet_id = tweet.id
tweet_source = tweet.source
if tweet_source == 'Twitter for iPhone':
try:
self.api.create_favorite(tweet_id)
except Exception as error:
print(error)
elif tweet_source == 'Twitter for Android':
try:
self.api.create_favorite(tweet_id)
except Exception as error:
print(error)
def retweets(self, tweets): #リツイート
for tweet in tweets:
tweet_id = tweet.id
tweet_source = tweet.source
if tweet_source == 'Twitter for iPhone':
try:
self.api.retweet(tweet_id)
except Exception as error:
print(error)
elif tweet_source == 'Twitter for Android':
try:
self.api.retweet(tweet_id)
except Exception as error:
print(error)
def likes_retweets(self, tweets): #いいね!とリツイート
for tweet in tweets:
tweet_id = tweet.id
tweet_source = tweet.source
if tweet_source == 'Twitter for iPhone':
try:
self.api.retweet(tweet_id)
self.api.create_favorite(tweet_id)
except Exception as error:
print(error)
elif tweet_source == 'Twitter for Android':
try:
self.api.retweet(tweet_id)
self.api.create_favorite(tweet_id)
except Exception as error:
print(error)
def get_timeline(self,tweets): #タイムラインの出力
for tweet in tweets:
contents = tweet.text
try:
print(contents)
except Exception as error:
print(error)