Created
May 15, 2022 09:33
-
-
Save Akkiesoft/3e8830e15bd943b54738fd973f0b646e to your computer and use it in GitHub Desktop.
Pixivの画像ファイルを作者のTwitterIDごとに振り分けるやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from html.parser import HTMLParser | |
import urllib.request | |
import json | |
import sys | |
import os | |
import time | |
import shutil | |
from pixiv_shiwake_config import dir_twitterid, dir_pixivid, user_id_shiwake_map | |
ua = 'curl/7.64.0' | |
class MyHTMLParser(HTMLParser): | |
def __init__(self): | |
super().__init__() | |
self.json = False | |
def handle_starttag(self, tag, attrs): | |
got_data = False | |
if tag == 'meta': | |
for attr in attrs: | |
if attr[0] == 'name' and 'preload-data': | |
got_data = True | |
if got_data: | |
for attr in attrs: | |
if attr[0] == 'content': | |
self.json = attr[1] | |
if len(sys.argv) < 2: | |
print("usage: %s [opts] <pixiv illust file>"%sys.argv[0]) | |
print("option:\n -o: open the destination directory that the image file is moved") | |
sys.exit(1) | |
illust_file = sys.argv[-1] | |
illust_id = sys.argv[-1].split("/")[-1].split("_")[0] | |
opts = sys.argv[1:-1] | |
# Get user's Pixiv ID | |
req = urllib.request.Request('https://www.pixiv.net/artworks/%s' % illust_id) | |
req.add_header('User-Agent', ua) | |
with urllib.request.urlopen(req) as r: | |
html = r.read() | |
parser = MyHTMLParser() | |
parser.feed(html.decode()) | |
if parser.json: | |
data = json.loads(parser.json) | |
pixiv_user_id = next(iter(data['user'])) | |
time.sleep(1) | |
# Get user's Twitter account or Pixiv ID | |
twitter = False | |
req = urllib.request.Request('https://www.pixiv.net/users/%s' % pixiv_user_id) | |
req.add_header('User-Agent', ua) | |
with urllib.request.urlopen(req) as r: | |
html = r.read() | |
parser = MyHTMLParser() | |
parser.feed(html.decode()) | |
if parser.json: | |
data = json.loads(parser.json) | |
user = list(data['user'].values())[0] | |
if 'social' in user and 'twitter' in user['social']: | |
twitter = user['social']['twitter']['url'].split("/")[-1] | |
if twitter and not pixiv_user_id in user_id_shiwake_map: | |
target = "%s/%s"%(dir_twitterid,twitter) | |
else: | |
if pixiv_user_id in user_id_shiwake_map: | |
target = "%s/%s"%(dir_twitterid,user_id_shiwake_map[pixiv_user_id]) | |
else: | |
target = "%s/%s"%(dir_pixivid,pixiv_user_id) | |
illust_destination = os.path.join(target, sys.argv[-1].split("/")[-1]) | |
if os.path.isfile(illust_destination): | |
print("もうある") | |
os.system("open -R %s"%illust_destination) | |
sys.exit(0) | |
os.makedirs(target, exist_ok=True) | |
shutil.move(illust_file, target) | |
if '-o' in opts: | |
os.system("open -R %s"%illust_destination) | |
print("%s は %s に仕分けられました"%(illust_file, target)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
dir_twitterid = '/Users/me/Pictures/pixiv_illusts/TwitterID' | |
dir_pixivid = '/Users/me/Pictures/pixiv_illusts/PixivID' | |
# ユーザー情報にTwitterIDがないときにTwitterIDに振り分けてほしいときの設定 | |
user_id_shiwake_map = { | |
'12345678': 'hogehogeuser', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment