forked from DvdChe/social-pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
social-pipe.py
executable file
·277 lines (195 loc) · 8.18 KB
/
social-pipe.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
import tweepy
import configparser
import re
import pickle
import logging
from logging.handlers import RotatingFileHandler
from os import path
###############################################################################
# General config
# ==============
CurrentLocation = str(path.dirname(path.abspath(__file__)))
FlagFile = str('/tmp/social-pipe.flag')
RetweetedHistoryFile = CurrentLocation+'/retweeted.bin'
FollowerNameFile = CurrentLocation+'/followers.bin'
AuthConfFile = CurrentLocation+'/social-pipe.conf'
###############################################################################
# Parsing conf file:
# ==================
conf = configparser.ConfigParser()
conf.read(str(AuthConfFile))
consumer_key = str(conf['AUTH']['ConsumerKey'])
consumer_secret = str(conf['AUTH']['ConsumerSecret'])
access_token = str(conf['AUTH']['AccessToken'])
access_token_secret = str(conf['AUTH']['AccessTokenSecret'])
DryRunConf = str(conf['OPTIONS']['DryRun'])
NFetchTweet = int(conf['OPTIONS']['FetchTweet'])
OwnScreenName = str(conf['OPTIONS']['ScreenName'])
LangSearch = str(conf['OPTIONS']['LangSearch'])
ContestSearchSTR = str(conf['OPTIONS']['SearchSTR'])
FollowSTR = str(conf['OPTIONS']['FollowSTR'])
RetweetSTR = str(conf['OPTIONS']['RetweetSTR'])
QuoteSTR = str(conf['OPTIONS']['QuoteSTR'])
FavSTR = str(conf['OPTIONS']['FavSTR'])
LogLevel = int(conf['OPTIONS']['LogLevel'])
if DryRunConf == 'True':
DryRun = True
logging.info('This is a dry run. Nothing will happens.')
else:
DryRun = False
###############################################################################
# Logging configuration
# =====================
logger = logging.getLogger()
logger.setLevel(LogLevel)
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
# set log into file .log
file_handler = RotatingFileHandler(CurrentLocation+'/social-pipe.log', 'a', 1000000, 1)
file_handler.setLevel(LogLevel)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# set log into output std
stream_handler = logging.StreamHandler()
stream_handler.setLevel(LogLevel)
logger.addHandler(stream_handler)
###############################################################################
# Avoiding multiple executions
# ============================
#if path.isfile(FlagFile):
# logging.error("FlagFile exists. Is Social pipe is already running ?")
# exit(1)
#
logging.info('============ Starting Social-Pipe ============')
#open(FlagFile, 'a')
###############################################################################
# Connect to Twitter:
# ===================
try:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
except tweepy.TweepError as e:
logging.error(e)
#remove(FlagFile)
exit(e.message[0]['code'])
###############################################################################
# Get list of followers :
# ======================
tFollowers = []
if not path.isfile(FollowerNameFile):
logging.info('No followers list file found. Generating... It may take a while')
followers = tweepy.Cursor(api.followers_ids,
screen_name=OwnScreenName).items()
for follower in followers:
UserObj = api.get_user(user_id=follower)
# Using screen_name cause it's more easy to parse when it's needed
# check if user is already followed
tFollowers.append('@'+UserObj.screen_name)
fp = open(str(FollowerNameFile), 'wb')
pickle.dump(tFollowers, fp)
fp.close()
f = open(FollowerNameFile, 'rb')
tFollowers = pickle.load(f)
###############################################################################
# Lets search Contest tweet
# =========================
def SearchTweet(Keyword, lang, NumFetch):
out = tweepy.Cursor(
api.search, q=Keyword,
lang=lang,
tweet_mode='extended'
).items(NumFetch)
return out
ContestTweet = SearchTweet(ContestSearchSTR, LangSearch, NFetchTweet)
##############################################################################
# Lets load allready retweeded stuffs
# ==================================
if path.isfile(str(RetweetedHistoryFile)):
f = open(RetweetedHistoryFile, 'rb')
tRetweeted = pickle.load(f)
else:
tRetweeted = []
###############################################################################
# Parsing the tweet to know what to do.
# ====================================
try:
for tweet in ContestTweet:
# If it's a retweet :
# ===================
if hasattr(tweet, 'retweeted_status'):
TweetText = tweet.retweeted_status.full_text
TweetId = tweet.retweeted_status.id
Author = tweet.retweeted_status.user.id_str
AuthorScrenName = tweet.retweeted_status.user.id_str
if str(TweetId) not in tRetweeted:
# Let's find if there is suckers to follow:
# =========================================
if re.search(FollowSTR, TweetText, re.IGNORECASE):
ScreenNames = re.findall(r'[@]\w+', TweetText)
for ScreenName in ScreenNames:
try:
user = api.get_user(screen_name=ScreenName)
except tweepy.error.TweepError:
logging.warning('%s may be already followed', user.screen_name)
pass
except:
logging.error('Unknown error')
pass
else:
if not DryRun:
logging.info('Create friendship of : %s ', ScreenName)
api.create_friendship(user.id)
if not DryRun:
api.create_friendship(Author)
# If It needs to retweet
# ======================
if TweetId not in tRetweeted:
RegTweet = re.compile(RetweetSTR, re.IGNORECASE)
RegTag = re.compile(QuoteSTR, re.IGNORECASE)
if re.search(RegTweet, TweetText):
try:
if not DryRun:
retweet = api.retweet(TweetId)
logging.info('Retweeted : %s',TweetId)
except tweepy.error.TweepError:
logging.info('Warning : Tweet %s may already RT',TweetId )
pass
except:
logging.error('Unknown error')
pass
tRetweeted.append(str(TweetId))
# If it needs to be liked
# =======================
RegFav = re.compile(FavSTR, re.IGNORECASE)
if re.search(RegFav, TweetText):
try:
if not DryRun:
api.create_favorite(TweetId)
logging.info('Favorited : %s', TweetId)
except tweepy.error.TweepError:
logging.info('Warning : Tweet %s already in fav', TweetId )
pass
except:
logging.error('Unknown error')
pass
except tweepy.error.TweepError as e:
logging.error('%s',e)
pass
fp = open(str(RetweetedHistoryFile), 'wb')
pickle.dump(tRetweeted, fp)
fp.close()
logging.info('============ Social-Pipe Stopped ============')
#remove(FlagFile)