This repository was archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert-delicious-to-webloc
executable file
·53 lines (45 loc) · 1.75 KB
/
convert-delicious-to-webloc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import os
import re
import requests
import sys
import xml.dom.minidom
from xml.dom.minidom import Node
PREAMBLE='''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
'''
POSTAMBLE='''
</dict>
</plist>
'''
def tidy_filename(filenameToTidy):
newFilenameToTidy = re.sub('[^\w\-_\. ]', '_', filenameToTidy)
return newFilenameToTidy
filename = sys.argv[1]
targetdirectory = sys.argv[2]
dom = xml.dom.minidom.parse(filename)
posts = dom.getElementsByTagName('post')
for post in posts:
description = post.attributes['description'].value
tags = post.attributes['tag'].value.split()
url = post.attributes['href'].value
if(not ('from' in tags and 'twitter' in tags)):
assert(description != '' and description is not None)
assert(url != '' and url is not None)
try:
request = requests.get(url)
if request.status_code != 200:
print("URL " + url + " returns status code " + str(request.status_code) + ", ignoring.", file=sys.stderr)
else:
for tag in tags:
targetdir = os.path.join(targetdirectory, tag)
os.makedirs(targetdir, exist_ok=True)
contents = PREAMBLE + '\t<string>' + url + '</string>' + POSTAMBLE
new_filename = os.path.join(targetdir, tidy_filename(description) + '.webloc')
with open(new_filename, 'w') as f:
f.write(contents)
except requests.exceptions.RequestException as e:
print("URL " + url + " failed with error " + str(e) + ", ignoring.", file=sys.stderr)