-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcastindex.py
executable file
·387 lines (333 loc) · 17.4 KB
/
podcastindex.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python3
import sys
import os
import json
import requests
import subprocess
import configuration
import PIfunctions
import Appfunctions
import configuration
import generalfunctions
def get_app_name(log_path):
print('Fetching app name...')
App_url = configuration.config["appname"]["url"]
result = Appfunctions.request(url, log_path)
if result != None and 'name' in result and type(result['name']) is str:
result = result['name']
else:
result = 'NodeBoost'
return result
def get_app_split(log_path):
print('Fetching app split...')
split = 1
take_split = configuration.config["appsplit"]["insplit"]
if bool(take_split):
app_url = configuration.config["appsplit"]["url"]
result = Appfunctions.request(url, log_path)
if result != None:
if 'split' in result and type(result['split']) is int:
result = result['split']
return split
def get_pi_split(log_path):
print('Fetching PodcastIndex split...')
split = 1
take_split = configuration.config["pisplit"]["insplit"]
if bool(take_split):
pi_url = configuration.config["pisplit"]["url"]
result = Appfunctions.request(url, log_path)
if result != None:
if 'split' in result and type(result['split']) is int:
result = result['split']
return split
def get_app_nodeaddress(log_path):
print('Fetching app address...')
App_url = configuration.config["appnodeaddress"]["url"]
result = Appfunctions.request(url, log_path)
if result != None and 'address' in result and type(result['address']) is str:
result = result['address']
else:
result = None
return result
def get_app_customKey(log_path):
App_url = configuration.config["app"]["url"]
url = App_url + "appcustomkey"
result = Appfunctions.request(url, log_path)
if result != None and 'customkey' in result and type(result['customkey']) is int:
result = result['customkey']
else:
result = None
return result
def get_app_customValue(log_path):
App_url = configuration.config["app"]["url"]
url = App_url + "appcustomvalue"
result = Appfunctions.request(url, log_path)
if result != None and 'customvalue' in result and type(result['customvalue']) is int:
result = result['customvalue']
else:
result = None
return result
def send_boostagram_command(sendboostagramscript, boostagrammode, unlocked, title, episode, timestamp, podcast_id, feed_url, name, address, customKey, customValue, message, sender, sats):
command = sendboostagramscript + ' ' + '\"' + str(boostagrammode) + '\"' + ' ' + str(unlocked) + ' ' + '\"' + title + '\"' + ' \"' + episode + '\"' + ' ' + str(timestamp) + ' ' + str(podcast_id) + ' ' + '\"' + feed_url + '\"' + ' ' + '\"' + str(name) + '\"' + ' ' + str(address) + ' ' + str(customKey) + ' ' + str(customValue) + ' ' + '\"' + message + '\"' + ' ' + '\"' + str(sender) + '\"' + ' ' + str(sats)
return command
def podcastdata(feedId, log_path):
PIurl = configuration.config["podcastindex"]["url"]
url = PIurl + "podcasts/byfeedid?id=" + str(feedId)
feed_result = PIfunctions.request(url, log_path)
return feed_result
def calculate_sats_after_fees(sats_total, valueblock):
sats_after_fees = int(sats_total)
if pi_split != None:
sats_after_fees -= int(int(sats_total) / 100 * int(pi_split))
if app_split != None:
sats_after_fees -= int(int(sats_total) / 100 * int(app_split))
for recipient in valueblock:
if 'fee' in recipient and recipient['fee']:
sats_after_fees -= int(int(sats_total) / 100 * int(recipient['split']))
return sats_after_fees
def calculate_split_total(valueblock):
split_total = 0
if valueblock !=None:
for recipient in valueblock:
if 'fee' not in recipient or not recipient['fee']:
split_total += int(recipient['split'])
return split_total
def check_valueblock(valueblock):
passed = False
if valueblock !=None:
for recipient in valueblock:
if 'type' in recipient and recipient['type'] == 'node':
passed = True
return passed
def nodesdata(data):
value = None
if data !=None:
if 'value' in data and 'model' in data['value'] and 'destinations' in data['value']:
if 'type' in data['value']['model'] and 'method' in data['value']['model']:
if data['value']['model']['type'] == 'lightning':
if data['value']['model']['method'] == 'keysend':
value = data['value']['destinations']
return value
def podcast_value(feedId, log_path):
value = None
PIurl = configuration.config["podcastindex"]["url"]
url = PIurl + "value/byfeedid?id=" + str(feedId) + "&pretty"
result = PIfunctions.request(url, log_path)
if result !=None:
value = nodesdata(result)
return value
def episodesdata(feedId, log_path):
value = None
PIurl = configuration.config["podcastindex"]["url"]
url = PIurl + "episodes/byfeedid?id=" + str(feedId) + "&max=1000" + "&pretty"
episodes_result = PIfunctions.request(url, log_path)
return episodes_result
def episodedata(feedId, episode_nr, log_path):
value = None
match = False
if int(episode_nr) > 0:
episodes_result = episodesdata(feedId, log_path)
for episode in episodes_result['items']:
if not match:
value = episode
if episode['episode'] == str(episode_nr):
match = True
else:
if str(episode_nr) in episode['title']:
match = True
else:
if '#' + str(episode_nr) in episode['description'] or ' ' + str(episode_nr) in episode['description']:
match = True
return value
def episode_value(feedId, episode, log_path):
value = None
if episode != None:
value = nodesdata(episode)
if value == None:
value = podcast_value(feedId, log_path)
else:
value = podcast_value(feedId, log_path)
return value
def process_file(mode, data, episode_nr, podcast_to_process):
for podcast_data in data['podcastlist']:
if podcast_data["id"][:1] != '#':
if podcast_to_process == "ALL" or str(podcast_to_process) == podcast_data["id"] or str(podcast_to_process) == podcast_data["feed"]:
if mode == "VALUE" or (mode == "BOOST" and podcast_data["boostable"] == "1"):
pi_podcast = podcastdata(podcast_data['id'], log_path)
pi_episode = episodedata(podcast_data['id'], episode_nr, log_path)
valueblock = episode_value(podcast_data['id'], pi_episode, log_path)
episode_in_index = False
if pi_episode != None:
episode_in_index = True
message = 'Podcast: ' + pi_podcast["feed"]['title']
generalfunctions.log(log_path, message, False, False)
print(message)
if int(episode_nr) > 0:
message = 'Episode: ' + pi_episode['title']
generalfunctions.log(log_path, message, False, False)
print(message)
if mode == "BOOST":
message = 'Timestamp: ' + str(timestamp)
generalfunctions.log(log_path, message, False, False)
message = 'Sender: ' + sender
generalfunctions.log(log_path, message, False, False)
message = 'sats: ' + str(sats_total)
generalfunctions.log(log_path, message, False, False)
if int(episode_nr) == 0 or episode_in_index:
if valueblock != None:
if mode == "BOOST":
message = 'Boostagram: ' + boostagrammessage
generalfunctions.log(log_path, message, False, False)
print(message)
message = 'Sender: ' + sender
generalfunctions.log(log_path, message, False, False)
print(message)
if mode == "BOOST" or mode == "VALUE":
if check_valueblock(valueblock):
split_total = calculate_split_total(valueblock)
sats_after_fees = calculate_sats_after_fees(sats_total, valueblock)
if int(episode_nr) == 0:
episode_title = 'No specific episode'
else:
episode_title = pi_episode['title']
if app_split == None:
message = 'Fee for app can\'t be determined.'
else:
message = 'App takes ' + str(app_split) + ' part(s) of split'
print(message)
message = 'Total split is: ' + str(split_total)
print(message)
if mode == "BOOST":
if app_split != None:
sats_app = int(int(sats_total) / 100 * int(app_split))
command = send_boostagram_command(sendboostagramscript, boostagrammode, unlocked, pi_podcast['feed']['title'], episode_title, timestamp, pi_podcast['feed']['id'], pi_podcast['feed']['url'], app_name, app_address, app_customKey, app_customValue, boostagrammessage, app_name, sats_app)
boostagram_result=subprocess.run(command, shell=True).returncode
message = 'Sent split to app developer. '
if boostagram_result == 0:
message += '(Successful)'
generalfunctions.log(log_path, message, False, False)
else:
message += '(FAILED)'
generalfunctions.log(log_path, message, True, False)
else:
message = 'No API (Application fee). Server (application side) offline or no internet connection.'
generalfunctions.log(log_path, message, True, False)
for recipient in valueblock:
if mode == "BOOST":
if os.path.exists(sendboostagramscript):
message = 'Boosting ' + recipient['name']
if 'fee' in recipient and recipient['fee']:
sats_recipient = int(int(sats_total) / 100 * int(recipient['split']))
else:
sats_recipient = int(int(recipient['split']) / int(split_total) * int(sats_after_fees))
if sats_recipient == 0:
message = message + '. Amount of sats is 0. No sats sent.'
generalfunctions.log(log_path, message, True, False)
print(message)
else:
message = message + ' ' + str(sats_recipient) + ' sats'
if 'fee' in recipient and recipient['fee']:
message = message + ' (fee)'
message += '.'
customKey = 0
customValue = 0
if 'customKey' in recipient and len(recipient['customKey']) > 0:
if 'customValue' in recipient and len(recipient['customValue']) > 0:
customKey = recipient['customKey']
customValue = recipient['customValue']
command = send_boostagram_command(sendboostagramscript, boostagrammode, unlocked, pi_podcast['feed']['title'], episode_title, timestamp, pi_podcast['feed']['id'], pi_podcast['feed']['url'], recipient['name'], recipient['address'], customKey, customValue, boostagrammessage, sender, sats_recipient)
boostagram_result=subprocess.run(command, shell=True).returncode
message += ' '
if boostagram_result == 0:
message += 'Successful'
else:
message += 'FAILED (lncli return code: ' + str(boostagram_result) + ')'
generalfunctions.log(log_path, message, False, False)
print(message)
else:
message = 'File \'' + sendboostagramscript + '\' does not exist'
print(message)
generalfunctions.log(log_path, message, True, False)
if mode == "VALUE":
message = 'Recipient: ' + recipient['name'] + ' Split: ' + str(recipient['split'])
if 'fee' in recipient and recipient['fee']:
message += '% (fee)'
print(message)
else:
message = 'No valueblock in index'
generalfunctions.log(log_path, message, False, False)
print(message)
else:
if int(episode_nr) > 0:
message = 'Episode ' + str(episode_nr) + ' not in index'
generalfunctions.log(log_path, message, True, False)
print(message)
else:
if mode == "BOOST":
message = 'Podcast \'' + podcast_data["title"] + '\'' + 'is set in your configuration to be not boostable'
print(message)
generalfunctions.log(log_path, message, False, False)
else:
if podcast_to_process == "ALL":
message = 'Skipping podcast \'' + podcast_data["title"] + '\''
print(message)
generalfunctions.log(log_path, message, False, False)
try:
podcast_to_process = "--help"
unlocked = 1
configuration.read()
podcastlist_file = configuration.config["file"]["podcastlist"]
sendboostagramscript = configuration.config["file"]["sendboostagram"]
boostagrammode = configuration.config["settings"]["mode"]
timestamp = configuration.config["settings"]["timestamp"]
boostagrammessage = configuration.config["settings"]["message"]
sender = configuration.config["settings"]["sender"]
sats_total = configuration.config["settings"]["sats_total"]
if len(sys.argv) == 8:
mode = sys.argv[1]
podcast_to_process = sys.argv[2]
episode_nr = sys.argv[3]
timestamp = sys.argv[4]
sender = sys.argv[5]
sats_total = sys.argv[6]
boostagrammessage = sys.argv[7]
if boostagrammessage == 'fromfile':
boostagrammessage = generalfunctions.read_file(configuration.config["file"]["boostagramfile"])
if os.path.exists(boostagrammessage):
boostagrammessage = generalfunctions.read_file(boostagrammessage)
else:
if boostagrammessage[:1] == '/':
boostagrammessage = configuration.config["settings"]["message"]
if len(sys.argv) == 4:
mode = sys.argv[1]
podcast_to_process = sys.argv[2]
episode_nr = sys.argv[3]
if (len(sys.argv) == 4 or len(sys.argv) == 8) and (mode == "VALUE" or mode == "BOOST"):
configuration.read()
log_path = configuration.config["directory"]["log"]
now = generalfunctions.now()
dateString = generalfunctions.format_dateYYYMMDDHHMMSS(now)
generalfunctions.create_directory(log_path)
log_path = os.path.join(log_path, dateString+'.log')
app_name = get_app_name(log_path)
app_split = get_app_split(log_path)
pi_split = get_pi_split(log_path)
app_address = get_app_nodeaddress(log_path)
app_customKey = get_app_customKey(log_path)
app_customValue = get_app_customValue(log_path)
if os.path.exists(podcastlist_file):
data = generalfunctions.read_json(podcastlist_file, log_path)
process_file(mode, data, episode_nr, podcast_to_process)
else:
message = 'Can\'t read podcastlist: ' + podcastlist_file
print(message)
generalfunctions.log(log_path, message, True, False)
else:
podcast_to_process = "--help"
if str(podcast_to_process) == "-h" or podcast_to_process == "--help":
print ('Usage: ' + sys.argv[0] + ' VALUE|BOOST <podcastindex-id> <episode_nr> [<timestamp> <sender> <sats_total> <message>]')
except Exception as e:
message = e
message = str(e)
generalfunctions.log(log_path, message, True, False)
print(message)