33def response(resp):
34 results = []
35
36 doc = html.fromstring(resp.text)
37
38 images = eval_xpath_list(doc, '//a[starts-with(@href, "/doc")]//img')
39
40 result_index = 0
41 for result in eval_xpath_list(doc, '//script[@type="text/javascript"]'):
42 info_js = extr(extract_text(result), '] = ', '};') + '}'
43
44 if not info_js:
45 continue
46
47 try:
48 info_item = loads(info_js)
49
50 if not info_item.get('mediakey'):
51 continue
52
53 thumbnail_src = extract_text(eval_xpath(images[result_index], './@src'))
54 img_src = thumbnail_src.replace('240.jpg', '640.jpg')
55
56 resolution = None
57 if info_item.get("width") and info_item.get("height"):
58 resolution = f'{info_item["width"]}x{info_item["height"]}'
59
60 item = {
61 'template': 'images.html',
62 'url': f"{base_url}/doc/{info_item['user_id']}/{info_item['doc_id']}",
63 'title': info_item.get('title'),
64 'content': info_item.get('content', ''),
65 'resolution': resolution,
66 'publishedDate': datetime.fromtimestamp(int(info_item['posted_at'])),
67 'thumbnail_src': thumbnail_src,
68 'img_src': img_src,
69 }
70 results.append(item)
71
72 result_index += 1
73 except JSONDecodeError:
74 continue
75
76 return results