sakura/manifest/manifest.py

126 lines
3.5 KiB
Python
Raw Normal View History

2019-11-29 12:11:37 +08:00
# coding=utf-8
2019-11-28 17:41:44 +08:00
'''
Created on Apr 23, 2018
Desc: Webp convertor
@author: Mashiro https://2heng.xin
'''
import os
import sys
import json
2019-12-01 18:45:46 +08:00
import requests
import base64
2019-11-28 17:41:44 +08:00
import hashlib
2019-12-04 20:35:35 +08:00
from Crypto.PublicKey import RSA
2019-11-28 17:41:44 +08:00
from PIL import Image
class Single(object):
def __init__(self, file, manifest):
self.file = file
self.mani = manifest
def hash(self):
hasher = hashlib.md5()
with open('gallary/' + self.file, 'rb') as afile:
buf = afile.read()
hasher.update(buf)
self.hash = hasher.hexdigest()
self.jpeg = 'jpeg/' + self.hash + '.jpeg'
self.webp = 'webp/' + self.hash + '.webp'
2019-12-03 21:20:42 +08:00
self.jpeg_th = 'jpeg/' + self.hash + '.th.jpeg'
self.webp_th = 'webp/' + self.hash + '.th.webp'
2019-11-28 17:41:44 +08:00
def optimize(self):
im = Image.open('gallary/' + self.file).convert('RGB')
im.save(self.jpeg, 'JPEG') # todo: TinyPNG API
im.save(self.webp, 'WEBP')
2019-11-29 12:11:37 +08:00
im.thumbnail((450, 300))
2019-12-03 21:20:42 +08:00
im.save(self.jpeg_th, 'JPEG') # todo: TinyPNG API
im.save(self.webp_th, 'WEBP')
2019-11-29 12:11:37 +08:00
2019-11-28 17:41:44 +08:00
def manifest(self):
self.mani[self.hash] = {
'source': self.file,
2019-12-03 21:20:42 +08:00
'jpeg': [self.jpeg, self.jpeg_th],
'webp': [self.webp, self.webp_th]
2019-11-28 17:41:44 +08:00
}
2019-12-04 20:35:35 +08:00
def main(self):
self.hash()
# if os.path.exists(self.jpeg) and os.path.exists(self.webp):
self.optimize()
self.manifest()
return self.mani
class Upload2Wordpress:
def __init__(self, username, password, url):
self.username = username
self.password = password
self.url = url
def upload(self, file, field):
data_string = self.username + ':' + self.password
2019-12-01 18:45:46 +08:00
token = base64.b64encode(data_string.encode()).decode('utf-8')
headers = {
'Authorization': 'Basic ' + token,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97"
}
2019-12-04 20:35:35 +08:00
files = {field: open(file, mode="rb")}
reply = requests.post(self.url, headers=headers, files=files)
2019-12-01 18:45:46 +08:00
print(json.loads(reply.content)['message'])
2019-12-04 20:35:35 +08:00
def upload_manifest(self):
print('start uploading `manifest.json`...')
self.upload('manifest.json', 'manifest')
def upload_key(self):
print('start uploading `private.key`...')
self.upload('private.key', 'rsa')
print('start uploading `public.key`...')
self.upload('public.key', 'rsa')
2019-11-28 17:41:44 +08:00
def main(self):
2019-12-04 20:35:35 +08:00
self.upload_manifest()
self.upload_key()
2019-11-28 17:41:44 +08:00
2019-12-04 20:35:35 +08:00
def gen_manifest_json():
2019-11-28 17:41:44 +08:00
onlyfiles = [f for f in os.listdir('gallary') if os.path.isfile(os.path.join('gallary', f))]
id = 1
Manifest = {}
for f in onlyfiles:
2019-12-04 20:35:35 +08:00
worker = Single(f, Manifest)
Manifest = worker.main()
print(str(id) + '/' + str(len(onlyfiles)))
id += 1
2019-11-28 17:41:44 +08:00
with open('manifest.json', 'w+') as json_file:
json.dump(Manifest, json_file)
2019-12-04 20:35:35 +08:00
def gen_key_pairs():
key = RSA.generate(1024)
pv_key_string = key.exportKey()
with open("private.key", "w+") as prv_file:
print("{}".format(pv_key_string.decode()), file=prv_file)
pb_key_string = key.publickey().exportKey()
with open("public.key", "w+") as pub_file:
print("{}".format(pb_key_string.decode()), file=pub_file)
def main():
gen_manifest_json()
if not os.path.exists("public.key") or not os.path.exists("private.key"):
print("start generating key pairs...")
gen_key_pairs()
username = input('Enter your username: ')
password = input('Enter your password: ')
url = input('Enter your rest api url: ')
upload = Upload2Wordpress(username, password, url)
upload.main()
2019-12-01 18:45:46 +08:00
2019-11-28 17:41:44 +08:00
if __name__ == '__main__':
main()
2019-12-04 20:35:35 +08:00
key = input('`manifest.json` saved. Press any key to quit.')
2019-12-03 21:27:18 +08:00
quit()