Created
May 26, 2011 17:39
-
-
Save pelletier/993582 to your computer and use it in GitHub Desktop.
Backup your Read It Later bookmarks with CouchDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import simplejson as json | |
import urllib | |
import httplib | |
import restkit | |
from restkit.resource import Resource | |
# ReadItLater credentials | |
RIL_KEY = "****" | |
RIL_USER = "****" | |
RIL_PASS = "****" | |
# CouchDB informations | |
COUCH_KEY = "****" # User | |
COUCH_PASS = "****" # Password | |
COUCH_DB = "****" # Database | |
COUCH_HOST = "****" # Server | |
def get_couch(): | |
cfg = { | |
'user': COUCH_KEY, | |
'pass': COUCH_PASS, | |
'host': COUCH_HOST, | |
} | |
return Resource("https://%(user)s:%(pass)s@%(host)s" % cfg) | |
def get_since(): | |
r = get_couch() | |
try: | |
res = r.get("/%s/infos" % COUCH_DB) | |
except restkit.errors.ResourceNotFound: | |
return None | |
return int(json.loads(res.body_string())['since']) | |
def get_infos_rev(): | |
r = get_couch() | |
try: | |
res = r.get("/%s/infos" % COUCH_DB) | |
except restkit.errors.ResourceNotFound: | |
return None | |
return json.loads(res.body_string())['_rev'] | |
def create_since(since): | |
r = get_couch() | |
payload = json.dumps({'since': since}) | |
headers = { "Content-type": "application/x-www-form-urlencoded" } | |
r.put("/%s/infos" % COUCH_DB, payload, headers) | |
def update_since(since): | |
r = get_couch() | |
rev = get_infos_rev() | |
payload = json.dumps({'since': since, '_rev': rev}) | |
headers = { "Content-type": "application/json" } | |
r.put("/%s/infos" % COUCH_DB, payload, headers) | |
def main(): | |
# First grab the state from RIL | |
conn = httplib.HTTPSConnection("readitlaterlist.com") | |
payload = { | |
'apikey': RIL_KEY, | |
'username': RIL_USER, | |
'password': RIL_PASS, | |
'since': 0, | |
} | |
since = get_since() | |
if since == None: | |
del payload['since'] | |
else: | |
payload['since'] = since | |
params = urllib.urlencode(payload) | |
conn.request("GET", "/v2/get?"+params) | |
resp = json.loads(conn.getresponse().read()) | |
new_since = int(resp['since']) | |
# Update the since? | |
if not since == None: | |
update_since(new_since) | |
# Or create it | |
else: | |
create_since(new_since) | |
if resp['list'] == []: | |
return None | |
for key, data in resp['list'].iteritems(): | |
del data['item_id'] | |
data['time_updated'] = int(data['time_updated']) | |
data['time_added'] = int(data['time_added']) | |
data['tags'] = list(data.get('tags', '').split(',')) | |
data['read'] = bool(data['state']) | |
del data['state'] | |
r = get_couch() | |
r.put("/%s/%s" % (COUCH_DB, key), json.dumps(data)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment