Created
June 26, 2017 02:36
-
-
Save fireattack/df1f1af23d67c08845e2dfb311875374 to your computer and use it in GitHub Desktop.
Batch rename CG pack
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
import re | |
from os import listdir, rename, makedirs | |
from os.path import join, splitext, basename, exists | |
from urllib.parse import quote | |
from bs4 import BeautifulSoup as bs | |
from requests import get # to make GET request | |
def advrename(original_full_path, new_filename, c): | |
new_full_path = join(pathforrenamed, new_filename) | |
if basename(original_full_path) == new_filename: | |
print('No rename needed; file only moved.') | |
if c != 'simulate': | |
rename(original_full_path, new_full_path) | |
else: | |
if c != 'simulate': | |
rename(original_full_path, new_full_path) | |
string = basename(original_full_path) + '->' + new_filename | |
print(string) | |
logfile = open(join(mypath, "log.txt"), "a", encoding='utf-8-sig') | |
logfile.write(string + '\n') | |
logfile.close() | |
mypath = 'H:\CG' | |
pathforrenamed = join(mypath, '_renamed') | |
if not exists(pathforrenamed): | |
makedirs(pathforrenamed) | |
myzipfiles = [join(mypath, f) for f in listdir(mypath) if (splitext(f)[1].lower() in ['.zip', '.7z', '.rar'])] | |
for original_full_path in myzipfiles: | |
original_filename = basename(original_full_path) | |
print('Processing ' + original_filename) | |
p = re.compile( | |
r'^(\((?P<prefix>.+)\))* *(\[(?P<date>\d+)\])* *(\[(?P<brand>[^][]+)\])* *(?P<name>[^[].+[^) ]) *(\((?P<format>.+)\))*\.(?P<extension>(.+?))$') | |
m = p.search(original_filename) | |
existing_prefix = m['prefix'] | |
if existing_prefix is not None and ('同人' in existing_prefix or 'CG' not in existing_prefix): | |
print('Prefix "{0}" is not CG pack, ignored.'.format(existing_prefix)) | |
continue | |
else: | |
existing_prefix = '18禁ゲームCG' # Assume CG by default | |
existing_date = m['date'] | |
if existing_date is not None: | |
if existing_date.startswith('20') or existing_date.startswith('19'): | |
existing_date = existing_date[2:] | |
existing_brand = m['brand'] | |
existing_name = m['name'] | |
existing_format = m['format'] | |
existing_extension = m['extension'] | |
if existing_date is None or existing_brand is None: | |
print('Missing date or brand, searching Getchu.') | |
keyword = existing_name.replace('~', '〜').replace('-', '') # replace dash and remove '-' | |
print('Searching ' + keyword) | |
keyword = quote(keyword, safe='/', encoding='EUC-JP') | |
url = 'http://www.getchu.com/php/nsearch.phtml?keyword_option_flag=1&genre=pc_soft&search_keyword=' + keyword | |
html = get(url) | |
if html.status_code == 200: | |
html.encoding = 'EUC-JP' | |
soup = bs(html.text, "lxml") | |
ul = soup.find('ul', {"class": "display"}) | |
lis = ul.find_all('li') | |
if lis: | |
li = ul.find_all('li')[-1] | |
name = re.search(r'blueb.+?>(.+?)</a>', str(li)).group(1).strip() | |
if name.split(' ')[-1].endswith('版') and len(name.split(' ')) > 1: | |
name = re.match(r'(.+) ([^ ]+$)', name)[1] | |
if name.endswith('>'): | |
name = re.match(r'(.+)<(.+?>$)', name)[1] | |
name = name.replace(' [Getchu.com限定販売]', '') | |
date = re.search(r"発売日:(.+?)<", str(li)).group(1) | |
date = date.replace('/', '')[2:] | |
brand = re.search(r"ブランド名:.+?>(.+?)</a", str(li), re.DOTALL).group(1) | |
new_filename = '(18禁ゲームCG) [' + date + '] [' + brand + '] ' + name | |
if existing_format is not None: | |
new_filename += ' (' + existing_format + ')' | |
new_filename += '.' + existing_extension | |
advrename(original_full_path, new_filename, 'go') | |
else: | |
print('No such title found on Getchu. Ignored.') | |
else: | |
new_filename = '(' + existing_prefix + ') [' + existing_date + '] [' + existing_brand + '] ' + existing_name | |
if existing_format is not None: | |
new_filename += ' (' + existing_format + ')' | |
new_filename += '.' + existing_extension | |
advrename(original_full_path, new_filename, 'go') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment