feat(scripts): 修复可能导致重复发送图片的问题;修改 logger 位置

修复可能导致重复发送图片的问题;修改 logger 位置

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2022-02-11 22:13:17 +08:00
parent 1cc841f3cc
commit f44ced4946
2 changed files with 11 additions and 6 deletions

View File

@ -147,3 +147,5 @@ MASTODON_SYNCED_IMAGES_LOG = ''
IG_PRIVATE_API_SETTINGS = ''
IG_LOGIN_USERNAME = ''
IG_LOGIN_PASSWORD = ''
MASTODON_NOFAN_ACCESS_TOKEN = ''

View File

@ -22,8 +22,9 @@ import logging
from dsite import settings
logger = logging.getLogger('dsite.' + __name__)
mastodon_cli = Mastodon(access_token='Ug_bUMWCk3RLamOnqYIytmeB0nO6aNfjdmf06mAj2bE', api_base_url='https://nofan.xyz')
logging.basicConfig(filename='/root/develop/log/ins2mastodon.log', level=logging.INFO)
logger = logging.getLogger('/root/develop/log/ins2mastodon.log')
mastodon_cli = Mastodon(access_token=settings.MASTODON_NOFAN_ACCESS_TOKEN, api_base_url='https://nofan.xyz')
def send_image_to_mastodon(image_url, text):
@ -37,7 +38,10 @@ def send_image_to_mastodon(image_url, text):
if toot_resp.get('id'):
media_ids = [toot_resp['id']]
mastodon_cli.status_post(text, media_ids=media_ids)
print(text)
logger.info('send %s', text)
image_name = urlparse(url).path.split('/')[-1]
with open(settings.MASTODON_SYNCED_IMAGES_LOG, 'a') as f:
f.write(image_name + '\n')
# write binary file with api.settings
@ -73,10 +77,9 @@ if __name__ == '__main__':
with open(settings.MASTODON_SYNCED_IMAGES_LOG, 'r') as f:
send_images = f.readlines()
send_images = [x.strip() for x in send_images]
if image_url not in send_images:
image_name = urlparse(image_url).path.split('/')[-1]
if image_name not in send_images:
send_image_to_mastodon(image_url, text)
with open(settings.MASTODON_SYNCED_IMAGES_LOG, 'a') as f:
f.write(image_url + '\n')
except Exception as e:
logger.error(e)
time.sleep(60)