feat(scripts): 增加 instagram 同步 mastodon 脚本
增加 instagram 同步 mastodon 脚本 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
1bea995dc1
commit
b4b5a0e68a
@ -47,3 +47,4 @@ if __name__ == '__main__':
|
|||||||
new_name = '%s-gray.%s' % (img_path.split('.')[0], img_path.split('.')[-1])
|
new_name = '%s-gray.%s' % (img_path.split('.')[0], img_path.split('.')[-1])
|
||||||
plt.savefig(new_name)
|
plt.savefig(new_name)
|
||||||
print(new_name)
|
print(new_name)
|
||||||
|
plt.close()
|
||||||
|
|||||||
73
scripts/ins2mastodon.py
Normal file
73
scripts/ins2mastodon.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# scraper instagram posts
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dsite.settings")
|
||||||
|
sys.path.insert(0, '../')
|
||||||
|
sys.path.insert(0, './')
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
get_wsgi_application()
|
||||||
|
|
||||||
|
import pickle
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from mastodon import Mastodon
|
||||||
|
from instagram_private_api import Client
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from dsite import settings
|
||||||
|
|
||||||
|
mastodon_cli = Mastodon(access_token='Ug_bUMWCk3RLamOnqYIytmeB0nO6aNfjdmf06mAj2bE', api_base_url='https://nofan.xyz')
|
||||||
|
|
||||||
|
|
||||||
|
def send_image_to_mastodon(image_url, text):
|
||||||
|
resp = requests.get(image_url)
|
||||||
|
mime_type = 'image/jpeg'
|
||||||
|
url = urlparse(image_url)
|
||||||
|
ext = url.path.split('.')[-1]
|
||||||
|
if ext == 'gif':
|
||||||
|
mime_type = 'image/gif'
|
||||||
|
toot_resp = mastodon_cli.media_post(resp.content, mime_type)
|
||||||
|
if toot_resp.get('id'):
|
||||||
|
media_ids = [toot_resp['id']]
|
||||||
|
mastodon_cli.status_post(text, media_ids=media_ids)
|
||||||
|
|
||||||
|
|
||||||
|
# write binary file with api.settings
|
||||||
|
def writeSettings(user, pwd, settings_file):
|
||||||
|
api = Client(user, pwd)
|
||||||
|
with open(settings_file, "wb") as FileObj:
|
||||||
|
pickle.dump(api.settings, FileObj)
|
||||||
|
|
||||||
|
|
||||||
|
# read binary file to api.settings
|
||||||
|
def readSettings(settings_file):
|
||||||
|
cache = None
|
||||||
|
with open(settings_file, "rb") as FileObj:
|
||||||
|
cache = pickle.load(FileObj)
|
||||||
|
return cache
|
||||||
|
|
||||||
|
|
||||||
|
if not os.path.exists(settings.IG_PRIVATE_API_SETTINGS):
|
||||||
|
writeSettings(settings.IG_LOGIN_USERNAME, settings.IG_LOGIN_PASSWORD, settings_file)
|
||||||
|
|
||||||
|
cache_settings = readSettings(settings.IG_PRIVATE_API_SETTINGS)
|
||||||
|
api = Client(settings.IG_LOGIN_USERNAME, settings.IG_LOGIN_PASSWORD, settings=cache_settings)
|
||||||
|
|
||||||
|
results = api.self_feed()
|
||||||
|
for item in results['items']:
|
||||||
|
text = item['caption']['text']
|
||||||
|
image_url = item['image_versions2']['candidates'][0]['url']
|
||||||
|
try:
|
||||||
|
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:
|
||||||
|
send_image_to_mastodon(image_url, text)
|
||||||
|
with open(settings.MASTODON_SYNCED_IMAGES_LOG, 'a') as f:
|
||||||
|
f.write(image_url + '\n')
|
||||||
|
except:
|
||||||
|
print('error')
|
||||||
42
scripts/ins_scraper.py
Executable file
42
scripts/ins_scraper.py
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# scraper instagram posts
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dsite.settings")
|
||||||
|
sys.path.insert(0, '../')
|
||||||
|
sys.path.insert(0, './')
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
get_wsgi_application()
|
||||||
|
|
||||||
|
import instagram_scraper
|
||||||
|
import time
|
||||||
|
from dsite import settings
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
args = {
|
||||||
|
'login_user': settings.IG_LOGIN_USERNAME,
|
||||||
|
'login_pass': settings.IG_LOGIN_PASSWORD,
|
||||||
|
'cookiejar': settings.IG_SCRAPER_COOKIE_FILE,
|
||||||
|
'latest_stamps': settings.IG_SCRAPER_LAST_STAMP_FILE,
|
||||||
|
'usernames': settings.IG_SCRAPER_USERNAMES,
|
||||||
|
'destination': settings.IG_SCRAPER_DESTINATION,
|
||||||
|
}
|
||||||
|
|
||||||
|
scraper = instagram_scraper.InstagramScraper(**args)
|
||||||
|
scraper.authenticate_with_login()
|
||||||
|
scraper.save_cookies()
|
||||||
|
while True:
|
||||||
|
time.sleep(10)
|
||||||
|
args_ = {
|
||||||
|
'cookiejar': settings.IG_SCRAPER_COOKIE_FILE,
|
||||||
|
'latest_stamps': settings.IG_SCRAPER_LAST_STAMP_FILE,
|
||||||
|
'usernames': settings.IG_SCRAPER_USERNAMES,
|
||||||
|
'destination': settings.IG_SCRAPER_DESTINATION,
|
||||||
|
'template': '{username}-{datetime}-{shortcode}',
|
||||||
|
}
|
||||||
|
scraper = instagram_scraper.InstagramScraper(**args_)
|
||||||
|
scraper.scrape()
|
||||||
Loading…
x
Reference in New Issue
Block a user