Skip to content

Instantly share code, notes, and snippets.

@wtsnjp
Created December 15, 2023 09:51
Show Gist options
  • Save wtsnjp/44229f72e8b11b3684d0a4faf80fee7b to your computer and use it in GitHub Desktop.
Save wtsnjp/44229f72e8b11b3684d0a4faf80fee7b to your computer and use it in GitHub Desktop.
LaTeX. Not LaTex.
#
# usage: python latex_bot.py
#
import os
import re
import twitter
import logging
from logging.handlers import RotatingFileHandler
def main():
CONSUMER_KEY = os.environ('TWITTER_CONSUMER_KEY')
CONSUMER_SECRET = os.environ('TWITTER_CONSUMER_SECRET')
TWITTER_CREDS = os.path.expanduser('~/.latex_bot_credentials')
if not os.path.exists(TWITTER_CREDS):
twitter.oauth_dance('latex_bot', CONSUMER_KEY, CONSUMER_SECRET,
TWITTER_CREDS)
oauth_token, oauth_secret = twitter.read_token_file(TWITTER_CREDS)
auth = twitter.OAuth(oauth_token, oauth_secret, CONSUMER_KEY,
CONSUMER_SECRET)
targets = [
'lAtex', 'LAtex', 'laTex', 'LaTex', 'lATex', 'LATex', 'latEx', 'LatEx',
'lAtEx', 'LAtEx', 'laTEx', 'LaTEx', 'lATEx', 'LATEx', 'lateX', 'LateX',
'lAteX', 'LAteX', 'laTeX', 'lATeX', 'LATeX', 'latEX', 'LatEX', 'lAtEX',
'LAtEX', 'laTEX', 'LaTEX', 'lATEX'
]
ignore = [
'LaTex2018', 'Latex2018', 'LaTex18', '#LaTex', 'LateX_Token', 'sex',
'LateX.Media'
]
bot = LatexBot(auth=auth, targets=targets, ignore=ignore)
bot.run()
def get_logger(name):
logger = logging.getLogger(name)
handler = RotatingFileHandler('latex_bot.log',
maxBytes=5000000,
backupCount=9)
formatter = logging.Formatter('%(asctime)s - %(message)s')
level = logging.INFO
handler.setLevel(level)
handler.setFormatter(formatter)
logger.setLevel(level)
logger.addHandler(handler)
logger.propagate = False
return logger
class LatexBot:
logger = get_logger('latex_bot')
def __init__(self, auth=None, targets=list(), ignore=list()):
self.name = 'latex_bot'
self.word = 'latex'
self.targets = targets
self.ignore = ignore
if auth is not None:
self.twitter = twitter.Twitter(auth=auth)
def __judge(self, text):
if True in [w in text for w in self.ignore]:
return False
text = re.sub(r'(^|[\s\n(])@[_\w\d]+', r'\1', text)
if True not in [w in text for w in self.targets]:
return False
return True
def run(self):
LatexBot.logger.info('Running')
# get blocked users list
targets = self.twitter.blocks.ids().get('ids', list())
# search 'latex' on Twitter
query = ' '.join([self.word, 'filter:safe'])
res = self.twitter.search.tweets(q=query,
tweet_mode='extended',
result_type='recent',
count=100)
for t in res.get('statuses', list()):
tx = t.get('full_text', '##! EMPTY !##')
_id = t.get('id', -1)
# skip faved/id-less tweets
if t.get('favorited', True) or _id < 0:
continue
# skip tweets from blocked users
if t.get('user', dict()).get('id', -1) in targets:
continue
# fav misspelling tweets
if self.__judge(tx):
try:
self.twitter.favorites.create(_id=_id)
LatexBot.logger.info(
'Favorite succeeded (id: {})'.format(_id))
except:
LatexBot.logger.info(
'Already favorited (id: {})'.format(_id))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment