2020-04-18 11:45:44 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2019-11-03 23:16:25 +00:00
|
|
|
# Copyright (C) 2017-2019 Tobias Gruetzmacher
|
2017-10-12 22:34:37 +00:00
|
|
|
import gzip
|
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
|
2020-02-03 00:03:31 +00:00
|
|
|
from functools import lru_cache
|
2017-10-12 22:34:37 +00:00
|
|
|
|
2019-12-01 21:36:49 +00:00
|
|
|
from responses import add, GET
|
2017-10-12 22:34:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _file(name):
|
2019-12-01 21:36:49 +00:00
|
|
|
return os.path.join(os.path.dirname(__file__), 'responses', name)
|
2017-10-12 22:34:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@lru_cache()
|
2020-07-31 18:14:04 +00:00
|
|
|
def content(name):
|
2017-10-12 22:34:37 +00:00
|
|
|
with gzip.open(_file(name + '.html.gz'), 'r') as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache()
|
2020-01-04 14:51:01 +00:00
|
|
|
def _img(name):
|
|
|
|
with open(_file(name + '.png'), 'rb') as f:
|
2017-10-12 22:34:37 +00:00
|
|
|
return f.read()
|
|
|
|
|
|
|
|
|
2019-12-01 21:36:49 +00:00
|
|
|
def page(url, pagename):
|
2020-07-31 18:14:04 +00:00
|
|
|
add(GET, url, content(pagename))
|
2017-10-12 22:34:37 +00:00
|
|
|
|
|
|
|
|
2020-01-04 14:51:01 +00:00
|
|
|
def png(url, name='empty'):
|
|
|
|
add(GET, url, _img(name), content_type='image/jpeg')
|
2017-10-12 22:34:37 +00:00
|
|
|
|
|
|
|
|
2020-01-04 14:51:01 +00:00
|
|
|
def jpeg(url, name='empty'):
|
|
|
|
add(GET, url, _img(name), content_type='image/jpeg')
|
2019-11-03 23:16:25 +00:00
|
|
|
|
|
|
|
|
2019-12-01 21:36:49 +00:00
|
|
|
def xkcd():
|
|
|
|
page('https://xkcd.com/', 'xkcd-1899')
|
|
|
|
for num in (302, 303, 1898, 1899):
|
|
|
|
page('https://xkcd.com/%i/' % num, 'xkcd-%i' % num)
|
|
|
|
png(re.compile(r'https://imgs\.xkcd\.com/.*\.png'))
|