public-health-ch/feedler/feedparser.py

69 lines
2 KiB
Python
Raw Normal View History

2017-07-04 08:44:22 +00:00
# -*- coding: utf-8 -*-
from datetime import datetime
2017-09-04 21:03:01 +00:00
from guess_language import guess_language
2017-07-04 08:44:22 +00:00
def parse(obj, raw, stream):
"""
Parse raw JSON implementation from the Feedly API
"""
obj.raw = raw
obj.stream = stream
obj.entry_id = raw['id']
# Date stamp handling
ts = raw['published'] / 1000
2017-07-04 09:05:58 +00:00
obj.published = datetime.fromtimestamp(ts)
2017-07-04 08:44:22 +00:00
# Authorship and title
obj.title = raw['title']
if 'author' in raw['origin']:
obj.author = raw['author']
elif 'title' in raw['origin']:
obj.author = raw['origin']['title']
# Parse links and references
if len(raw['alternate']) > 0:
obj.link = raw['alternate'][0]['href']
if 'thumbnail' in raw and len(raw['thumbnail']) > 0:
if 'url' in raw['thumbnail'][0]:
obj.visual = raw['thumbnail'][0]['url']
elif 'enclosure' in raw and len(raw['enclosure']) > 0:
if 'href' in raw['enclosure'][0]:
obj.visual = raw['enclosure'][0]['href']
elif 'visual' in raw and 'url' in raw['visual']:
obj.visual = raw['visual']['url']
2017-07-04 09:05:58 +00:00
if obj.visual.lower().strip() == 'none':
obj.visual = ''
2017-07-04 08:44:22 +00:00
# Collect text in nested JSON content
if 'content' in obj.raw:
obj.content = obj.raw['content']
else:
if 'summary' in obj.raw:
if 'content' in obj.raw['summary']:
obj.content = obj.raw['summary']['content']
else:
obj.content = obj.raw['summary']
else:
obj.content = ''
2017-09-04 21:03:01 +00:00
# Detect language
2017-09-14 07:56:38 +00:00
try:
obj.lang = guess_language(obj.content) or ''
except:
obj.lang = ''
2017-09-04 21:03:01 +00:00
2017-07-04 08:44:22 +00:00
# Collect tags
tags = []
2017-07-06 13:58:13 +00:00
if 'tags' in obj.raw:
for tag in obj.raw['tags']:
if 'label' in tag:
label = tag['label'].replace(',','-')
label = label.strip().lower()
if len(label) > 3 and not label in tags:
tags.append(label)
obj.tags = ','.join(tags)
2017-07-04 08:44:22 +00:00
return obj