/
pumpernickel.py
executable file
·211 lines (165 loc) · 6.99 KB
/
pumpernickel.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
#!/usr/bin/env python3
import sys
import os
from PyPump.PyPump import PyPump
import webbrowser
class Pumpernickel():
def __init__(self,args):
self.client_name = 'Pumpernickel'
self.user = None
self.oauth_token = None
self.oauth_token_secret = None
self.oauth_client_id = None
self.oauth_client_secret = None
self.configFile = None
self.config = None
self.pump = None
if not self.checkConfigFile():
self.doSetup()
self.args = args
def doArgActions(self, args):
print('looking through the arguments')
for arg in args:
if '--debug' in arg:
self.debugMode = True
elif '--followersfile=' in arg.lower():
#read a line-delimited file of users,
# and follow them all
followersFile = arg.split('=')[1]
self.followUsersFromFile(followersFile)
elif '--setup' in arg.lower():
#re-authenticate and all that
self.doSetup()
def checkConfigFile(self):
self.configFile = os.path.join(os.environ['HOME'],'.config',self.client_name.lower())
if not os.path.exists(self.configFile):
os.makedirs(self.configFile)
self.configFile = os.path.join(self.configFile,self.client_name.lower() + '.conf')
if not os.path.exists(self.configFile):
print('config file is invalid: ' + self.configFile)
return False
print('config file check is done')
print('config file: ' + self.configFile)
return True
def doSetup(self):
self.webfinger = input('Enter your webfinger ID (ie. "jrobb@microca.st", no quotes):')
if '@' not in self.webfinger:
print('not a valid webfinger address')
return False
self.user, self.server = self.webfinger.split('@')
self.pump = PyPump(self.webfinger, client_name=self.client_name)
self.pump.set_nickname(self.user)
registration = self.pump.get_registration() #return (self.consumer.key, self.consumer.secret, self.consumer.expirey)
self.oauth_client_id = registration[0]
self.oauth_client_secret = registration[1]
self.oauth_client_expirey = registration[2]
del registration
token = self.pump.get_token() #return (self.token, self.token_secret)
self.oauth_token = token[0]
self.oauth_token_secret = token[1]
del token
print('setup stuff done, write to config file:')
self.writeConfig()
def writeConfig(self):
with open(self.configFile, 'w') as f:
f.write('webfinger,' + self.webfinger)
f.write('\n')
f.write('oauth_client_id,' + self.oauth_client_id )
f.write('\n')
f.write('oauth_client_secret,' + self.oauth_client_secret)
f.write('\n')
f.write('oauth_token,' + self.oauth_token)
f.write('\n')
f.write('oauth_token_secret,' + self.oauth_token_secret)
f.write('\n')
f.close()
def readConfig(self):
print('reading config file')
with open(self.configFile, 'r') as f:
self.config = f.readlines() #read the lines into a list
f.close()
print('checking the data if it exists:')
if self.config:
for line in self.config:
lineVars = line.split(',')
print(lineVars[0] + ' :: ' + lineVars[1])
if 'webfinger' in line and self.user == None:
self.user, self.server = lineVars[1].split("@")
elif 'oauth_client_id' in line:
self.oauth_client_id = lineVars[1]
elif 'oauth_client_secret' in line:
self.oauth_client_secret = lineVars[1]
elif 'oauth_token_secret' in line:
self.oauth_token_secret = lineVars[1]
elif 'oauth_token' in line:
self.oauth_token = lineVars[1]
else:
print('unknown config param: ' + line)
if not self.user or not self.server or not self.oauth_token_secret or not self.oauth_client_secret:
print('user info not sufficiently filled out!')
if not self.user:
print('user')
elif not self.server:
print('server')
elif not self.oauth_token_secret:
print('token secret')
elif not self.oauth_client_secret:
print('client secret')
sys.exit()
self.webfinger = self.user + '@' + self.server
print('webfinger: ' + self.webfinger)
isSecure = True #default to https
#~ (self, server, key=None, secret=None,
#~ client_name="", client_type="web", token=None,
#~ token_secret=None, save_token=None, secure = True):
self.pump = PyPump( \
server=self.server, \
key=self.oauth_client_id, \
secret=self.oauth_client_secret, \
client_name=self.client_name, \
client_type="native", \
token=self.oauth_token, \
token_secret=self.oauth_token_secret, \
secure= isSecure \
)
else:
print('no information, do setup')
self.doSetup()
self.pump.set_nickname(self.user.split('@')[0])
print('done reading config file. loaded.')
def followUsersFromFile(self, inFile):
if self.pump == None:
print('pump is not authorized yet')
return
if not os.path.isfile(inFile):
print(inFile + ' is not a valid file.')
sys.exit()
with open(inFile, 'r') as f:
print("file is opened")
sfile = f.readlines() #read the lines into a list
f.close()
if not sfile:
print('file could not be read')
return
for userToFollow in sfile:
print('trying to sub: ' + userToFollow)
self.pump.follow(userToFollow)
print('done subbing')
def run(self):
print('running')
self.readConfig()
if self.args:
self.doArgActions(self.args)
del self.args
print('startup is finished')
#~ print('attempting to post note:')
#pump.post_note("one last test post (from python), using this libstuff: https://github.com/xray7224/PyPump ")
#~ if postContent != '':
#~ pump.post_note(postContent)
#~ print('note posted')
#print('here\'s the inbox:')
#print(pump.inbox('major'))
#~ print('...done...')
if __name__ == "__main__":
app = Pumpernickel(sys.argv[1:])
app.run()