Last active
March 14, 2022 23:47
-
-
Save Akkiesoft/e6f1fe05dd5ad05bcfbfd9ae6eedc6b6 to your computer and use it in GitHub Desktop.
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 | |
dir_twitterid = '/path/to/pixiv-siwake/TwitterID' | |
dir_pixivid = '/path/to/pixiv-siwake/PixivID' | |
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', 'curl/7.64.1') | |
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', 'curl/7.64.1') | |
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: | |
target = "%s/%s"%(dir_twitterid,twitter) | |
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("もうある") | |
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment