Created
October 15, 2011 11:13
-
-
Save sliem/1289424 to your computer and use it in GitHub Desktop.
Simple python script to publish an url to the Instapaper service.
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 argparse, urllib, sys | |
def error(msg): | |
sys.exit(msg) | |
def main(): | |
api = 'https://www.instapaper.com/api/add' | |
argparser = argparse.ArgumentParser(description='Simple utility to add articles to an Instapaper account.') | |
argparser.add_argument('url', help='url to add') | |
argparser.add_argument('-u', '--username', help='account username') | |
argparser.add_argument('-p', '--password', help='account password') | |
argparser.add_argument('-t', '--title', default='', help='set title of article manually') | |
argparser.add_argument('-d', '--description', default='', help='set description') | |
argparser.add_argument('-v', '--verbose', action='store_true', default=False, | |
help='increase verbosity') | |
args = argparser.parse_args() | |
params = urllib.urlencode({ | |
'username' : args.username, | |
'password' : args.password, | |
'url' : args.url, | |
'title' : args.title, | |
'selection' : args.description # While wierd the API documentation defines the 'selection' | |
# parameter an an description of the article. | |
}) | |
r = urllib.urlopen(api, params) | |
status = r.getcode() | |
if status == 201: | |
if args.verbose: | |
print('%s saved as %s' % (r.headers['Content-Location'], r.headers['X-Instapaper-Title'])) | |
elif status == 400: | |
error('Status 400: Bad request or exceeded the rate limit. Probably missing a required parameter, such as url.') | |
elif status == 403: | |
error('Status 403: Invalid username or password.') | |
elif status == 500: | |
error('Status 500: The service encountered an error. Please try again later') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment