forked from hmason/ml_class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delicious_import.py
51 lines (38 loc) · 1.64 KB
/
delicious_import.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
#!/usr/bin/env python
# encoding: utf-8
"""
delicious_import.py
Created by Hilary Mason on 2010-11-28.
Copyright (c) 2010 Hilary Mason. All rights reserved.
"""
import sys, os
import urllib
import csv
from xml.dom import minidom
from optparse import OptionParser
class delicious_import(object):
def __init__(self, username, password=''):
# API URL: https://user:[email protected]/v1/posts/all
url = "https://%s:%[email protected]/v1/posts/all" % (username, password)
h = urllib.urlopen(url)
content = h.read()
h.close()
x = minidom.parseString(content)
data = []
# sample post: <post href="http://www.pixelbeat.org/cmdline.html" hash="e3ac1d1e4403d077ee7e65f62a55c406" description="Linux Commands - A practical reference" tag="linux tutorial reference" time="2010-11-29T01:07:35Z" extended="" meta="c79362665abb0303d577b6b9aa341599" />
post_list = x.getElementsByTagName('post')
for post_index, post in enumerate(post_list):
url = post.getAttribute('href')
desc = post.getAttribute('description')
tags = ",".join([t for t in post.getAttribute('tag').split()])
timestamp = post.getAttribute('time')
data.append([url, tags])
writer = csv.writer(open("links.csv", 'wb'))
for entry in data:
writer.writerow(entry)
if __name__ == '__main__':
try:
(username, password) = sys.argv[1:]
except ValueError:
print "Usage: python delicious_import.py username password"
d = delicious_import(username, password)