Last active
December 7, 2018 04:14
-
-
Save rubyu/550f482f26f490f1006c to your computer and use it in GitHub Desktop.
Rename RJxxxxxx.zip files downloaded from dlsite.com
This file contains 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
@echo off | |
:loop | |
IF "%1"=="" GOTO completed | |
C:\Python27\python.exe C:\Users\user\Documents\BTSync\rubyu\code\rrj\rrj.py %1 | |
SHIFT | |
GOTO loop | |
:completed |
This file contains 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
# -*- coding: utf-8 -*- | |
''' | |
Created on 2015/05/08 | |
@author: user | |
''' | |
import sys | |
import os | |
import urllib2 | |
from bs4 import BeautifulSoup | |
def work_information_provider(): | |
def dlsite_soft(str): | |
page = urllib2.urlopen("http://www.dlsite.com/soft/work/=/product_id/%s.html" % str) | |
soup = BeautifulSoup(page) | |
title = soup.find(id="work_name") | |
for elm in title.findAll("span"): | |
elm.extract() | |
author = soup.find(id="work_maker").find(attrs={"class", "maker_name"}) | |
return "[%s] %s" % (author.text, title.text) | |
def dlsite_pro(str): | |
page = urllib2.urlopen("http://www.dlsite.com/pro/work/=/product_id/%s.html" % str) | |
soup = BeautifulSoup(page) | |
title = soup.find(id="work_name") | |
for elm in title.findAll("span"): | |
elm.extract() | |
author = soup.find(id="work_maker").find(attrs={"class", "maker_name"}) | |
return "[%s] %s" % (author.text, title.text) | |
def dlsite_home(str): | |
page = urllib2.urlopen("http://www.dlsite.com/home/work/=/product_id/%s.html" % str) | |
soup = BeautifulSoup(page) | |
title = soup.find(id="work_name") | |
for elm in title.findAll("span"): | |
elm.extract() | |
author = soup.find(id="work_maker").find(attrs={"class", "maker_name"}) | |
return "[%s] %s" % (author.text, title.text) | |
def dlsite_maniax(str): | |
page = urllib2.urlopen("http://www.dlsite.com/maniax/work/=/product_id/%s.html" % str) | |
soup = BeautifulSoup(page) | |
title = soup.find(id="work_name") | |
for elm in title.findAll("span"): | |
elm.extract() | |
author = soup.find(id="work_maker").find(attrs={"class", "maker_name"}) | |
return "[%s] %s" % (author.text, title.text) | |
yield dlsite_maniax | |
yield dlsite_home | |
yield dlsite_pro | |
yield dlsite_soft | |
def escape_filename(str): | |
#remove last period or blank | |
buf = [] | |
stop = False | |
for s in reversed(str): | |
if not stop: | |
if s == "." or \ | |
s == " " or \ | |
s == "\t": | |
continue | |
else: | |
stop = True | |
buf.insert(0, s) | |
str = "".join(buf) | |
#replace character of ascii 0-31 | |
buf = [] | |
for s in str: | |
if 31 < ord(s): | |
buf.append(s) | |
else: | |
buf.append("_") | |
str = "".join(buf) | |
#replace reserved character | |
str = str.replace("\\", u"¥") | |
str = str.replace("/", u"/") | |
str = str.replace(":", u":") | |
str = str.replace("*", u"*") | |
str = str.replace("?", u"?") | |
str = str.replace("\"", u"”") | |
str = str.replace("<", u"<") | |
str = str.replace(">", u">") | |
str = str.replace("|", u"|") | |
#delete if reserved name | |
if str == "AUX" or \ | |
str == "CLOCK$" or \ | |
str == "COM1" or str == "COM2" or \ | |
str == "COM3" or str == "COM4" or \ | |
str == "COM5" or str == "COM6" or \ | |
str == "COM7" or str == "COM8" or \ | |
str == "COM9" or \ | |
str == "CON" or \ | |
str == "CONFIG$" or \ | |
str == "LPT1" or str == "LPT2" or \ | |
str == "LPT3" or str == "LPT4" or \ | |
str == "LPT5" or str == "LPT6" or \ | |
str == "LPT7" or str == "LPT8" or \ | |
str == "LPT9" or \ | |
str == "NUL" or \ | |
str == "PRN": | |
str = "" | |
#delete if .. or . | |
if str == "." or str == "..": | |
str = "" | |
#if empty | |
if str == "": | |
str = "(empty)" | |
return str | |
def main(): | |
if len(sys.argv) == 1: | |
sys.exit("target path is not given") | |
path = unicode(sys.argv[1], "cp932") | |
if not os.path.exists(path): | |
sys.exit("given string is not a valid path") | |
parent, rename_from = os.path.split(path) | |
name, ext = os.path.splitext(rename_from) | |
for provider in work_information_provider(): | |
try: | |
res = provider(name) | |
rename_to = escape_filename(name + " " + res) | |
os.rename(path, os.path.join(parent, rename_to) + ext) | |
break | |
except: | |
pass | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to install:
Requirements: