remote whitespace from empty lines

This commit is contained in:
Holly 2021-03-09 14:20:30 +00:00
parent 86dc07d64b
commit 68c8f66740
1 changed files with 25 additions and 25 deletions

View File

@ -20,18 +20,18 @@ async def startup():
if not os.path.exists('config.py'): if not os.path.exists('config.py'):
log('Config file not found, copying', Severity.WARNING) log('Config file not found, copying', Severity.WARNING)
shutil.copyfile('config.sample.py', 'config.py') shutil.copyfile('config.sample.py', 'config.py')
import config import config
async with atoot.client(config.instance, access_token=config.access_token) as c: async with atoot.client(config.instance, access_token=config.access_token) as c:
log('Connected to server!') log('Connected to server!')
me = await c.verify_account_credentials() me = await c.verify_account_credentials()
log('Credentials verified!') log('Credentials verified!')
tasks = [] tasks = []
tasks.append(asyncio.create_task(listen(c, me))) tasks.append(asyncio.create_task(listen(c, me)))
tasks.append(asyncio.create_task(repeat(5 * 60, update_followers, c, me))) tasks.append(asyncio.create_task(repeat(5 * 60, update_followers, c, me)))
for t in tasks: for t in tasks:
await t await t
@ -39,7 +39,7 @@ def get_cards(card_names):
cards = [] cards = []
found = [] found = []
images = [] images = []
for name in card_names: for name in card_names:
try: try:
c = scrython.cards.Named(fuzzy=name) c = scrython.cards.Named(fuzzy=name)
@ -47,17 +47,17 @@ def get_cards(card_names):
cards.append(f'{c.name()} - {c.scryfall_uri()}') cards.append(f'{c.name()} - {c.scryfall_uri()}')
except scrython.foundation.ScryfallError: except scrython.foundation.ScryfallError:
cards.append(f'No card named "{name}" was found.') cards.append(f'No card named "{name}" was found.')
if 1 <= len(found) <= 4: if 1 <= len(found) <= 4:
r = requests.get(c.image_uris(0, 'normal'), stream=True) r = requests.get(c.image_uris(0, 'normal'), stream=True)
has_pt = False has_pt = False
try: try:
c.power() c.power()
has_pt = True has_pt = True
except KeyError: except KeyError:
pass pass
description = c.name() description = c.name()
if c.mana_cost(): if c.mana_cost():
description += f' - {c.mana_cost()}' description += f' - {c.mana_cost()}'
@ -66,22 +66,22 @@ def get_cards(card_names):
description += f'\n\n{c.oracle_text()}' description += f'\n\n{c.oracle_text()}'
if has_pt: if has_pt:
description += f'\n\n{c.power()}/{c.toughness()}' description += f'\n\n{c.power()}/{c.toughness()}'
images.append((io.BytesIO(r.content), description)) images.append((io.BytesIO(r.content), description))
return cards, images return cards, images
async def update_followers(c, me): async def update_followers(c, me):
log('Updating followed accounts...') log('Updating followed accounts...')
accounts_following_me = set(map(lambda a: a['id'], await c.account_followers(me))) accounts_following_me = set(map(lambda a: a['id'], await c.account_followers(me)))
accounts_i_follow = set(map(lambda a: a['id'], await c.account_following(me))) accounts_i_follow = set(map(lambda a: a['id'], await c.account_following(me)))
# accounts that follow me that i don't follow # accounts that follow me that i don't follow
to_follow = accounts_following_me - accounts_i_follow to_follow = accounts_following_me - accounts_i_follow
# accounts i follow that don't follow me # accounts i follow that don't follow me
to_unfollow = accounts_i_follow - accounts_following_me to_unfollow = accounts_i_follow - accounts_following_me
if to_follow: if to_follow:
log(f'{len(to_follow)} accounts to follow:') log(f'{len(to_follow)} accounts to follow:')
for account in to_follow: for account in to_follow:
@ -89,7 +89,7 @@ async def update_followers(c, me):
log(f'Followed {account}') log(f'Followed {account}')
else: else:
log('No accounts to follow.') log('No accounts to follow.')
if to_unfollow: if to_unfollow:
log(f'{len(to_unfollow)} accounts to unfollow:') log(f'{len(to_unfollow)} accounts to unfollow:')
for account in to_unfollow: for account in to_unfollow:
@ -103,12 +103,12 @@ async def listen(c, me):
async with c.streaming('user') as stream: async with c.streaming('user') as stream:
async for msg in stream: async for msg in stream:
status = json.loads(msg.json()['payload']) status = json.loads(msg.json()['payload'])
try: try:
# two events come in for the statuses, one of them has the status nested deeper # two events come in for the statuses, one of them has the status nested deeper
# just ignore that one # just ignore that one
if 'status' in status: continue if 'status' in status: continue
status_id = status['id'] status_id = status['id']
status_author = '@' + status['account']['acct'] status_author = '@' + status['account']['acct']
status_text = status['content'] status_text = status['content']
@ -116,33 +116,33 @@ async def listen(c, me):
except: except:
# ignore any events we don't know how to handle # ignore any events we don't know how to handle
continue continue
reply_visibility = min(('unlisted', status_visibility), key=['direct', 'private', 'unlisted', 'public'].index) reply_visibility = min(('unlisted', status_visibility), key=['direct', 'private', 'unlisted', 'public'].index)
media_ids = None media_ids = None
try: try:
card_names = re.findall(r'\[\[(.+?)\]\]', status_text) card_names = re.findall(r'\[\[(.+?)\]\]', status_text)
# ignore any statuses without cards in them # ignore any statuses without cards in them
if not card_names: continue if not card_names: continue
cards, media = get_cards(card_names) cards, media = get_cards(card_names)
reply_text = status_author reply_text = status_author
if len(cards) == 1: if len(cards) == 1:
reply_text += ' ' + cards[0] reply_text += ' ' + cards[0]
else: else:
reply_text += '\n\n' + '\n'.join(cards) reply_text += '\n\n' + '\n'.join(cards)
if media: if media:
media_ids = [] media_ids = []
for image, desc in media: for image, desc in media:
media_ids.append((await c.upload_attachment(fileobj=image, params={}, description=desc))['id']) media_ids.append((await c.upload_attachment(fileobj=image, params={}, description=desc))['id'])
except: except:
reply_text = f'{status_author} Sorry! You broke me somehow. Please let Holly know what you did!' reply_text = f'{status_author} Sorry! You broke me somehow. Please let Holly know what you did!'
log('Sending reply...') log('Sending reply...')
await c.create_status(status=reply_text, media_ids=media_ids, in_reply_to_id=status_id, visibility=reply_visibility) await c.create_status(status=reply_text, media_ids=media_ids, in_reply_to_id=status_id, visibility=reply_visibility)