2017-10-23 09:58:41 +00:00
|
|
|
"""
|
|
|
|
Everything related to parsing links from bookmark services.
|
|
|
|
|
|
|
|
For a list of supported services, see the README.md.
|
|
|
|
For examples of supported files see examples/.
|
|
|
|
|
|
|
|
Parsed link schema: {
|
|
|
|
'url': 'https://example.com/example/?abc=123&xyc=345#lmnop',
|
|
|
|
'domain': 'example.com',
|
|
|
|
'base_url': 'example.com/example/',
|
|
|
|
'timestamp': '15442123124234',
|
|
|
|
'tags': 'abc,def',
|
|
|
|
'title': 'Example.com Page Title',
|
|
|
|
'sources': ['ril_export.html', 'downloads/getpocket.com.txt'],
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2017-07-04 10:38:07 +00:00
|
|
|
import re
|
|
|
|
import json
|
2018-03-14 01:11:11 +00:00
|
|
|
import xml.etree.ElementTree as etree
|
2017-10-23 09:58:41 +00:00
|
|
|
|
2017-07-04 10:38:07 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
from util import (
|
|
|
|
domain,
|
|
|
|
base_url,
|
2017-10-23 09:58:41 +00:00
|
|
|
str_between,
|
2017-10-18 22:38:17 +00:00
|
|
|
get_link_type,
|
|
|
|
)
|
|
|
|
|
2017-07-04 10:38:07 +00:00
|
|
|
|
2017-10-23 09:58:41 +00:00
|
|
|
def get_parsers(file):
|
|
|
|
"""return all parsers that work on a given file, defaults to all of them"""
|
|
|
|
|
|
|
|
return {
|
|
|
|
'pocket': parse_pocket_export,
|
|
|
|
'pinboard': parse_json_export,
|
|
|
|
'bookmarks': parse_bookmarks_export,
|
|
|
|
'rss': parse_rss_export,
|
2018-03-14 01:11:11 +00:00
|
|
|
'pinboard_rss': parse_pinboard_rss_feed,
|
2018-03-14 02:23:52 +00:00
|
|
|
'medium_rss': parse_medium_rss_feed,
|
2017-10-23 09:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def parse_links(path):
|
2017-07-04 10:38:07 +00:00
|
|
|
"""parse a list of links dictionaries from a bookmark export file"""
|
2017-10-18 22:38:17 +00:00
|
|
|
|
|
|
|
links = []
|
|
|
|
with open(path, 'r', encoding='utf-8') as file:
|
2017-10-23 09:58:41 +00:00
|
|
|
for parser_func in get_parsers(file).values():
|
2017-10-18 22:38:17 +00:00
|
|
|
# otherwise try all parsers until one works
|
|
|
|
try:
|
|
|
|
links += list(parser_func(file))
|
|
|
|
if links:
|
|
|
|
break
|
2018-03-14 21:38:39 +00:00
|
|
|
except (ValueError, TypeError, IndexError, etree.ParseError):
|
2017-10-23 09:58:41 +00:00
|
|
|
# parser not supported on this file
|
2017-10-18 22:38:17 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
return links
|
2017-07-04 10:38:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_pocket_export(html_file):
|
|
|
|
"""Parse Pocket-format bookmarks export files (produced by getpocket.com/export/)"""
|
|
|
|
|
|
|
|
html_file.seek(0)
|
2017-10-23 09:58:41 +00:00
|
|
|
pattern = re.compile("^\\s*<li><a href=\"(.+)\" time_added=\"(\\d+)\" tags=\"(.*)\">(.+)</a></li>", re.UNICODE)
|
2017-07-04 10:38:07 +00:00
|
|
|
for line in html_file:
|
2017-10-23 09:58:41 +00:00
|
|
|
# example line
|
|
|
|
# <li><a href="http://example.com/ time_added="1478739709" tags="tag1,tag2">example title</a></li>
|
2017-07-04 10:38:07 +00:00
|
|
|
match = pattern.search(line)
|
|
|
|
if match:
|
|
|
|
fixed_url = match.group(1).replace('http://www.readability.com/read?url=', '') # remove old readability prefixes to get original url
|
2017-10-18 22:38:17 +00:00
|
|
|
time = datetime.fromtimestamp(float(match.group(2)))
|
2017-07-04 10:38:07 +00:00
|
|
|
info = {
|
|
|
|
'url': fixed_url,
|
2017-10-18 22:38:17 +00:00
|
|
|
'domain': domain(fixed_url),
|
|
|
|
'base_url': base_url(fixed_url),
|
|
|
|
'timestamp': str(time.timestamp()),
|
2017-07-04 10:38:07 +00:00
|
|
|
'tags': match.group(3),
|
2017-10-18 22:38:17 +00:00
|
|
|
'title': match.group(4).replace(' — Readability', '').replace('http://www.readability.com/read?url=', '') or base_url(fixed_url),
|
|
|
|
'sources': [html_file.name],
|
2017-07-04 10:38:07 +00:00
|
|
|
}
|
|
|
|
info['type'] = get_link_type(info)
|
|
|
|
yield info
|
|
|
|
|
|
|
|
def parse_json_export(json_file):
|
|
|
|
"""Parse JSON-format bookmarks export files (produced by pinboard.in/export/)"""
|
|
|
|
|
|
|
|
json_file.seek(0)
|
|
|
|
json_content = json.load(json_file)
|
|
|
|
for line in json_content:
|
2017-10-23 09:58:41 +00:00
|
|
|
# example line
|
|
|
|
# {"href":"http:\/\/www.reddit.com\/r\/example","description":"title here","extended":"","meta":"18a973f09c9cc0608c116967b64e0419","hash":"910293f019c2f4bb1a749fb937ba58e3","time":"2014-06-14T15:51:42Z","shared":"no","toread":"no","tags":"reddit android"}]
|
2017-07-04 10:38:07 +00:00
|
|
|
if line:
|
|
|
|
erg = line
|
2017-10-18 22:38:17 +00:00
|
|
|
time = datetime.strptime(erg['time'].split(',', 1)[0], '%Y-%m-%dT%H:%M:%SZ')
|
2017-07-04 10:38:07 +00:00
|
|
|
info = {
|
|
|
|
'url': erg['href'],
|
2017-10-18 22:38:17 +00:00
|
|
|
'domain': domain(erg['href']),
|
|
|
|
'base_url': base_url(erg['href']),
|
|
|
|
'timestamp': str(time.timestamp()),
|
2017-07-04 10:38:07 +00:00
|
|
|
'tags': erg['tags'],
|
|
|
|
'title': erg['description'].replace(' — Readability', ''),
|
2017-10-18 22:38:17 +00:00
|
|
|
'sources': [json_file.name],
|
2017-07-04 10:38:07 +00:00
|
|
|
}
|
|
|
|
info['type'] = get_link_type(info)
|
|
|
|
yield info
|
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
def parse_rss_export(rss_file):
|
|
|
|
"""Parse RSS XML-format files into links"""
|
|
|
|
|
|
|
|
rss_file.seek(0)
|
|
|
|
items = rss_file.read().split('</item>\n<item>')
|
|
|
|
for item in items:
|
|
|
|
# example item:
|
|
|
|
# <item>
|
|
|
|
# <title><![CDATA[How JavaScript works: inside the V8 engine]]></title>
|
|
|
|
# <category>Unread</category>
|
|
|
|
# <link>https://blog.sessionstack.com/how-javascript-works-inside</link>
|
|
|
|
# <guid>https://blog.sessionstack.com/how-javascript-works-inside</guid>
|
|
|
|
# <pubDate>Mon, 21 Aug 2017 14:21:58 -0500</pubDate>
|
|
|
|
# </item>
|
|
|
|
|
|
|
|
trailing_removed = item.split('</item>', 1)[0]
|
|
|
|
leading_removed = trailing_removed.split('<item>', 1)[-1]
|
|
|
|
rows = leading_removed.split('\n')
|
|
|
|
|
2017-10-23 09:58:41 +00:00
|
|
|
def get_row(key):
|
|
|
|
return [r for r in rows if r.startswith('<{}>'.format(key))][0]
|
2017-10-18 22:38:17 +00:00
|
|
|
|
2017-10-23 09:58:41 +00:00
|
|
|
title = str_between(get_row('title'), '<![CDATA[', ']]')
|
|
|
|
url = str_between(get_row('link'), '<link>', '</link>')
|
|
|
|
ts_str = str_between(get_row('pubDate'), '<pubDate>', '</pubDate>')
|
2017-10-18 22:38:17 +00:00
|
|
|
time = datetime.strptime(ts_str, "%a, %d %b %Y %H:%M:%S %z")
|
|
|
|
|
|
|
|
info = {
|
|
|
|
'url': url,
|
|
|
|
'domain': domain(url),
|
|
|
|
'base_url': base_url(url),
|
|
|
|
'timestamp': str(time.timestamp()),
|
|
|
|
'tags': '',
|
|
|
|
'title': title,
|
|
|
|
'sources': [rss_file.name],
|
|
|
|
}
|
|
|
|
info['type'] = get_link_type(info)
|
2017-10-23 09:58:41 +00:00
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
yield info
|
|
|
|
|
2017-07-04 10:38:07 +00:00
|
|
|
def parse_bookmarks_export(html_file):
|
|
|
|
"""Parse netscape-format bookmarks export files (produced by all browsers)"""
|
|
|
|
|
|
|
|
html_file.seek(0)
|
|
|
|
pattern = re.compile("<a href=\"(.+?)\" add_date=\"(\\d+)\"[^>]*>(.+)</a>", re.UNICODE | re.IGNORECASE)
|
|
|
|
for line in html_file:
|
2017-10-23 09:58:41 +00:00
|
|
|
# example line
|
|
|
|
# <DT><A HREF="https://example.com/?q=1+2" ADD_DATE="1497562974" LAST_MODIFIED="1497562974" ICON_URI="https://example.com/favicon.ico" ICON="data:image/png;base64,...">example bookmark title</A>
|
|
|
|
|
2017-07-04 10:38:07 +00:00
|
|
|
match = pattern.search(line)
|
|
|
|
if match:
|
|
|
|
url = match.group(1)
|
2017-10-18 22:38:17 +00:00
|
|
|
time = datetime.fromtimestamp(float(match.group(2)))
|
2017-07-04 10:38:07 +00:00
|
|
|
|
|
|
|
info = {
|
|
|
|
'url': url,
|
2017-10-18 22:38:17 +00:00
|
|
|
'domain': domain(url),
|
|
|
|
'base_url': base_url(url),
|
|
|
|
'timestamp': str(time.timestamp()),
|
2017-07-04 10:38:07 +00:00
|
|
|
'tags': "",
|
|
|
|
'title': match.group(3),
|
2017-10-18 22:38:17 +00:00
|
|
|
'sources': [html_file.name],
|
2017-07-04 10:38:07 +00:00
|
|
|
}
|
|
|
|
info['type'] = get_link_type(info)
|
2017-10-23 09:58:41 +00:00
|
|
|
|
2017-07-04 10:38:07 +00:00
|
|
|
yield info
|
2018-03-14 01:11:11 +00:00
|
|
|
|
|
|
|
def parse_pinboard_rss_feed(rss_file):
|
|
|
|
"""Parse Pinboard RSS feed files into links"""
|
|
|
|
|
|
|
|
rss_file.seek(0)
|
|
|
|
root = etree.parse(rss_file).getroot()
|
|
|
|
items = root.findall("{http://purl.org/rss/1.0/}item")
|
|
|
|
for item in items:
|
|
|
|
url = item.find("{http://purl.org/rss/1.0/}link").text
|
|
|
|
tags = item.find("{http://purl.org/dc/elements/1.1/}subject").text
|
|
|
|
title = item.find("{http://purl.org/rss/1.0/}title").text
|
|
|
|
ts_str = item.find("{http://purl.org/dc/elements/1.1/}date").text
|
|
|
|
# Pinboard includes a colon in its date stamp timezone offsets, which
|
|
|
|
# Python can't parse. Remove it:
|
|
|
|
if ":" == ts_str[-3:-2]:
|
|
|
|
ts_str = ts_str[:-3]+ts_str[-2:]
|
|
|
|
time = datetime.strptime(ts_str, "%Y-%m-%dT%H:%M:%S%z")
|
|
|
|
info = {
|
|
|
|
'url': url,
|
|
|
|
'domain': domain(url),
|
|
|
|
'base_url': base_url(url),
|
|
|
|
'timestamp': str(time.timestamp()),
|
|
|
|
'tags': tags,
|
|
|
|
'title': title,
|
|
|
|
'sources': [rss_file.name],
|
|
|
|
}
|
|
|
|
info['type'] = get_link_type(info)
|
|
|
|
yield info
|
2018-03-14 02:23:52 +00:00
|
|
|
|
|
|
|
def parse_medium_rss_feed(rss_file):
|
|
|
|
"""Parse Medium RSS feed files into links"""
|
|
|
|
|
|
|
|
rss_file.seek(0)
|
|
|
|
root = etree.parse(rss_file).getroot()
|
|
|
|
items = root.find("channel").findall("item")
|
|
|
|
for item in items:
|
|
|
|
for child in item:
|
|
|
|
print(child.tag, child.text)
|
|
|
|
url = item.find("link").text
|
|
|
|
title = item.find("title").text
|
|
|
|
ts_str = item.find("pubDate").text
|
|
|
|
time = datetime.strptime(ts_str, "%a, %d %b %Y %H:%M:%S %Z")
|
|
|
|
info = {
|
|
|
|
'url': url,
|
|
|
|
'domain': domain(url),
|
|
|
|
'base_url': base_url(url),
|
|
|
|
'timestamp': str(time.timestamp()),
|
|
|
|
'tags': "",
|
|
|
|
'title': title,
|
|
|
|
'sources': [rss_file.name],
|
|
|
|
}
|
|
|
|
info['type'] = get_link_type(info)
|
|
|
|
yield info
|