-
Notifications
You must be signed in to change notification settings - Fork 0
/
Upload.py
78 lines (70 loc) · 2.67 KB
/
Upload.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
from subprocess import call
from Config import Config
from Download import Download
import json
import hashlib
class Upload:
def __init__(self,
file_name=None,
keep_name=True,
description=None,
config=Config()):
self.file_name = file_name
self.keep_name = keep_name
self.description = description
self.config = config
def hash_file(self, filename):
h = hashlib.sha1()
with open(filename, 'rb') as f:
chunk = 0
while chunk != b'':
chunk = f.read(1024)
h.update(chunk)
return h.hexdigest()
def file_exists(self, file_path):
hash_local = self.hash_file(file_path)
download = Download(
("https://commons.wikimedia.org/w/api.php?action=query&list"
"=allimages&format=json&aisha1=") + hash_local, as_var=True)
if(download.perform()):
content = download.get_result().getvalue()
json_data = json.loads(content)
if(len(json_data["query"]["allimages"]) > 0):
return True
else:
return False
def perform(self):
"""Do the upload, dont forget the params should be set"""
self.convert()
file_path = self.config.get_image_folder() + "/" + self.file_name
if(not self.file_exists(file_path)):
print "Start upload"
run_process = ["python3",
"pywikibot/pwb.py",
"upload",
file_path,
"-keep",
"-noverify",
self.description.get_desc()]
print run_process
subProc = call(run_process)
print "End Upload"
else:
print "File Existed in Commons"
def convert(self):
"""If the file is NEF convert it to tiff so it is Commons Compatible"""
if(self.file_name.split(".")[-1] in ["NEF", "Nef", "nef"]):
input_file = self.config.get_image_folder() + "/" + self.file_name
self.file_name = "".join(
set(self.file_name.split(".")
[0:len(self.file_name.split(".")) - 1])) + '.jpeg'
output_file_path = self.config.get_image_folder() + "/" + \
self.file_name
run_process = ["ufraw-batch", "--out-type=jpeg",
"--output=" + output_file_path, "--overwrite",
input_file]
subprocess = call(run_process)
else:
pass
# up = Upload("iss040e112792.NEF")
# up.perform()