def __init__(self, format="json", domain="api.twitter.com", secure=True, auth=None, api_version=_DEFAULT): """ Create a new twitter API connector. Pass an `auth` parameter to use the credentials of a specific user. Generally you'll want to pass an `OAuth` instance:: twitter = Twitter(auth=OAuth( token, token_secret, consumer_key, consumer_secret)) `domain` lets you change the domain you are connecting. By default it's `api.twitter.com` but `search.twitter.com` may be useful too. If `secure` is False you will connect with HTTP instead of HTTPS. `api_version` is used to set the base uri. By default it's '1'. If you are using "search.twitter.com" set this to None. """ if not auth: auth = NoAuth() if (format not in ("json", "xml", "")): raise ValueError("Unknown data format '%s'" % (format)) if api_version is _DEFAULT: if domain == 'api.twitter.com': api_version = '1.1' else: api_version = None uriparts = () if api_version: uriparts += (str(api_version), ) TwitterCall.__init__(self, auth=auth, format=format, domain=domain, callable_cls=TwitterCall, secure=secure, uriparts=uriparts)
def __init__(self, format="json", domain="twitter.com", secure=True, auth=None, api_version=''): """ Create a new twitter API connector. Pass an `auth` parameter to use the credentials of a specific user. Generally you'll want to pass an `OAuth` instance:: twitter = Twitter(auth=OAuth( token, token_secret, consumer_key, consumer_secret)) `domain` lets you change the domain you are connecting. By default it's twitter.com but `search.twitter.com` may be useful too. If `secure` is False you will connect with HTTP instead of HTTPS. The value of `agent` is sent in the `X-Twitter-Client` header. This is deprecated. Instead Twitter determines the application using the OAuth Client Key and Client Key Secret parameters. `api_version` is used to set the base uri. By default it's nothing, but if you set it to '1' your URI will start with '1/'. """ if not auth: auth = NoAuth() if (format not in ("json", "xml", "")): raise ValueError("Unknown data format '%s'" % (format)) uriparts = () if api_version: uriparts += (str(api_version), ) TwitterCall.__init__(self, auth=auth, format=format, domain=domain, secure=secure, uriparts=uriparts)
def main(args=sys.argv[1:]): options = { 'oauth': False, 'followers': True, 'api-rate': False, 'show_id': False } try: parse_args(args, options) except GetoptError as e: err("I can't do that, %s." % e) raise SystemExit(1) # exit if no user or given, except if asking for API rate if not options['extra_args'] and not options['api-rate']: print(__doc__) raise SystemExit(1) # authenticate using OAuth, asking for token if necessary if options['oauth']: # oauth_filename = (os.getenv("HOME", "") + os.sep # + ".twitter-follow_oauth") # if not os.path.exists(oauth_filename): # oauth_dance("Twitter-Follow", CONSUMER_KEY, CONSUMER_SECRET, # oauth_filename) # oauth_token, oauth_token_secret = read_token_file(oauth_filename) auths = get_auths_data() random_index = random.randint(0, len(auths) - 1) err('Using the number %d oauth user' % random_index) auth = OAuth(auths[random_index][0], auths[random_index][1], CONSUMER_KEY, CONSUMER_SECRET) else: auth = NoAuth() twitter = Twitter(auth=auth, api_version='1.1', domain='api.twitter.com') if options['api-rate']: rate_limit_status(twitter) return # obtain list of followers (or following) for every given user for user in options['extra_args']: user_ids, users = [], {} user_id = twitter.users.lookup(screen_name=user)[0]['id'] try: user_ids = follow(twitter, user, options['followers']) users = lookup(twitter, user_ids) except KeyboardInterrupt as e: err() err("Interrupted.") raise SystemExit(1) print(''.join([ '%d' % user_id, '\t', '%d' % len(user_ids) ])) for uid in user_ids: if options['show_id']: try: print('following' + '\t' + str(uid) + '\t' + users[uid].encode("utf-8")) except KeyError: pass else: try: print(users[uid].encode("utf-8")) except KeyError: pass # print total on stderr to separate from user list on stdout if options['followers']: err("Total followers for %s: %i" % (user, len(user_ids))) else: err("Total users %s is following: %i" % (user, len(user_ids)))
def main(args=sys.argv[1:]): options = { 'oauth': False, 'save-dir': ".", 'api-rate': False, 'timeline': "", 'mentions': "", 'dms': "", 'favorites': False, 'follow-redirects': False, 'redirect-sites': None, 'isoformat': False, } try: parse_args(args, options) except GetoptError as e: err("I can't do that, %s." % e) raise SystemExit(1) # exit if no user given # except if asking for API rate, or archive of timeline or mentions if not options['extra_args'] and not ( options['api-rate'] or options['timeline'] or options['mentions'] or options['dms']): print(__doc__) return # authenticate using OAuth, asking for token if necessary if options['oauth']: # oauth_filename = (os.getenv("HOME", "") + os.sep # + ".twitter-archiver_oauth") # if not os.path.exists(oauth_filename): # oauth_dance("Twitter-Archiver", CONSUMER_KEY, CONSUMER_SECRET, # oauth_filename) # oauth_token, oauth_token_secret = read_token_file(oauth_filename) auths = get_auths_data() random_index = random.randint(0, len(auths) - 1) print('Using the number %d oauth user' % random_index) auth = OAuth(auths[random_index][0], auths[random_index][1], CONSUMER_KEY, CONSUMER_SECRET) else: auth = NoAuth() twitter = Twitter(auth=auth, api_version='1.1', domain='api.twitter.com') if options['api-rate']: rate_limit_status(twitter) return global format_text if options['follow-redirects'] or options['redirect-sites']: if options['redirect-sites']: hosts = parse_host_list(options['redirect-sites']) else: hosts = None format_text = functools.partial(expand_format_text, hosts) else: format_text = direct_format_text # read users from command-line or stdin users = options['extra_args'] if len(users) == 1 and users[0] == "-": users = [line.strip() for line in sys.stdin.readlines()] # save tweets for every user total, total_new = 0, 0 for user in users: filename = options['save-dir'] + os.sep + user user_id = twitter.users.lookup(screen_name=user)[0]['id'] # print('%d\n' % user_id) if options['favorites']: filename = filename + "-favorites" print("* Archiving %s tweets in %s" % (user, filename)) tweets = {} try: tweets = load_tweets(filename) except Exception as e: err("Error when loading saved tweets: %s - continuing without" % str(e)) new = 0 before = len(tweets) try: statuses(twitter, user, tweets, options['mentions'], options['favorites'], isoformat=options['isoformat']) except KeyboardInterrupt: err() err("Interrupted") raise SystemExit(1) save_tweets(filename, tweets, user_id) total += len(tweets) new = len(tweets) - before total_new += new print("Total tweets for %s: %i (%i new)" % (user, len(tweets), new)) print("Total: %i tweets (%i new) for %i users" % (total, total_new, len(users)))
def main(args=sys.argv[1:]): options = { 'oauth': False, 'followers': True, 'api-rate': False, 'show_id': False } try: parse_args(args, options) except GetoptError as e: err("I can't do that, %s." % e) raise SystemExit(1) # exit if no user or given, except if asking for API rate if not options['extra_args'] and not options['api-rate']: print(__doc__) raise SystemExit(1) # authenticate using OAuth, asking for token if necessary if options['oauth']: oauth_filename = (os.getenv("HOME", "") + os.sep + ".twitter-follow_oauth") if not os.path.exists(oauth_filename): oauth_dance("Twitter-Follow", CONSUMER_KEY, CONSUMER_SECRET, oauth_filename) oauth_token, oauth_token_secret = read_token_file(oauth_filename) auth = OAuth(oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET) else: auth = NoAuth() twitter = Twitter(auth=auth, api_version='1.1', domain='api.twitter.com') if options['api-rate']: rate_limit_status(twitter) return # obtain list of followers (or following) for every given user for user in options['extra_args']: user_ids, users = [], {} try: user_ids = follow(twitter, user, options['followers']) users = lookup(twitter, user_ids) except KeyboardInterrupt as e: err() err("Interrupted.") raise SystemExit(1) for uid in user_ids: if options['show_id']: try: print(str(uid) + "\t" + users[uid].encode("utf-8")) except KeyError: pass else: try: print(users[uid].encode("utf-8")) except KeyError: pass # print total on stderr to separate from user list on stdout if options['followers']: err("Total followers for %s: %i" % (user, len(user_ids))) else: e