Created
August 31, 2016 02:37
-
-
Save gujiaxi/0a2ac303938aa89e52953dcdc71c99c3 to your computer and use it in GitHub Desktop.
Import bookmarks to http://saved.io/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
f = open('./bookmarks.html') | |
url = 'http://devapi.saved.io/bookmarks/' | |
devkey = '<dev key>' | |
usrkey = '<user key>' | |
def parse(w): | |
r = {} | |
p1, p2, none = w.split(">") | |
p2, p3 = p2.split("</") | |
r['content'] = p2 | |
r['type'] = p3 | |
for s in p1.split()[1:]: | |
k, v = s.split('=', 1) | |
r[k] = v[1:-1] | |
return r | |
DL = "<DL>" | |
RDL = "</DL>" | |
DT = "<DT>" | |
level = -1 | |
data = [] | |
for w in f: | |
w = w.strip() | |
if w.startswith(DL): | |
level = level + 1 | |
elif w.startswith(RDL): | |
level = level - 1 | |
elif w.startswith(DT): | |
w = w[4:] | |
info = parse(w) | |
if info['type'] == 'A': | |
data.append({'devkey': devkey, 'key': usrkey, 'url': info['HREF'], 'title': info['content']}) | |
for item in data: | |
r = requests.post(url, data = item) | |
print('Done.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment