From 96ec375de3a2bf8b7e6c348f21593c13a304c276 Mon Sep 17 00:00:00 2001 From: Phrancis Date: Tue, 20 Aug 2019 21:57:25 -0400 Subject: [PATCH] Various fixes to un-fuck handling of file paths when importing modules within each other The use of manually building file paths was causing various FileNotFoundError problems. I decided to instead use the os.path module to let the paths build themselves using the location of __file__ --- __init__.py => api/__init__.py | 0 get_twitter_api.py => api/get_twitter_api.py | 13 ++++++++--- .../init_twitter_api.py | 0 followers/extract_all_user_followers.py | 22 +++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) rename __init__.py => api/__init__.py (100%) rename get_twitter_api.py => api/get_twitter_api.py (72%) rename init_twitter_api.py => api/init_twitter_api.py (100%) create mode 100644 followers/extract_all_user_followers.py diff --git a/__init__.py b/api/__init__.py similarity index 100% rename from __init__.py rename to api/__init__.py diff --git a/get_twitter_api.py b/api/get_twitter_api.py similarity index 72% rename from get_twitter_api.py rename to api/get_twitter_api.py index e599d23..e35595b 100644 --- a/get_twitter_api.py +++ b/api/get_twitter_api.py @@ -1,17 +1,23 @@ import json +import os from typing import Dict, TextIO from tweepy import API from tweepy import OAuthHandler +PATH_TO_APP_DATA = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'app_data') -def get_twitter_api(file_path: str = 'app_data/twitter_api_keys.json') -> API: + +def get_twitter_api(file_path: str = os.path.join(PATH_TO_APP_DATA, 'twitter_api_keys.json')) -> API: """ Authenticates Twitter API using stored values, and returns an API instance. + :param file_path: The path to app_data/twitter_api_keys.json :return: A Twitter API instance from tweepy. """ try: _file: TextIO _keys: Dict = dict() + print(os.path.dirname(__file__)) + print(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'app_data', 'twitter_api_keys.json')) with open(file_path, 'r') as _file: _keys = json.load(_file) except FileNotFoundError as _err: @@ -39,10 +45,11 @@ def get_current_user_json_field(field_name: str) -> object: :param field_name: Name of the JSON field :return: Value of the JSON field """ - return api.me()._json[field_name] + _api = get_twitter_api(os.path.join(PATH_TO_APP_DATA, 'twitter_api_keys.json')) + return _api.me()._json[field_name] if __name__ == '__main__': - api: API = get_twitter_api() + 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")}') diff --git a/init_twitter_api.py b/api/init_twitter_api.py similarity index 100% rename from init_twitter_api.py rename to api/init_twitter_api.py diff --git a/followers/extract_all_user_followers.py b/followers/extract_all_user_followers.py new file mode 100644 index 0000000..b551662 --- /dev/null +++ b/followers/extract_all_user_followers.py @@ -0,0 +1,22 @@ +import datetime +import os +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_follower_user_ids(screen_name: str = '') -> None: + """ + Get all of an account's followers' user IDs and write them to a JSON file with today's date. + :param screen_name: + :return: + """ + _api = api.get_twitter_api(os.path.join(PATH_TO_APP_DATA, 'twitter_api_keys.json')) + if screen_name == '': + screen_name = api.get_current_user_json_field('screen_name') + _file_name = f'follower_user_ids {screen_name} {datetime.date.today()}' + print(_file_name) + + +if __name__ == '__main__': + extract_all_follower_user_ids()