This repository was archived by the owner on Sep 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchmixesbot.py
More file actions
executable file
·73 lines (62 loc) · 2.37 KB
/
archmixesbot.py
File metadata and controls
executable file
·73 lines (62 loc) · 2.37 KB
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
import tweepy
import sys
import os
class ArchmixesBot(object):
def __init__(self, consumer_key, consumer_secret, access_token, token_secret,
source_file_name, index_file_name):
self.source_file_name = source_file_name
self.index_file_name = index_file_name
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, token_secret)
self.twitter = tweepy.API(auth)
def _get_current_index(self):
if not(os.path.isfile(self.index_file_name)):
return 0
with open(self.index_file_name) as index_fh:
return int(index_fh.read().strip())
def _increment_index(self, index):
with open(self.index_file_name, "w") as index_fh:
index_fh.truncate()
index_fh.write("%d" % (index + 1))
index_fh.close()
def _get_current_line(self, index):
with open(self.source_file_name) as source_fh:
# read up until the desired line
for i in range(index+1):
status_str = source_fh.readline().strip()
return status_str
def post(self):
# get index from current_line.txt
index = self._get_current_index()
# get path corresponding to above index from tweet_list.txt
status_str = self._get_current_line(index)
# processing the file name - split at start
trash1, tweetTextRaw = status_str.split('@')
# processing the file name - split at end
tweetText, trash2 = tweetTextRaw.split('.jpg')
# post image and status
self.twitter.update_with_media(status_str, '@'+tweetText)
# increment index
self._increment_index(index)
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option('--consumer_key', dest='consumer_key',
help="twitter consumer key")
parser.add_option('--consumer_secret', dest='consumer_secret',
help="twitter consumer secret")
parser.add_option('--access_token', dest='access_token',
help="twitter token key")
parser.add_option('--token_secret', dest='token_secret',
help="twitter token secret")
parser.add_option('--source_file', dest='source_file',
default="tweet_list.txt",
help="source file (one line per tweet)")
parser.add_option('--index_file', dest='index_file',
default="index",
help="index file (must be able to write to this file)")
(options, args) = parser.parse_args()
bot = ArchmixesBot(options.consumer_key, options.consumer_secret,
options.access_token, options.token_secret, options.source_file,
options.index_file)
bot.post()