This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wunderlist2Wunderbar.py
executable file
·135 lines (109 loc) · 3.52 KB
/
Wunderlist2Wunderbar.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, optparse, urllib2
import xml.etree.ElementTree as ET
reload(sys)
sys.setdefaultencoding("UTF-8")
# config
usage = "usage: %s <wunderurl>" % ( sys.argv[0], )
usage += "\n"
usage += "Sample:\n"
usage += " %s https://www.wunderlist.com/lists/70326287" % ( sys.argv[0], )
optParser = optparse.OptionParser(usage=usage)
# a Wunderlist is not proper XHTML, so we need to patch it up
def cleanupWLCrap(filedata):
idx = filedata.find("<body")
filedata = filedata[idx:]
idx = filedata.find("</body>")
filedata = filedata[:idx + 7]
# we use the <ol> since we can't easily scan for </div> close tags in invalid
# XML
idx = filedata.find('<ol class="tasks')
filedata = filedata[idx:]
idx = filedata.find("</ol>")
filedata = filedata[:idx + 5]
return filedata
def hasAttr(node, attr, value):
if not node.attrib.has_key(attr):
return None
v = node.attrib[attr]
vs = v.split(" ")
return value in vs
def iCalEscapeValue(str):
# FIXME: properly escape ...
return str
class MakeWunderlistWunderbar:
def handleTaskItem(self, node):
"""
<li class="taskItem" aria-label="Release It: Design and Deploy ..
http://www.amazon.com/Release-It-Production-Ready-Pragmatic-Programmers/dp/0978739213">
<span class="checkBox left"></span>
<div class="title">
Release It: Design and Deploy Production-Ready Software - Michael Nygard
<a href="http://www.amazon.com/Release-It-Production-Ready-Pragmatic-Programmers/dp/0978739213" target="_blank">www.amazon.com/Release-It-Production-Ready-Pragmatic-Programmers/dp/0978739213</a>
</div>
</li>
"""
title = ""
url = ""
for child in node:
if hasAttr(child, "class", "title"):
title = child.text.strip()
u = child.find("a")
if u is not None:
url = u.attrib["href"]
print "BEGIN:VTODO"
print "STATUS:NEEDS-ACTION" # this is a template ...
print "SUMMARY:" + iCalEscapeValue(title)
print "URL:" + iCalEscapeValue(url)
# Lame: OSX reminders doesn't support URL ...
print "DESCRIPTION:" + iCalEscapeValue(url)
print "END:VTODO"
def handleTaskList(self, node):
for child in node:
if hasAttr(child, "class", "taskItem"):
self.handleTaskItem(child)
# we could actually import this info, - if, the HTML wouldn't be crap
"""
<div class="tasks container">
<h1 class="information">
<span class="list-name">Must-Read Software Developer Books</span>
<div class="avatar">
<img src="https://a.wunderlist.com/api/v1/avatar?user_id=4194452"/>
</div>
<span class="sender-name">
Published by Chad Fowler
</span>
</h1>
"""
def scanHTML(self, root):
print "BEGIN:VCALENDAR"
print "VERSION:2.0"
print "PRODID:-//Always Right Institute//Wunderbar//EN"
print "CALSCALE:GREGORIAN"
for child in root:
if hasAttr(child, "class", "tasks"):
self.handleTaskList(child)
print "END:VCALENDAR"
def main():
options, args = optParser.parse_args()
if len(args) < 1:
print usage
sys.exit(42)
# load wunderlist
if not args[0].startswith("http"):
filename = args[0]
f = open(filename, "r")
filedata = f.read()
f.close()
else:
response = urllib2.urlopen(args[0])
filedata = response.read()
# parse
tree = ET.fromstring(cleanupWLCrap(filedata))
# process
root = ( tree, )
w2w = MakeWunderlistWunderbar()
w2w.scanHTML( root )
if __name__ == "__main__":
main()