-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappimage-crawler.py
341 lines (293 loc) · 10 KB
/
appimage-crawler.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
#!/usr/bin/env python3
# pip3 install requests
import requests
import os
import json
import logging
from html.parser import HTMLParser
import argparse
import datetime
from urllib.parse import urlsplit, urlunsplit
# constants
URL_FEEDS = "https://appimage.github.io/feed.json"
HOME = os.path.expanduser("~") # this is portable across OS"s
LOCAL_CONF_DIR = os.path.join(HOME, ".config", "appimages-util")
DB = os.path.join(LOCAL_CONF_DIR, "appimages.json")
SAVE_OFTEN = True
LOG_LEVEL = logging.DEBUG
MAX_LOOPS = 0 # DEBUG ONLY. if <=0 then no limit.
VERSION = "1.0"
def init_logging():
"""
Naive logging module init
"""
global _logger
_logger = logging.getLogger("appimage-utils")
_logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
_logger.addHandler(ch)
def download_initial_db():
"""
Download an initial version of DB
@return a list (we hope)
"""
response = requests.get(URL_FEEDS)
return response.json()["items"]
def read_db():
"""
Return json data present on local db
"""
x = None
with open(DB) as in_file:
x = json.load(in_file)
return x
def create_db(_continue=False):
"""
Entry point
"""
global statistics
if not _continue:
all_apps = download_initial_db()
save_db(all_apps)
else:
all_apps = read_db()
statistics.packages = len(all_apps)
loops = 0
for app_dict in all_apps:
# es. app_dict =
# {"name": "AKASHA", "description": "Akasha platform", "categories": ["Network"],
# "authors": [{"name": "AkashaProject", "url": "https://github.com/AkashaProject"}],
# "license": None, "links": [{"type": "GitHub", "url": "AkashaProject/Community"},
# {"type": "Download", "url": "https://github.com/AkashaProject/Community/releases"}],
# "icons": ["AKASHA/icons/128x128/akasha.png"], "screenshots": ["AKASHA/screenshot.png"]}
url = None
if "links" in app_dict and app_dict["links"]:
for x in app_dict["links"]:
if x["type"] == "Download":
url = x["url"]
break
if url:
_logger.info("package " + str(app_dict["name"]))
_oldappimages = statistics.app_images
if "versions" in app_dict and _continue:
# skip package, previously elaborated
continue
loops += 1
app_dict["versions"] = search_versions(url)
if statistics.app_images > _oldappimages:
statistics.packages_with_appimages += 1
if SAVE_OFTEN:
save_db(all_apps)
if MAX_LOOPS > 0 and loops >= MAX_LOOPS:
break
_logger.info("Statistics:\n" + str(statistics))
# TODO else delete that package
save_db(all_apps)
def search_versions(url, versions=None, depth=1):
"""
Follow url searching for AppImages
@param url is a full url, not just "/hello" or "#hello"
"""
global crawled_urls, statistics
if versions is None:
versions = []
if url is not None and url != "/":
if url in crawled_urls:
return versions
_logger.info(" > crawling URL " + str(url))
crawled_urls.add(url)
try:
_logger.debug("DEBUG: init requests.head...")
response = requests.head(url, allow_redirects=True, timeout=10) # timeout in seconds
_logger.debug("DEBUG: requests.head done.")
# github link redirects to real link
# by default, 'requests' follow links in GET non in HEAD
except:
_logger.error("Cannot connect to URL '%s'" % url)
return versions
crawled_urls.add(response.url)
mimetype = get_mimetype(response)
if mimetype is not None and mimetype.startswith("text/html"):
# The given URL is an HTML Page
if depth > 0:
_logger.debug("DEBUG: init requests.get...")
response = requests.get(url, timeout=30) # timeout in seconds
_logger.debug("DEBUG: requests.get done.")
parser = AppImageHTMLParser(url, versions, depth)
parser.feed(response.text)
else:
# The given URL could be a downloadable file
props = guess_appimage_properties(url, response)
if props is not None:
_logger.info(" > found AppImage!")
if props not in versions:
versions.append(props)
statistics.app_images += 1
_logger.info(" > END crawling URL " + str(url))
return versions
class AppImageHTMLParser(HTMLParser):
"""
Follow links in HTML page
"""
def __init__(self, url, versions, depth):
"""
@param versions a set of already-found files
@param depth depth of search algorithm
"""
super().__init__()
self.versions = versions
self.depth = depth
self.url = url
def handle_starttag(self, tag, attrs):
"""
Executed when any tag begins
"""
if tag != "a": # Hope it's case-insensitive
return
url2 = self.get_attr(attrs, "href")
if url2 and url2 != "/" and not url2.startswith("#"):
if url2.startswith("/"):
url2 = url_absolute(url2, self.url)
search_versions(url2, self.versions, self.depth-1)
def get_attr(self, attrs, attr_name):
"""
Return first occurrence of given attr
"""
for (attr, value) in attrs:
if attr == attr_name:
return value
def guess_appimage_properties(url, response):
"""
return a record of AppImage properties, or None
@param url Gihubb download URL
"""
# TODO allow tar/zip archives
if url.startswith("https://github.com") and "/releases/" in url:
filename = url[url.rfind("/")+1:]
os = None
# os values as in sys.platform
if filename.endswith(".dmg"):
os = "darwin"
elif filename.endswith(".exe"):
os = "win32"
elif filename.endswith("AppImage"):
os = "linux"
if os is not None:
return {
"url": url,
"os": os,
"filesize": get_filesize(response)
}
return None
def url_absolute(path, parent_url):
"""
(/newpath, http://server/oldpath) => http://server/newpath
"""
parts = urlsplit(parent_url)
return urlunsplit([parts[0], parts[1], path, None, None])
def url_remove_fragment(url):
"""
http://one/two#three => http://one/two
"""
index = url.find("#")
if index > 0:
return url[0:index]
else:
return url
def url_get_parent(url):
"""
http://one/two/three => http://one/two
http://one => http://one
"""
index = url.rfind("/")
if index > 8: # avoid https://
return url[0:index]
else:
return url
def get_mimetype(response):
"""
Extract MIME type from given response (usually a HEAD request), or None
"""
try:
return response.headers["Content-Type"]
except: # KeyError
return None
def get_filesize(response):
"""
Extract file size from given response (usually a HEAD request), or None
"""
try:
return int(response.headers["Content-Length"])
except: # KeyError, ValueError
return None
def create_parent_dir(filename):
"""
Create parent directory
"""
parentdir = os.path.dirname(filename)
if parentdir:
os.makedirs(parentdir, exist_ok=True)
def save_db(all_apps):
"""
Save all_apps to disk, using DB as filename
"""
_logger.debug("Saving to database " + DB)
create_parent_dir(DB)
with open(DB, "w") as outfile:
json.dump(all_apps, outfile)
def parse_cli_args():
"""
Parse CLI aguments, return an object containing all of them
"""
parser = argparse.ArgumentParser(description="(Re)generate apps database." +
"DB is by default in " + DB)
parser.add_argument("-v", "--version", action="version", version="%(prog)s " + VERSION)
parser.add_argument("--db", help="custom DB location (optional)")
parser.add_argument("--max-loops", type=int, help="a limit on number of packages (for debug purposes)")
parser.add_argument("--continue", action="store_true", dest="_continue",
help="do not download, continue a previous build")
args = parser.parse_args()
return args
class Statistics:
"""
Plain object, store some packages statistics
"""
def __init__(self):
self.packages = 0
self.app_images = 0
self.packages_with_appimages = 0
self.start_time = datetime.datetime.now()
def __str__(self):
global crawled_urls
return "Tot.packages:\t\t\t\t" + str(self.packages) + "\n" + \
"Crawled URL's:\t\t\t\t" + str(len(crawled_urls)) + "\n" + \
"AppImage's found:\t\t\t" + str(self.app_images) + "\n" + \
"Packages with at least one AppImage:\t" + str(self.packages_with_appimages) + "\n" + \
"Time elapsed:\t\t\t\t" + str(self.time_elapsed()) + "h.\n"
def time_elapsed(self):
"""
Return minutes elapsed since self.start_time
"""
end_time = datetime.datetime.now()
difference = end_time - self.start_time
return (int)(difference.total_seconds() // 3600)
# global variables
crawled_urls = set()
_logger = None
statistics = Statistics()
if __name__ == "__main__":
args = parse_cli_args()
init_logging()
_logger.debug("CLI arguments: " + str(args))
if args.db:
DB = args.db
if args.max_loops:
MAX_LOOPS = args.max_loops
_logger.info("Begin search...")
try:
create_db(args._continue)
finally:
# Statistics will be printed also in case of exceptions
_logger.info("Statistics:\n" + str(statistics))
_logger.info("Done.")