Skip to content

Instantly share code, notes, and snippets.

@istallia
Created March 14, 2020 12:21
Show Gist options
  • Save istallia/c0aa6c5afc4dfd8f98074302aa395274 to your computer and use it in GitHub Desktop.
Save istallia/c0aa6c5afc4dfd8f98074302aa395274 to your computer and use it in GitHub Desktop.
コモンズ素材自動命名スクリプト(Python)
#!python3
# coding: utf-8
# 必要なモジュール
import os.path
import time
import sys
import re
from urllib.request import urlopen
from bs4 import BeautifulSoup
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler
from pprint import pprint
# イベントハンドラの用意
class MyHandler(RegexMatchingEventHandler):
def __init__(self, regexes):
super(MyHandler, self).__init__(regexes=regexes, ignore_directories=True)
def on_created(self, event):
if not os.path.isfile(event.src_path):
return
res = re.search('(nc\\d{4,8})\\.', event.src_path)
if res is not None:
ID = res.groups()[0]
print('コモンズIDを持つファイルが検出されました: '+ID)
title = self.get_title(ID)
if title is not None:
os.rename(os.path.basename(event.src_path), ID+'_'+re.sub(r'[\\/:*?"<>|]+','',title)+'.'+os.path.splitext(event.src_path)[1])
def get_title(self, ncID, count=0):
if count > 4:
print('['+ncID+'] 再挑戦中止')
return None
try:
html = urlopen('http://commons.nicovideo.jp/material/'+ncID).read()
soup = BeautifulSoup(html, 'html.parser')
return soup.select('div.commons_title')[0].text
except urllib.error.HTTPError as e:
print('['+ncID+'] 失敗('+str(e.code)+')、再挑戦')
time.sleep(1)
return get_title(ncID, count+1)
def on_modified(self, event):
self.on_created(event)
# ファイルパスの用意
filepath = ''
if len(sys.argv) < 2:
print('監視したいディレクトリのフルパスを入力: ', end='')
filepath = input().strip()
else:
filepath = sys.argv[1]
if not os.path.isdir(filepath):
print('ERROR: 指定されたディレクトリが存在しません。')
sys.exit(1)
filepath = os.path.abspath(filepath)
print('監視を開始: '+filepath)
# 監視の開始
event_handler = MyHandler(regexes=['.*nc\\d{4,8}\\.'])
observer = Observer()
observer.schedule(event_handler, filepath, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment