def __init__(self): """ Initialises a Twitter session for making various data requests to Twitter. Populate the 'twitter.cfg' file. Allowed params are 1. consumer_key 2. consumer_secret 3. token_file 4. state_file 5. count of favourites 6. blocked links """ self.favouritesURL = 'https://api.twitter.com/1.1/favorites/list.json' self.params = { 'consumer_key': '', 'consumer_secret': '', 'token_file': '', 'state_file': '', 'count': '50', 'blocked': ['youtube.com', 'youtu.be'] } try: self.params = parse_configuration_file(TWITTER_CONFIGURATION_FILE, self.params) except ValueError, e: raise ValueError(e)
def get_access_token(configuration_file, request_token_url, access_token_url, authorization_url, verification_type): """ :param configuration_file: :param request_token_url: :param access_token_url: :param authorization_url: :param verification_type: :return: """ # Examples here https://github.com/requests/requests-oauthlib/blob/master/requests_oauthlib/oauth1_session.py # consumer_key, consumer_secret and token_file are needed here. # Parse it from the configuration file. try: tokens = parse_configuration_file(configuration_file, { 'consumer_key': '', 'consumer_secret': '', 'token_file': '' }) except ValueError, e: print e return
def main(): # Parse the command line arguments. parser = argparse.ArgumentParser(description='Favourite to Kindle') # parser.add_argument('--debug', '-d', help='Run in verbose mode.', action='store_true') parser.add_argument('--version', '-v', help='Display the app version.', action='store_true') parser.add_argument('--authorise', '-a', help='Authorise the Twitter app.', action='store_true') parser.add_argument('--revoke', '-r', help='Delete the Twitter token file.', action='store_true') args = parser.parse_args() if args.version: print str(version[0]) + '.' + str(version[1]) + '.' + str(version[2]) return 0 if args.authoriseTwitter: get_access_token(TWITTER_CONFIGURATION_FILE, TWITTER_REQUEST_TOKEN_URL, TWITTER_ACCESS_TOKEN_URL, TWITTER_AUTHORIZATION_URL, 'url') return 0 if args.revokeTwitter: tokens = parse_configuration_file(TWITTER_CONFIGURATION_FILE, {'token_file': ''}) try: os.remove(tokens['token_file']) except OSError as e: print 'Could not delete the token file.\n%s' % e return 1 print """Token file removed. To remove the app access, login to your account on Twitter and revoke access. Run the app with -a to authorise it.""" return 0 # Parsing done. Start the job. try: twitter_session = twitterOps.TwitterSession() readability_handle = readabilityOps.ReadabilitySession() sleep_interval = parse_configuration_file(TWITTER_CONFIGURATION_FILE, { 'sleep': 600 }) except ValueError, e: print e return 1
def __init__(self): """ Initialises a Readability session for readability operations. Currently sending the article to a kindle is the only operation. It does not need authentication. :return: None """ self.kindle_api = 'http://www.readability.com/api/kindle/v1/generator' try: self.kindle_details = parse_configuration_file(READABILITY_CONFIGURATION_FILE, { 'kindle_email': '' }) except ValueError, e: raise ValueError(e)