26def response(resp):
27 results = []
28
29
30
31 json_str = ""
32 for line in resp.text.split("\n"):
33 if line.startswith("[{"):
34 json_str = line
35 break
36
37 for result in loads(json_str):
38 thumbnail = None
39
40 attachments = result.get('media_attachments', [])
41 images = [attachment['preview_url'] for attachment in attachments if attachment['type'] == 'image']
42 if len(images) > 0:
43 thumbnail = images[0]
44
45 title = result.get('card', {}).get('title')
46 if not title:
47 title = html_to_text(result['content'])[:75]
48
49 results.append(
50 {
51 'url': result['url'],
52 'title': title,
53 'content': html_to_text(result['content']),
54 'thumbnail': thumbnail,
55 'publishedDate': datetime.strptime(result['created_at'], '%Y-%m-%d %H:%M:%S'),
56 }
57 )
58
59 return results