-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.py
More file actions
58 lines (42 loc) · 1.29 KB
/
import.py
File metadata and controls
58 lines (42 loc) · 1.29 KB
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
from html.parser import HTMLParser
from html.entities import name2codepoint
from datetime import datetime
class Link(object):
title = ''
href = ''
private = False
add_date = None
tags = ''
notes = ''
def __str__(self):
return ('Link:' + self.title + ', ' + self.href + ', ' + str(self.private) + ', '
+ str(self.add_date) + ', ' + self.tags + ', ' + self.notes)
class MyHTMLParser(HTMLParser):
current_tag = None
current_link = None
def handle_starttag(self, tag, attrs):
self.current_tag = tag
if tag == 'dt':
if self.current_link:
# if self.current_link.private is True:
print(self.current_link)
# self.current_link.save()
self.current_link = Link()
# print('Entry')
elif tag == 'a':
attr_dict = dict(attrs)
self.current_link.href = attr_dict.get('href',None)
self.current_link.private = bool(int(attr_dict['private']))
self.current_link.add_date = datetime.utcfromtimestamp(float(attr_dict['add_date']))
self.current_link.tags = attr_dict['tags']
elif tag == 'dd':
None
def handle_data(self, data):
if data != '\n':
if self.current_tag == 'a':
self.current_link.title = data
elif self.current_tag == 'dd':
self.current_link.notes = data
parser = MyHTMLParser()
file = open('delicious.html', 'r')
parser.feed(file.read())