336831.jpg

nideii

一个音声改名脚本

你可能会说这个写的什么玩意;尝试压缩代码,怕太长不能发出来,只能弄到1900字。            

用过代理抓取DLSite页面,然后修改文件名。代理默认是 socks5://localhost:10808, V2RayN 默认开启。

第三方库依赖
复制代码
  1. pip install lxml pysocks requests beautifulsoup4


顺便把目录下只有一个文件夹的目录结构进行调整,减少了没有必要打开目录。        

例子:
[RJ123456] FolderA
    - Subfolder1
        - Subfolder2
            -  SubfolderN
               - OtherFolders
                   - Files
               - MoreFiles

调整为:
[RJ123456] FolderA
    - OtherFolders
        - Files
    - MoreFiles


我在下后知后觉dalao整理的2T音声。看上去挺多人在下载音声,我就把这脚本发出来了。        

复制代码
  1. import os, re, random, shutil, string
  2. import requests
  3. from bs4 import BeautifulSoup
  4. def safe_filename(fname):
  5.     for k, v in {
  6.         '<': '<', '>': '>', ':': ':', '"': '"', '/': '/',
  7.         '\\': '\', '|': '|', '?': '?', '*': '*',
  8.     }.items(): fname = fname.replace(k, v)
  9.     return fname
  10. def move_to_parent(fp):
  11.     fp = os.path.abspath(fp)
  12.     parent = os.path.dirname(fp)
  13.     temp_name = os.path.join(parent, ''.join(random.choices(string.ascii_uppercase + string.digits, k=32)))
  14.     os.rename(fp, temp_name)
  15.     for f in os.listdir(fp): shutil.move(os.path.join(temp_name, f), parent)
  16.     os.rmdir(temp_name)
  17. for fp in os.listdir():
  18.     if not os.path.isdir(fp): continue
  19.     rjid = re.match(r'^(RJ[0-9]+)', fp)
  20.     if rjid is None: rjid = re.search(r'\[(RJ[0-9]+)\]', fp)
  21.     if rjid is None: continue
  22.     rjid = rjid[1]
  23.     r = requests.get(f'https://www.dlsite.com/maniax/work/=/product_id/{rjid}.html', proxies={
  24.         'https': 'socks5://localhost:10808'
  25.     }, headers={
  26.         'Accept': 'text/html',
  27.         'Accept-Language': 'ja',
  28.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
  29.         'DNT': '1',
  30.     })
  31.     if not r.ok: continue
  32.     doc = BeautifulSoup(r.text, 'lxml')
  33.     maker = doc.select_one('#work_maker span.maker_name').text.strip()
  34.     title = doc.select_one('#work_name > a').text.strip()
  35.     cv = []
  36.     for row in doc.select('#work_outline > tr'):
  37.         if row.select_one('th').text.strip() != '声優': continue
  38.         cv = [a.text.strip() for a in row.select('td > a')]
  39.     cv = '/'.join(cv[:5])
  40.     if len(cv): fname = safe_filename(f'[{maker}][{rjid}][{cv}] {title}')
  41.     else: fname = safe_filename(f'[{maker}][{rjid}] {title}')
  42.     
  43.     while True:
  44.         sub_entries = os.listdir(fp)
  45.         if len(sub_entries) > 1: break
  46.         sub = os.path.join(fp, sub_entries[0])
  47.         if not os.path.isdir(sub): break
  48.         move_to_parent(sub)
  49.     if fp != fname: os.rename(fp, fname)

1303680.jpg

accelerate12138

B1F  2021-01-19 13:11
(accelerate12138)
自己写代码最恨写注释,看别人的代码最恨别人不写注释.jpg   

1024859.png

alice

B2F  2021-01-19 14:07
(笑止千万)
1L很真实。。

none.gif

扣1送地狱火

用户被禁言,该主题自动屏蔽!

726521.png

kujisanfun

B4F  2021-01-19 15:14
(cc)
实验了一下,我才知道如果:后只有一行代码,还真不需要换行缩进

另外想问个问题,为什么我用RedisDesktopManager可以连接localhost:6379,但浏览器不行
redis设置的开机自启应该没毛病

相对的MongoDB没啥事,27017能访问

1294378.jpg

笃笃

B5F  2021-01-24 19:45
(变成美少女吧!)
看不懂