def __init__(self): """ Checks the environment for the API_KEY and API_SECRET variables If no env variables available, ask the user for their API details Ask the user for their Readability username and password Exchange these credentials for user token and secret via xauth """ print "Welcome to unstability by @richbs" if 'API_KEY' in os.environ: k = os.environ['API_KEY'] else: k = raw_input("What is your Readability API key? ") if 'API_SECRET' in os.environ: s = os.environ['API_SECRET'] else: s = raw_input("What is your Readability API secret? ") u = raw_input("What is your Readability account username? ") p = getpass.getpass("What is your Readability password? ") self.username = u self.password = p user_token, user_secret = readability.xauth(k, s, u, p) self.rdc = readability.ReaderClient(k, s, user_token, user_secret)
def test_successful_auth(self): """ Test getting a token with proper creds """ # Credentials should be set as environment variables when running tests token = xauth() self.assertEqual(len(token), 2)
def main(): token = readability.xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) rdd = readability.oauth(CONSUMER_KEY, CONSUMER_SECRET, token=token) user = rdd.get_me() logger.info("Updating readability library") library_urls = [u.article.url for u in user.bookmarks()] logger.info("Found %d articles in library", len(library_urls)) # Fetch URLs urls = get_article_urls_from_twitter_favourites(TWITTER_USERNAME) urls += get_top_hacker_news_articles(5) urls += get_economist_articles(5) urls += get_atlantic_articles(2) # Only 3 as it's too noisy logger.info("Found %d articles to add", len(urls)) num_dupes = num_new = num_errors = 0 for url in urls: if url in library_urls: num_dupes += 1 else: logger.info("Adding %s", url) try: rdd.add_bookmark(url) except ResponseError: num_errors += 1 except Exception, e: logger.error("Unexpected exception: %s", e) num_errors += 1 else: num_new += 1
def setUp(self): """ Add a few bookmarks. """ token_key, token_secret = xauth() self.reader_client = ReaderClient(token_key=token_key, token_secret=token_secret) self.urls = [ 'http://www.theatlantic.com/technology/archive/2013/01/the-never-before-told-story-of-the-worlds-first-computer-art-its-a-sexy-dame/267439/', 'http://www.theatlantic.com/business/archive/2013/01/why-smart-poor-students-dont-apply-to-selective-colleges-and-how-to-fix-it/272490/', ] self.favorite_urls = [ 'http://www.theatlantic.com/sexes/archive/2013/01/the-lonely-existence-of-mel-feit-mens-rights-advocate/267413/', 'http://www.theatlantic.com/technology/archive/2013/01/women-in-combat-an-idea-whose-time-has-come-aided-by-technology/272483/' ] self.archive_urls = [ 'http://www.theatlantic.com/business/archive/2013/01/what-economics-can-and-cant-tell-us-about-the-legacy-of-legal-abortion/267459/', 'http://www.theatlantic.com/business/archive/2013/01/5-ways-to-understand-just-how-absurd-spains-26-unemployment-rate-is/272502/' ] self.all_urls = self.urls + self.favorite_urls + self.archive_urls for url in self.urls: response = self.reader_client.add_bookmark(url) self.assertTrue(response.status_code in [201, 202]) for url in self.favorite_urls: response = self.reader_client.add_bookmark(url, favorite=True) self.assertTrue(response.status_code in [201, 202]) for url in self.archive_urls: response = self.reader_client.add_bookmark(url, archive=True) self.assertTrue(response.status_code in [201, 202])
def setUp(self): """ Need to get a token for each test. """ token_key, token_secret = xauth() self.reader_client = ReaderClient(token_key, token_secret)
def get_token(self): """ Returns an array with the user_key [0] and user_secret [1] """ token = readability.xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) return token
def main(): token = readability.xauth( CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) rdd = readability.oauth( CONSUMER_KEY, CONSUMER_SECRET, token=token) user = rdd.get_me() logger.info("Updating readability library") library_urls = [u.article.url for u in user.bookmarks()] logger.info("Found %d articles in library", len(library_urls)) # Fetch URLs urls = get_article_urls_from_twitter_favourites(TWITTER_USERNAME) urls += get_top_hacker_news_articles(5) urls += get_economist_articles(5) urls += get_atlantic_articles(2) # Only 3 as it's too noisy logger.info("Found %d articles to add", len(urls)) num_dupes = num_new = num_errors = 0 for url in urls: if url in library_urls: num_dupes += 1 else: logger.info("Adding %s", url) try: rdd.add_bookmark(url) except ResponseError: num_errors += 1 except Exception, e: logger.error("Unexpected exception: %s", e) num_errors += 1 else: num_new += 1
def test_bad_password(self): """ If given a bad password, an exception should be raised. """ token = None with self.assertRaises(Exception): token = xauth(password='******') self.assertEqual(token, None)
def test_bad_username(self): """ If given a bad username, an exception should be raised. """ token = None with self.assertRaises(Exception): token = xauth(username='******') self.assertEqual(token, None)
def test_bad_password(self): """ If given a bad password, an exception should be raised. """ token = None with self.assertRaises(Exception): token = \ xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, 'badpassword') self.assertEqual(token, None)
def setUp(self): """ Get a client and add a bookmark """ token_key, token_secret = xauth() self.reader_client = ReaderClient(token_key=token_key, token_secret=token_secret) self.url = 'http://www.theatlantic.com/technology/archive/2013/01/the-never-before-told-story-of-the-worlds-first-computer-art-its-a-sexy-dame/267439/' add_response = self.reader_client.add_bookmark(self.url) self.assertTrue(add_response.status_code in [201, 202])
def test_bad_password(self): """If given a bad password, an exception should be raised. """ token = None with self.assertRaises(Exception): token = \ xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, 'badpassword') self.assertEqual(token, None)
def test_bad_username(self): """ If given a bad username, an exception should be raised. """ token = None with self.assertRaises(Exception): token = \ xauth(CONSUMER_KEY, CONSUMER_SECRET, 'bad username', PASSWORD) self.assertEqual(token, None)
def test_bad_consumer_secret(self): """ If given a bad consumer key, the `xauth` method should raise an exception. """ token = None with self.assertRaises(Exception): token = xauth(consumer_secret='bad consumer secret') self.assertEqual(token, None)
def test_bad_base_url(self): """ If given a bad base url template, the request to the ACCESS_TOKEN_URL should fail and an exception be raised. """ token = None with self.assertRaises(Exception): token = xauth(base_url_template='https://arc90.com/{0}') self.assertEqual(token, None)
def test_bad_username(self): """If given a bad username, an exception should be raised. """ token = None with self.assertRaises(Exception): token = \ xauth(CONSUMER_KEY, CONSUMER_SECRET, 'bad username', PASSWORD) self.assertEqual(token, None)
def test_bad_consumer_secret(self): """If given a bad consumer key, the `xauth` method should raise an exception. """ token = None with self.assertRaises(Exception): token = \ xauth(CONSUMER_KEY, 'bad consumer secret', USERNAME, PASSWORD) self.assertEqual(token, None)
def setUp(self): """Need to get a token for each test. """ token_pair = xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) self.token_key = token_pair[0] self.token_secret = token_pair[1] self.base_client = ReaderClient(CONSUMER_KEY, CONSUMER_SECRET, self.token_key, self.token_secret)
def test_bad_consumer_secret(self): """ If given a bad consumer key, the `xauth` method should raise an exception. """ token = None with self.assertRaises(Exception): token = \ xauth(CONSUMER_KEY, 'bad consumer secret', USERNAME, PASSWORD) self.assertEqual(token, None)
def api_readability_register(username, password): token = readability.xauth( app.config['READABILITY_CONSUMER_KEY'], app.config['READABILITY_CONSUMER_SECRET'], username, password) # oauth_token, oauth_secret = token # FIXME: should be in SESSION user_id = str(uuid.uuid4()) # Retrieve the user's articles and save them into the database with his user_id worker.run('retrieve_readability', token, user_id) return dumps({'user': user_id})
def test_bad_base_url(self): """If given a bad base url template, the request to the ACCESS_TOKEN_URL should fail and an exception be raised. """ token = None with self.assertRaises(Exception): token = xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD, base_url_template='https://arc90.com/{0}') self.assertEqual(token, None)
def setUp(self): """Get a client and add a bookmark """ token_pair = xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) self.token_key = token_pair[0] self.token_secret = token_pair[1] self.base_client = ReaderClient(CONSUMER_KEY, CONSUMER_SECRET, self.token_key, self.token_secret) self.url = 'http://www.theatlantic.com/technology/archive/2013/01/the-never-before-told-story-of-the-worlds-first-computer-art-its-a-sexy-dame/267439/' add_response = self.base_client.add_bookmark(self.url) self.assertEqual(add_response.status, 202)
def get_readability_api(api_key=None, api_secret=None, username=None, password=None): """ Authorise in Readability API""" if not api_key: api_key = os.environ.get('READABILITY_API_KEY', '') if not api_secret: api_secret = os.environ.get('READABILITY_API_SECRET', '') if not username: username = os.environ.get('READABILITY_USERNAME', '') if not password: password = os.environ.get('READABILITY_PASSWORD', '') token = readability.xauth(api_key, api_secret, username, password) rdd = readability.oauth(api_key, api_secret, token=token) return rdd
def readability_login(user, password): token_key, token_secret = xauth( username=user, password=password, consumer_key=READABILITY_CONSUMER_KEY, consumer_secret=READABILITY_CONSUMER_SECRET, ) rdd = ReaderClient( token_key=token_key, token_secret=token_secret, consumer_key=READABILITY_CONSUMER_KEY, consumer_secret=READABILITY_CONSUMER_SECRET, ) return rdd
def rdd_delete_bookmarks(): """ 清空 readability """ import readability token = readability.xauth(configs.RDD_APIKEY, configs.RDD_SECRET, configs.RDD_USERNAME, configs.RDD_PASSWORD) rdd = readability.oauth(configs.RDD_APIKEY, configs.RDD_SECRET, token=token) list = rdd.get_bookmarks() count = 0 for bm in list: resource = 'bookmarks/%s' % bm.id rdd._delete_resource(resource) count += 1 print count print 'delete count: ', count
def authorize(username, password): import config try: user_credentials = xauth( config.CONSUMER_KEY, config.CONSUMER_SECRET, username, password) if len(user_credentials) == 2: wf.save_password( 'readability_oauth_token', user_credentials[0]) wf.save_password( 'readability_oauth_token_secret', user_credentials[1]) return 'Workflow authorized.' except: pass return 'Authorization failed.'
def _import(list): """ 导入 readablility """ import readability token = readability.xauth(configs.RDD_APIKEY, configs.RDD_SECRET, configs.RDD_USERNAME, configs.RDD_PASSWORD) rdd = readability.oauth(configs.RDD_APIKEY, configs.RDD_SECRET, token=token) count = 0 for url, state in list: try: rdd.add_bookmark(url, archive=int(state)) count += 1 print count except Exception, e: if e.message: print url, state print e.message
def setUp(self): """Add a few bookmarks. """ token_pair = xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) self.token_key = token_pair[0] self.token_secret = token_pair[1] self.base_client = ReaderClient(CONSUMER_KEY, CONSUMER_SECRET, self.token_key, self.token_secret) self.urls = [ 'http://www.theatlantic.com/technology/archive/2013/01/the-never-before-told-story-of-the-worlds-first-computer-art-its-a-sexy-dame/267439/', 'http://www.theatlantic.com/business/archive/2013/01/why-smart-poor-students-dont-apply-to-selective-colleges-and-how-to-fix-it/272490/', ] self.favorite_urls = [ 'http://www.theatlantic.com/sexes/archive/2013/01/the-lonely-existence-of-mel-feit-mens-rights-advocate/267413/', 'http://www.theatlantic.com/technology/archive/2013/01/women-in-combat-an-idea-whose-time-has-come-aided-by-technology/272483/' ] self.archive_urls = [ 'http://www.theatlantic.com/business/archive/2013/01/what-economics-can-and-cant-tell-us-about-the-legacy-of-legal-abortion/267459/', 'http://www.theatlantic.com/business/archive/2013/01/5-ways-to-understand-just-how-absurd-spains-26-unemployment-rate-is/272502/' ] self.all_urls = self.urls + self.favorite_urls + self.archive_urls for url in self.urls: add_response = self.base_client.add_bookmark(url) self.assertEqual(add_response.status, 202) for url in self.favorite_urls: add_response = self.base_client.add_bookmark(url, favorite=True) self.assertEqual(add_response.status, 202) for url in self.archive_urls: add_response = self.base_client.add_bookmark(url, archive=True) self.assertEqual(add_response.status, 202)
#!/usr/bin/python from PIL import Image import sys, cStringIO, urllib, re import MySQLdb, readability # --/ readability authorization token = readability.xauth('getassembly', 'CutXN5cHHAS8NxnPt3eq9au6hHfvUqcB', 'getassembly', 'assembly') rdd = readability.oauth('getassembly', 'CutXN5cHHAS8NxnPt3eq9au6hHfvUqcB', token=token) # --/ bookmark url url = sys.argv[1] print url # --/ add to readability b = rdd.add_bookmark(url) a = rdd.get_article(b.article.id) # --/ article content t = a.title c = a.content print t print c # --/ extract video m = re.compile(r'<iframe src="http://www.youtube.com/embed/(.*?)".*?</iframe>').search(c) if m is None: vid = '' else: vid = m.group(1)
source_url = "http://news.ycombinator.com/best" soup = Soup(requests.get(source_url).content) urls = [] for td in soup("td", attrs={"class": "title"}): anchor = td.find("a") if not anchor: continue urls.append(anchor["href"]) if len(urls) == n: break return urls from config import * token = readability.xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) rdd = readability.oauth(CONSUMER_KEY, CONSUMER_SECRET, token=token) user = rdd.get_me() logger.info("Updating readability library") logger.info("Fetching library") library_urls = [u.article.url for u in user.bookmarks()] logger.info("Found %d articles in library", len(library_urls)) # Fetch URLs urls = get_article_urls_from_twitter_favourites(TWITTER_USERNAME) urls += get_top_hacker_news_articles() num_dupes = num_new = num_errors = 0
def test_successful_auth(self): """Test getting a token with proper creds """ token = xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) self.assertEqual(len(token), 2)
#!/usr/bin/python import readability from datetime import date, timedelta from sqlalchemy import * ''' readability config ''' token = readability.xauth('read_username', 'read_apikey', 'read_email', 'read_pass') rdd = readability.oauth('read_username', 'read_apikey', token=token) ''' sqlalchemy setup ''' engine = create_engine('mysql://*****:*****@db_host/db') connection = engine.connect() ''' Fetch new bookmarks since yesterday ''' yesterday = date.today() - timedelta(1) for b in rdd.get_bookmarks(added_since=yesterday.strftime('%m-%d-%y')): result = connection.execute("INSERT INTO bookmarks VALUES(NULL, " + str(b.id) + ", '" + b.article.title.encode("utf-8") + "', '" + b.article.url.encode("utf-8") + "')" )
credentials = False while credentials == False: # input of key and secret inpUser= ""; while len(inpUser) < 2: inpUser = raw_input("Your Readability username: "******""; while len(inpPwd) < 2: inpPwd = raw_input("Your Readability password: "******"Connecting to Readability to verify your credentials...") connected = False try: token = readability.xauth(keys["consumerKey"], keys["consumerSecret"], inpUser, inpPwd) connected = True except: print("Wrong credentials, please try again.") # load settings if connected: settingsFile = "data/settings.json" settings = parser.getSettings(settingsFile) if settings == False: # assign new preset settings settings = {} settings['title'] = '' # the last parsed title settings['kindle'] = False # directly send to Kindle settings['token1'] = token[0]
def __init__(self, key, secret, username, pwd): self.token = readability.xauth(key, secret, username,pwd) self.rdd = readability.oauth(key, secret, token=self.token)
def readability_login(user, password): xauth_token = (readability.xauth(READABILITY_CONSUMER_KEY, READABILITY_CONSUMER_SECRET, user, password)) return readability.oauth(READABILITY_CONSUMER_KEY, READABILITY_CONSUMER_SECRET, token=xauth_token)
def main(): try: # This script is meant only to run on a TTY assert sys.stdout.isatty() and sys.stdin.isatty() # Parse args parser = argparse.ArgumentParser(description='Move Readability queue to Instapaper.') parser.add_argument('-v', '--verbose', action='store_true', default=False) parser.add_argument('-a', '--all', action='store_true', default=False, help="Transfer all queued Readability items to Instapaper, whether or not they're already present.") parser.add_argument('-i', '--instapaper-csv', type=argparse.FileType('r'), help="Use supplied CSV (e.g. from instapaper.com) to filter what will be transferred.") parser.add_argument('-k', '--readability-key', action='store', default=os.environ.get('READABILITY_KEY'), help="The Readability API key to use.") parser.add_argument('-s', '--readability-secret', action='store', default=os.environ.get('READABILITY_SECRET'), help="The Readability API secret to use.") args = parser.parse_args() if not args.readability_key: parser.error("A Readability API key is required. Specify with -k or the READABILITY_KEY envvar.") if not args.readability_secret: parser.error("A Readability API secret is required. Specify with -s or the READABILITY_SECRET envvar.") # Warn about not using an Instapaper CSV if not args.all and not args.instapaper_csv: print >>sys.stderr, colored.yellow('Warning')+':', "No Instapaper CSV supplied.", \ "Won't filter already-added URLs. This can be hazardous to your rate limit.\n" # Load the set of URLs already added to Instapaper already_added = set() if args.instapaper_csv: for row in unicode_dict_csv_read(args.instapaper_csv): already_added.add(row['URL'].strip().lower()) args.instapaper_csv.close() # Log into Instapaper sys.stdout.write("Instapaper email: ") sys.stdout.flush() insta_uname = raw_input().strip() insta_pwd = getpass.getpass() print (CURSOR_UP_ONE_AND_ERASE_LINE) * 2 # clear prompts sys.stdout.write('Logging into Instapaper...') sys.stdout.flush() insta = instapaperlib.Instapaper(insta_uname, insta_pwd) r, msg = insta.auth() if r == 200: print ' [ '+colored.green('OK')+' ]' else: print ' [ '+colored.red('FAILED')+' ]' print >>sys.stderr, msg sys.exit(1) # Auth with Readability sys.stdout.write("\nReadability username: ") sys.stdout.flush() read_uname = raw_input().strip() read_pwd = getpass.getpass() print (CURSOR_UP_ONE_AND_ERASE_LINE) * 3 # clear prompts sys.stdout.write('Logging into Readability...') sys.stdout.flush() try: read_token = readability.xauth(args.readability_key, args.readability_secret, read_uname, read_pwd) rdd = readability.oauth(args.readability_key, args.readability_secret, token=read_token) except readability.api.AuthenticationError, e: print ' [ '+colored.red('FAILED')+' ]' print >>sys.stderr, 'Invalid username or password.' sys.exit(2) else:
def test_successful_auth(self): """ Test getting a token with proper creds """ token = xauth(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) self.assertEqual(len(token), 2)