diff --git a/requirements.txt b/requirements.txt index 5cde2f1..69ae13e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -TwitterAPI \ No newline at end of file +tweepy \ No newline at end of file diff --git a/secrets.ini b/secrets.ini index 9b94c1b..6198eae 100644 --- a/secrets.ini +++ b/secrets.ini @@ -13,7 +13,11 @@ # # https://stackoverflow.com/questions/10755655/git-ignore-tracked-files +# note: api_key/secret = consumer_key/secret + [Credentials] -api_key='xxx' -api_secret='yyy' -bearer_token='zzz' +api_key='x' +api_secret='y' +bearer_token='z' +oauth1_access_token='a' +oauth1_access_secret='b' \ No newline at end of file diff --git a/src/api.py b/src/api.py new file mode 100644 index 0000000..ce13d84 --- /dev/null +++ b/src/api.py @@ -0,0 +1,29 @@ +import tweepy + +import secrets +import util + +class API: + instance = None + + def __init__(self): + API.instance = self + self.client = tweepy.Client( + bearer_token=secrets.bearer_token(), + # consumer_key=secrets.api_key(), + # consumer_secret=secrets.api_secret(), + # access_token=secrets.access_token(), + # access_token_secret=secrets.access_secret() + ) + + def get_tweets(self, id: int, count=5): + posts = list() + + step = util.clamp(count, 5, 100) + retrieved_tweets = 0 + pagination_token = None + + # while retrieved_tweets < count: + + resp = self.client.get_users_tweets(id, max_results=count) + print(resp) \ No newline at end of file diff --git a/src/catchup.py b/src/catchup.py index e5ad38a..66d4af3 100644 --- a/src/catchup.py +++ b/src/catchup.py @@ -2,5 +2,16 @@ # Scan all accounts for cross-company interactions. # Terminates when finished scanning. +import os +import TwitterAPI as api + +from util import * + +## Returns list of tweets present in queue.txt +def get_local_queue(): + f = open(os.path.join(get_project_dir(), 'queue.txt')) + pass + def run(): + queue = get_local_queue() pass \ No newline at end of file diff --git a/src/listen.py b/src/listen.py index e2b8dd3..1357a5a 100644 --- a/src/listen.py +++ b/src/listen.py @@ -1,5 +1,7 @@ ## The bot's listen mode # Continuously listen for cross-company interactions. +import TwitterAPI as api + def run(): pass \ No newline at end of file diff --git a/src/main.py b/src/main.py index 3134eb8..787b286 100644 --- a/src/main.py +++ b/src/main.py @@ -6,6 +6,8 @@ import secrets import catchup import listen +from api import API + def init_argparse(): p = argparse.ArgumentParser(description='Twitter bot that follows interactions between Nijisanji EN/ID and hololive EN/ID members.', formatter_class=RawTextHelpFormatter) p.add_argument('mode', nargs='?', \ @@ -28,14 +30,17 @@ def main(): if args.mode is None: return + api = API() + api.get_tweets(1390620618001838086) + # determine running mode match args.mode.lower(): case 'l' | 'listen': - print('LISTEN MODE\n') - catchup.run() - case 'c' | 'catchup': - print('CATCH-UP MODE\n') + print('RUNNING IN LISTEN MODE\n') listen.run() + case 'c' | 'catchup': + print('RUNNING IN CATCH-UP MODE\n') + catchup.run() case _: print('\ninvalid mode. run with no arguments for help page, including mode list.') return diff --git a/src/secrets.py b/src/secrets.py index 2e49803..6b84477 100644 --- a/src/secrets.py +++ b/src/secrets.py @@ -3,20 +3,22 @@ import os import configparser +from util import * + # returns dictionary of the Credentials section. # [NOT TO BE USED OUTSIDE OF THIS FILE.] def get_ini_credentials(): c = configparser.RawConfigParser() - if len(c.read(os.path.join(os.path.dirname(__file__), os.pardir, 'secrets.ini'))) > 0 and c.has_section('Credentials'): + if len(c.read(os.path.join(get_project_dir(), 'secrets.ini'))) > 0 and c.has_section('Credentials'): return c['Credentials'] return None -# returns the api_key stored in secrets.ini +# returns the consumer api_key stored in secrets.ini def api_key(): c = get_ini_credentials() return c.get(option='api_key', fallback='xxx') if c is not None else 'xxx' -# returns the api_secret stored in secrets.ini +# returns the consumer api_secret stored in secrets.ini def api_secret(): c = get_ini_credentials() return c.get(option='api_secret', fallback='yyy') if c is not None else 'yyy' @@ -26,9 +28,15 @@ def bearer_token(): c = get_ini_credentials() return c.get(option='bearer_token', fallback='zzz') if c is not None else 'zzz' -def get_all_secrets(): - return f'api_key:{api_key()}\napi_secret:{api_secret()}\nbearer_token:{bearer_token()}' +# returns the access_token stroed in secrets.ini +def access_token(): + c = get_ini_credentials() + return c.get(option='oauth1_access_token', fallback='zzz') if c is not None else 'aaa' -# print(api_key()) -# print(api_secret()) -# print(bearer_token()) \ No newline at end of file +# returns the access_secret stroed in secrets.ini +def access_secret(): + c = get_ini_credentials() + return c.get(option='oauth1_access_secret', fallback='zzz') if c is not None else 'bbb' + +def get_all_secrets(): + return f'api_key:{api_key()}\napi_secret:{api_secret()}\nbearer_token:{bearer_token()}\naccess_token:{access_token()}\naccess_secret:{access_secret()}' \ No newline at end of file diff --git a/src/util.py b/src/util.py new file mode 100644 index 0000000..2f1cb56 --- /dev/null +++ b/src/util.py @@ -0,0 +1,18 @@ +## Shared utility functions. + +import os + +global twAPI +twAPI = None + +# returns system path to this project, which is +# up one level from this file's directory. +def get_project_dir(): + return os.path.join(os.path.dirname(__file__), os.pardir) + +# determine if tweet involves cross-company interaction +def is_cross_company(tweet): + pass + +def clamp(n, smallest, largest): + return max(smallest, min(n, largest)) \ No newline at end of file