-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_handle.py
39 lines (30 loc) · 928 Bytes
/
twitter_handle.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
import oauth2
import re
import tweepy
class Tweets():
def __init__(self, api):
self.api = api
self.screen_name = api.me().screen_name
def get(self, demand=128):
cnt = 0
data = []
exclude_ptn = re.compile(r"^[(?:RT)@]")
tl = tweepy.Cursor(self.api.user_timeline, screen_name=self.screen_name, exclude_replies=True).items()
for tweet in tl:
if cnt >= demand:
break
try:
text = tweet.text
if text and not exclude_ptn.search(text):
data.append(text)
cnt += 1
except UnicodeEncodeError:
pass
return data
def post(self, status):
res = "ok"
try:
self.api.update_status(status=status)
except tweepy.TweepError:
res = "error"
return res