-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublish-to-fte.py
461 lines (297 loc) · 13.4 KB
/
publish-to-fte.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from wordpress_xmlrpc import WordPressPost
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts,media
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import taxonomies
from urllib.request import urlopen
import urllib
import shutil
import unittest, time, re
import os
import re
import yaml
import sys
import time
import uuid
import shutil
import json
import argparse
import requests
parser = argparse.ArgumentParser()
parser.add_argument('-l','--local-file', help='Use Local File', required=False)
parser.add_argument('-r','--remote-book-url', help='Use Remote File', required=False)
parser.add_argument('-i','--interactive', help='Interactive Mode', required=False)
parser.add_argument('-bn','--book_number', help='Book Number', required=True)
args = parser.parse_args()
local_file = args.local_file
remote_book_url = args.remote_book_url
interactive = args.interactive
book_number = args.book_number
home = os.getenv("HOME")
fte_yaml = yaml.load(open(home + '/.config/fte-login.yaml'))
fte_username = fte_yaml['username']
fte_password = fte_yaml['password']
git_username = fte_yaml['git_username']
git_password = fte_yaml['git_password']
android_push_Authorization = fte_yaml['android_push_Authorization']
if remote_book_url:
book_url = remote_book_url
print("Getting Book Info")
book_info = yaml.load(urlopen(book_url.replace('details','download') + '/book-info.yaml'))
book_title = book_info['book_title']
print(book_title)
book_title_in_english = book_info['book_title_in_english'].replace(" ","-")
author = book_info['author']
print(author)
category = book_info['category']
print(category)
if book_info['author_mail']:
author_mail = book_info['author_mail']
else:
author_mail = " "
cover_image = book_info['cover_image']
if book_info['artist']:
artist = book_info['artist']
else:
artist = ""
if book_info['artist_email']:
artist_email = book_info['artist_email']
else:
artist_email = ""
if book_info['translator']:
translator = book_info['translator']
else:
translator = ""
if book_info['translator_email']:
translator_email = book_info['translator_email']
else:
translator_email = ""
ebook_maker = book_info['ebook_maker']
ebook_maker_email = book_info['ebook_maker_email']
license = book_info['license']
epub_url = book_url.replace('details','download') + "/" + book_title_in_english + ".epub"
mobi_url = book_url.replace('details','download') + "/" + book_title_in_english + ".mobi"
a4_pdf_url = book_url.replace('details','download') + "/" + book_title_in_english + ".pdf"
six_inch_pdf_url = book_url.replace('details','download') + "/" + book_title_in_english + "-6-inch.pdf"
cover_url = book_url.replace('details','download') + "/" + cover_image
if interactive == "yes":
book_title = input("Book Title : ").strip()
book_title_in_english = input("Book Title in English : ").strip()
book_url = input("Book Archive URL : ").strip()
author = input("Author : ").strip()
author_mail = input("Author Mail : ").strip()
artist = input("Artist : ").strip()
artist_email = input("Artist Email : ").strip()
translator = input("Translator : ").strip()
translator_email = input("Translator Email : ").strip()
ebook_maker = input("Ebook made by : ").strip()
ebook_maker_email = input("Ebook Maker email : ").strip()
license = input("License : ").strip()
category = input("category : ").strip()
cover_url = input("Cover Image URL : ").strip()
epub_url = input("Epub URL : ").strip()
mobi_url = input("Mobi URL : ").strip()
a4_pdf_url = input("A4 PDF URL : ").strip()
six_inch_pdf_url = input("Six inch PDF URL : ")
cover_image = book_title_in_english + "_cover.jpg"
print("Logging in to FreeTamilEbooks.com")
driver = webdriver.Firefox(executable_path=r'./geckodriver')
driver.implicitly_wait(5)
driver.maximize_window()
base_url = "http://freetamilebooks.com"
verificationErrors = []
accept_next_alert = True
driver.get("http://freetamilebooks.com/wp-login.php?redirect_to=http%3A%2F%2Ffreetamilebooks.com%2Fwp-admin%2Fpost-new.php%3Fpost_type%3Ddlm_download&reauth=1&jetpack-sso-show-default-form=1")
driver.implicitly_wait(5)
# driver.find_element_by_link_text("Log in with username and password").click()
driver.find_element_by_id("user_pass").clear()
driver.find_element_by_id("user_pass").send_keys(fte_password)
driver.find_element_by_id("user_login").clear()
driver.find_element_by_id("user_login").send_keys(fte_username)
driver.find_element_by_id("wp-submit").click()
# driver.find_element_by_link_text("Download").click()
driver.implicitly_wait(5)
def add_download(filename,file_url):
driver.get("http://freetamilebooks.com/wp-admin/post-new.php?post_type=dlm_download")
driver.find_element_by_id("title").clear()
driver.find_element_by_id("title").send_keys(filename)
driver.find_element_by_xpath(".//*[@class='button plus add_file']").click()
# driver.implicitly_wait(30)
time.sleep(20)
# driver.find_element_by_xpath(".//*[@class='downloadable_file_urls']").click()
driver.find_element_by_xpath(".//*[@class='downloadable_file_urls']").send_keys(file_url)
# driver.implicitly_wait(30)
time.sleep(20)
publish_button = WebDriverWait(driver, 40).until(
EC.presence_of_element_located((By.XPATH, ".//*[@id='publish']"))
)
publish_button.click()
# driver.find_element_by_xpath(".//*[@id='publish']").click()
# driver.implicitly_wait(30)
time.sleep(20)
dl_text = WebDriverWait(driver, 40).until(
EC.presence_of_element_located((By.ID, "dlm-info-id"))
)
dl_id = dl_text.get_attribute('value')
# dl_id = driver.find_element_by_id('dlm-info-id').get_attribute('value')
# driver.implicitly_wait(30)
time.sleep(20)
dl_url = driver.find_element_by_id('dlm-info-url').get_attribute('value')
print(dl_id)
return(dl_id,dl_url)
print("Adding Downloads")
epub_data = add_download(book_title + " epub",epub_url )
mobi_data = add_download(book_title + " mobi",mobi_url)
a4_pdf_data = add_download(book_title + " A4 PDF",a4_pdf_url)
six_inch_pdf_data = add_download(book_title + " 6 inch PDF",six_inch_pdf_url)
driver.quit()
#epub_data = (1,"a")
#mobi_data = (1,"a")
#a4_pdf_data = (1,"a")
#six_inch_pdf_data = (1,"a")
#print(book_title_in_english)
#For security, consider creating a user just for your script.
wp = Client('http://freetamilebooks.com/xmlrpc.php', fte_username, fte_password)
post = WordPressPost()
taxes = wp.call(taxonomies.GetTaxonomies())
print(taxes)
with urllib.request.urlopen(cover_url) as response, open(cover_image, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
print("Downloaded Cover Image")
filename = cover_image
data = {
'name': filename,
'type': 'image/jpeg', # mimetype
}
# read the binary file and let the XMLRPC library encode it into base64
with open(filename, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = wp.call(media.UploadFile(data))
print(response)
image_url = response["url"]
attachment_id = response['id']
print("Uploaded cover Image")
medium_image_width = str(response["metadata"]["sizes"]["medium"]["width"])
medium_image_height = str(response["metadata"]["sizes"]["medium"]["height"])
medium_image_url = image_url.replace('.jpg','-' + str(medium_image_width) +'x' + str(medium_image_height) + '.jpg')
content = "நூல் : " + book_title + "\n\n" + "ஆசிரியர் : " + author + "\n\n" + "<a href=" + image_url + "><img class='alignright size-medium wp-image-6958' src=" + medium_image_url + " width='" + medium_image_width + "' height='" + medium_image_height +"' /></a>" + "\n"
print("Generating Content to Post")
if author_mail :
content = content + "மின்னஞ்சல் : " + author_mail + "\n\n"
if artist:
content = content + "அட்டைப்படம் : " + artist + "\n\n"
if artist_email:
content = content + artist_email + "\n\n"
if translator:
content = content + "தமிழாக்கம் : " + translator + "\n\n"
if translator_email:
content = content + translator_email + "\n\n"
content = content + "மின்னூலாக்கம் : " + ebook_maker + "\n"
content = content + "மின்னஞ்சல் : " + ebook_maker_email + "\n\n"
content = content + "வெளியிடு : FreeTamilEbooks.com" + "\n\n"
content = content + "உரிமை : " + license + "\n\n" + "உரிமை – கிரியேட்டிவ் காமன்ஸ். எல்லாரும் படிக்கலாம், பகிரலாம்." + "\n"
extra = '''
<b>பதிவிறக்க*</b>
ஆன்ட்ராய்டு(FBreader), ஆப்பிள், புது நூக் கருவிகளில் படிக்க
[download id=" ''' + str(epub_data[0]) + '''" template="button"]
புது கிண்டில் கருவிகளில் படிக்க
[download id=" ''' + str(mobi_data[0]) + '''" template="button"]
குனூ/லினக்ஸ், விண்டோஸ் கணிணிகளில் படிக்க
[download id=" ''' + str(a4_pdf_data[0]) + '''" template="button"]
பழைய கிண்டில்,நூக் கருவிகளில் படிக்க
[download id=" ''' + str(six_inch_pdf_data[0]) + '''" template="button"]
<a>Send To Kindle Directly</a><a href="http://35.166.185.40/send2kindle?fileurl=''' + str(mobi_data[1]) + '''&filename=''' +book_title_in_english + '''.mobi" target="_blank" rel="noopener"><img class="alignleft wp-image-6127 " src="http://freetamilebooks.com/wp-content/uploads/2017/09/send-to-kindle-logo.jpg" alt="" width="579" height="129" /></a>
பிற வடிவங்களில் படிக்க – ''' + book_url + '''
புத்தக எண் - ''' + str(book_number) + '''
'''
content = content + extra
post.title = book_title + " - " + category + " - " + author
post.slug = book_title_in_english
post.post_type = 'ebooks'
post.content = content
post.thumbnail = attachment_id
post.comment_status = 'open'
post.post_status = 'publish'
#post.post_status = 'draft'
if artist:
post.terms_names = {
'genres': [category],
'contributors': [artist, ebook_maker],
'authors' : [author]
}
else:
post.terms_names = {
'genres': [category],
'contributors': [ebook_maker],
'authors' : [author]
}
wp.call(NewPost(post))
print("Published")
book_name = book_title
bookid = str(uuid.uuid4())
cover_image = image_url
gen = category
epub_link = epub_data[1]
authors = author
url = "http://freetamilebooks.com/ebooks/" + book_title_in_english
xml_content = "\n\n" + "<book>" +"\n" + "<bookid>" + bookid + "</bookid>" + "\n" + "<title>" + book_name + "</title>" + "\n"
xml_content = xml_content + "<author>" + authors + "</author>" + "\n"
xml_content = xml_content + "<image>" + cover_image + "</image>" + "\n"
xml_content = xml_content + "<link>" + url + "</link>" + "\n"
xml_content = xml_content + "<epub>" + epub_link + "</epub>" + "\n"
xml_content = xml_content + "<pdf />" + "\n"
xml_content = xml_content + "<category>" + gen + "</category>" + "\n"
xml_content = xml_content + "<date />" + "\n" + "</book>" + "\n"
#print(xml_content)
print("Adding into booksdb.xml\n")
git_clone = "git clone https://" + git_username +":" + git_password + "@github.com/kishorek/Free-Tamil-Ebooks fte_repo"
os.system(git_clone)
temp = open('temp', 'w')
with open('fte_repo/booksdb.xml','r') as f:
for line in f:
if line.startswith('<books>'):
line = line + xml_content
temp.write(line)
temp.close()
shutil.move('temp', 'fte_repo/booksdb.xml')
os.chdir('fte_repo')
git_add = "git add booksdb.xml"
os.system(git_add)
git_commit = "git commit -m 'added a book'"
os.system(git_commit)
git_push = "git push origin master"
os.system(git_push)
os.chdir('../')
os.system("rm -rf fte_repo")
print("Sending Push Notification to Android Devices")
title = "புது மின்னூல் - " + book_title
text = "FreeTamilEbooks ன் புதிய மின்னூல் வெளியிடப்பட்டுள்ளது. " + book_title + " " + author + ". இன்றே படியுங்கள். "
headers = {'Authorization': android_push_Authorization}
headers = json.dumps(headers,ensure_ascii=False)
header_json = json.loads(headers)
data = {'notification':{'title': title, 'text': text }, 'data' : { 'keyname': 'shrini' },'to' : '/topics/fte_books'}
data = json.dumps(data,ensure_ascii=False)
data_json = json.loads(data)
api = "https://fcm.googleapis.com/fcm/send"
r = requests.post(api, json=data_json, headers=header_json)
#print(r.json())
print("Done")