Skip to content

Commit

Permalink
Add console interface and some error handling in case of invalud scre…
Browse files Browse the repository at this point in the history
…en name input by user
  • Loading branch information
Phrancis committed Aug 21, 2019
1 parent e4e216b commit 590bf5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/get_twitter_api.py
Expand Up @@ -50,4 +50,4 @@ def get_current_user_json_field(field_name: str) -> object:
if __name__ == '__main__':
api: API = get_twitter_api(os.path.join('app_data', 'twitter_api_keys.json'))
# Query the API for self data to make sure it's working
print(f'Current API user: {get_current_user_json_field("screen_name")}')
print(f'Current API user: {get_current_user_json_field("_screen_name")}')
33 changes: 23 additions & 10 deletions followers/extract_all_user_followers_ids.py
Expand Up @@ -3,34 +3,47 @@
from typing import List, TextIO

import tweepy
from tweepy import TweepError

from api import get_twitter_api as api

PATH_TO_APP_DATA = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'app_data')


def extract_all_user_followers_ids(screen_name: str = '') -> str:
def extract_all_user_followers_ids(screen_name: str) -> str:
"""
Get all of an account's followers' user IDs and write them to a JSON file with today's date.
:param screen_name: The screen name of the account to query. If left blank, will use the API user's screen name.
:return: The file path where the queried data was written.
"""
_api = api.get_twitter_api(os.path.join(PATH_TO_APP_DATA, 'twitter_api_keys.json'))
if screen_name == '':
if screen_name == '' or not screen_name:
screen_name = api.get_current_user_json_field('screen_name')
_output_file_name = f'user_followers_ids {screen_name} {datetime.date.today()}.txt'
_output_file_name = f'user_followers_ids {screen_name.lower()} {datetime.date.today()}.txt'
_output_file_path = os.path.join(PATH_TO_APP_DATA, _output_file_name)
_followers_count: int = 0
for page in tweepy.Cursor(_api.followers_ids, screen_name=screen_name).pages():
_output_file: TextIO
with open(_output_file_path, 'w') as _output_file:
for item in page:
_output_file.write(f'{item}\n')
return _output_file_path
try:
for page in tweepy.Cursor(_api.followers_ids, screen_name=screen_name).pages():
_output_file: TextIO
with open(_output_file_path, 'w') as _output_file:
for item in page:
_output_file.write(f'{item}\n')
return _output_file_path
except TweepError as err:
print(f'Could not query screen name: {screen_name}\nError(s) found:')
print(err)
raise err


if __name__ == '__main__':
output_file_path: str = extract_all_user_followers_ids()
print('Enter user screen name you wish to extract followers\' IDs, then press Enter.')
print('Do not include @ symbol before screen name.')
print('Leave blank to use your own screen name.')
_screen_name: str = input('Screen name: ')
# Just in case they don't read instructions ;-)
_screen_name = _screen_name.replace('@', '')

output_file_path: str = extract_all_user_followers_ids(_screen_name)
print(f'Results saved to file: {output_file_path}')
followers_count: int = 0
with open(output_file_path, 'r') as file:
Expand Down

0 comments on commit 590bf5d

Please sign in to comment.