forked from bbolli/tumblr-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelicious_import.py
executable file
·96 lines (79 loc) · 2.51 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
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
#!/usr/bin/env python
"""Imports delicious.com links into Tumblr.
- get all your delicious.com links into a file:
curl -u user:password \
'https://api.del.icio.us/v1/posts/all?meta=yes' >links.xml
- if curl is missing:
open the above URL in a browser and save the resulting page as "links.xml"
- run this script to post the links to Tumblr
- delete your delicious.com account
The link's timestamp, tags and privacy flag are kept.
XML format of a link:
<post description="The problem with Django's Template Tags"
extended="My reply"
hash="ec46dd48f9b117f2adc8d7b10b8356d7"
href="http://ericholscher.com/blog/2008/nov/8/problem-django-template-tags/"
meta="28717456b732b4117631ff27fd959708"
private="yes" shared="no" tag="reply django templetetags"
time="2008-12-04T07:33:25Z"
/>
"""
import sys
import os
import getopt
import urllib
import urllib2
import netrc
from time import strptime, sleep
from datetime import datetime
from calendar import timegm
import xmltramp
DEBUG = False
HOST = 'www.tumblr.com'
def tumble(links):
auth = netrc.netrc().authenticators(HOST)
if auth is not None:
auth = {'email': auth[0], 'password': auth[2]}
return [post(auth, e) for e in links]
def post(auth, link):
''
data = {'type': 'link', 'url': link('href'), 'name': link('description')}
if link('private') == 'yes':
data['private'] = '1'
ext = link('extended')
if ext:
data['description'] = ext
tags = link('tag')
if tags:
data['tags'] = ','.join('"%s"' % t for t in tags.split())
t = datetime.fromtimestamp(timegm(strptime(link('time'), '%Y-%m-%dT%H:%M:%SZ')))
data['date'] = t.isoformat(' ')
if DEBUG:
return 'debug', data
data.update(auth)
for k in data:
if type(data[k]) is unicode:
data[k] = data[k].encode('utf-8')
sleep(2)
print data['url']
try:
return 'ok', urllib2.urlopen('http://' + HOST + '/api/write', urllib.urlencode(data)).read()
except Exception, e:
return 'error', repr(e)
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], 'd')
except:
print "Usage: %s [-d]" % sys.argv[0].split(os.sep)[-1]
sys.exit(1)
for o, v in opts:
if o == '-d':
DEBUG = True
links = xmltramp.parse(open('links.xml').read())
result = tumble(links)
if result:
import pprint
pprint.pprint(result)
if 'error' in [r[0] for r in result]:
sys.exit(2)
sys.exit(0)