forked from anoved/ReadingListReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadinglist2instapaper.py
executable file
·68 lines (57 loc) · 2.25 KB
/
readinglist2instapaper.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
#!/usr/bin/env python
# Requires https://github.com/mrtazz/InstapaperLibrary
from instapaperlib import Instapaper
from readinglistlib import ReadingListReader
# Standard library modules
import argparse
import sys
import os
import re
# Configure and consume command line arguments.
ap = argparse.ArgumentParser(description='This script adds your Safari Reading List articles to Instapaper.')
ap.add_argument('-u', '--username', action='store', default='', help='Instapaper username or email.')
ap.add_argument('-p', '--password', action='store', default='', help='Instapaper password (if any).')
ap.add_argument('-v', '--verbose', action='store_true', help='Print article URLs as they are added.')
args = ap.parse_args()
if '' == args.username:
# For compatibility with instapaperlib's instapaper.py tool,
# attempt to read Instapaper username and password from ~/.instapaperrc.
# (Login pattern modified to accept blank passwords.)
login = re.compile("(.+?):(.*)")
try:
config = open(os.path.expanduser('~') + '/.instapaperrc')
for line in config:
matches = login.match(line)
if matches:
args.username = matches.group(1).strip()
args.password = matches.group(2).strip()
break
if '' == args.username:
print >> sys.stderr, 'No username:password line found in ~/.instapaperrc'
ap.exit(-1)
except IOError:
ap.error('Please specify a username with -u/--username.')
# Log in to the Instapaper API.
instapaper = Instapaper(args.username, args.password)
(auth_status, auth_message) = instapaper.auth()
# 200: OK
# 403: Invalid username or password.
# 500: The service encountered an error.
if 200 != auth_status:
print >> sys.stderr, auth_message
ap.exit(-1)
# Get the Reading List items
rlr = ReadingListReader()
articles = rlr.read()
for article in articles:
(add_status, add_message) = instapaper.add_item(article['url'].encode('utf-8'), title=article['title'].encode('utf-8'))
# 201: Added
# 400: Rejected (malformed request or exceeded rate limit; probably missing a parameter)
# 403: Invalid username or password; in most cases probably should have been caught above.
# 500: The service encountered an error.
if 201 == add_status:
if args.verbose:
print article['url'].encode('utf-8')
else:
print >> sys.stderr, add_message
ap.exit(-1)