switch to tweepy

This commit is contained in:
msk
2022-09-12 01:31:09 -07:00
parent 2d7e6324cb
commit 2ab28ec5c8
8 changed files with 93 additions and 16 deletions
+1 -1
View File
@@ -1 +1 @@
TwitterAPI
tweepy
+7 -3
View File
@@ -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'
+29
View File
@@ -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)
+12 -1
View File
@@ -2,5 +2,16 @@
# Scan all accounts for cross-company interactions.
# Terminates when finished scanning.
def run():
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
+2
View File
@@ -1,5 +1,7 @@
## The bot's listen mode
# Continuously listen for cross-company interactions.
import TwitterAPI as api
def run():
pass
+9 -4
View File
@@ -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
+16 -8
View File
@@ -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())
# 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()}'
+18
View File
@@ -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))