-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikimediaDownloader.py
More file actions
244 lines (140 loc) · 5.66 KB
/
Copy pathWikimediaDownloader.py
File metadata and controls
244 lines (140 loc) · 5.66 KB
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
import os
import re
from pdf2image import convert_from_path
import urllib
import requests
import xmltodict, json
import html2text
import PIL.Image
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
PIL.Image.MAX_IMAGE_PIXELS = None
G_H2T = html2text.HTML2Text()
G_H2T.ignore_links = True
DEFAULT_WIDTH = 100
class DownloadException(Exception):
pass
class RequestedWidthBiggerThanSourceException(DownloadException):
pass
def MakeThumbnailName(ImageName, Extension):
FileName, _ = os.path.splitext(ImageName)
if "jpeg" in Extension:
return FileName + ".jpg"
else:
return FileName + "." + Extension
def MakeWikimediaRequest(URL,ImageName,TextMode=False):
Response = requests.get(URL, headers={'User-Agent': 'Python urllib'}) #headers
try:
ExtensionType = Response.headers['Content-Type']
if "/" not in ExtensionType:
raise Exception("Could not find slash for extension: "+ExtensionType)
Extension = ExtensionType[ExtensionType.find('/')+1:]
if ";" in Extension:
Extension = Extension[:Extension.find(';')]
if TextMode is False:
return Response.content, MakeThumbnailName(ImageName, Extension)
else:
return Response.text, MakeThumbnailName(ImageName, Extension)
except Exception as e:
raise Exception(e)
def GetThumbnailOfFileReq(ImageName, Width):
URL = "http://commons.wikimedia.org/w/index.php?title=Special:FilePath&file=%s&width=%s" % (urllib.parse.quote(ImageName), Width)
return MakeWikimediaRequest(URL,ImageName)
def GetFullSizeFileReq(ImageName):
URL = "http://commons.wikimedia.org/w/index.php?title=Special:FilePath&file=%s" % (ImageName)
return MakeWikimediaRequest(URL,ImageName)
def GetMetaDataImageReq(ImageName):
URL = "https://tools.wmflabs.org/magnus-toolserver/commonsapi.php?image=%s" % (ImageName)
return MakeWikimediaRequest(URL,ImageName,True)
#response->licenses->license[iterable]->name
#response->file->author
# Image by [Name], distributed under [licence]
def ExtractWikimediaXMLMetadata(IncomingXMLAsString):
JSONStringFromXML = json.dumps(xmltodict.parse(IncomingXMLAsString))
JSONFromXML = json.loads(JSONStringFromXML)
if JSONFromXML["response"]["licenses"] is None:
Licence = None
elif JSONFromXML["response"]["licenses"]["license"] is None:
Licence = None
elif type(JSONFromXML["response"]["licenses"]["license"]) is dict:
Licence = JSONFromXML["response"]["licenses"]["license"]["name"]
elif type(JSONFromXML["response"]["licenses"]["license"]) is list:
Licence = JSONFromXML["response"]["licenses"]["license"][0]["name"]
else:
Licence = "Unknown type"
print("Unknown type "+str(type(JSONFromXML["response"]["licenses"]["license"])))
AuthorTemp = JSONFromXML["response"]["file"]["author"]
Author = G_H2T.handle(AuthorTemp)
return Author, Licence
#Position is a bracketed input of (X,Y), EG (10,20)
#Fill is a bracketed input of (R,B,G,[A]), EG (255,100,0,0)
def WriteTextToImage(ImageFilename,Text,CaptionPosition=(0,0),TextColour=(0,0,0,0),RectangleColour=(255,255,255,255)):
CleanedText = Text.replace("\n", "")
print(ImageFilename)
TempImage = Image.open(ImageFilename)
ImageWidth, ImageHeight = TempImage.size
FontSizeIter = 1
HalvedWidth = ImageWidth/2
while True:
TempFont = ImageFont.truetype("LeagueMono-Regular.ttf",FontSizeIter)
TextWidth, TextHeight = TempFont.getsize(CleanedText)
if TextWidth > HalvedWidth:
break
FontSizeIter += 1
FontSizeIter -= 1
TempFont = ImageFont.truetype("LeagueMono-Regular.ttf",FontSizeIter)
TextWidth, TextHeight = TempFont.getsize(Text)
BoxSize = (TextWidth, TextHeight)
BoxImage = Image.new('RGBA', BoxSize, RectangleColour)
TempImage.paste(BoxImage, CaptionPosition)
TempDraw = ImageDraw.Draw(TempImage)
TempDraw.text(CaptionPosition, CleanedText, font=TempFont, fill=TextColour)
TempImage.save(ImageFilename)
def PDFToImage(PDFFilename,LicenceText):
BaseFilename = PDFFilename[:PDFFilename.rfind('.')]
PDFPages = convert_from_path(PDFFilename, 500)
Iter = 0
for APage in PDFPages:
try:
JPGFilename = BaseFilename+"_"+str(Iter)+".jpg"
APage.save(JPGFilename,'JPEG')
WriteTextToImage(JPGFilename,LicenceText)
except Exception as e:
print("PDFToImage: Skipped page "+str(Iter)+" due to an error: "+str(e))
Iter += 1
os.remove(PDFFilename)
return JPGFilename
def DownloadWikimediaImage(ImageName, OutputPath, Width=None):
ImageName = ImageName.strip().replace(' ', '_')
XMLContent, OutputXMLName = GetMetaDataImageReq(ImageName)
try:
Author, Licence = ExtractWikimediaXMLMetadata(XMLContent)
except Exception as e:
raise Exception(e)
if Author is not None and Licence is not None:
LicenceText = "Image by "+Author+", distributed under a "+Licence+" license."
elif Author is not None and Licence is None:
LicenceText = "Image by "+Author
elif Author is None and Licence is not None:
LicenceText = "Distributed under a "+Licence+" license."
else:
LicenceText = None
try:
if isinstance(Width,int):
ImageContent, OutputFileName = GetThumbnailOfFileReq(ImageName, Width)
else:
ImageContent, OutputFileName = GetFullSizeFileReq(ImageName)
except Exception as e:
raise Exception(e)
OutputFilePath = os.path.join(OutputPath, OutputFileName)
OutputFilePathExt = OutputFilePath[OutputFilePath.rfind('.')+1:]
try:
print("Writing image "+str(ImageName))
with open(OutputFilePath, 'wb') as f:
f.write(ImageContent)
if LicenceText is not None:
if "pdf" in OutputFilePathExt:
PDFToImage(OutputFilePath,LicenceText)
else:
WriteTextToImage(OutputFilePath,LicenceText)
except Exception as e:
raise Exception(e)