Use Python to download and save the audio of Lazy People's Listening to Books, pure dry goods, no nonsense!

Use Python to crawl to the audio download address, and then use Internet Download Manager to download automatically in batches

# -*- coding: utf-8 -*- 
""" 
Created on Fri Dec 14 21:07:11 2018 
@author: fuwen 
""" 
from subprocess import call 
import os,requests, base64, json, time 
import jsonpath 
import html 
from bs4 import BeautifulSoup 
type=1 
#https://www.lrts.me/album/400485 bookid 
BookID = 99144 
# 
Sections=0 #How 
many chapters in total Chapter 
=92 #Save 
path 
FilePath ='d:\\mp3' 
# Delete all files in the folder and re-create 
def delDir(TitleName): 
    global FilePath 
    FilePath=FilePath+"\\"+TitleName 
    if os.path.exists(FilePath) is True: 
        os.system('RD /q /s' + FilePath) 
    print() 
    os.mkdir(FilePath)
     
#Use 
IDM to install and download IdmPath ='d:\\Internet Download Manager\\IDMan.exe' #Close the 
autoit program to detect the pop-up box and click the program 
exe_name="lrts.exe" 
def kill_exe(exe_name): 
    "" " 
    Kill exe process 
    : param exe_name: process name 
    : return: none 
    """ 
    os.system('taskkill /f /t /im'+exe_name)#MESMTPC.exe program name 
    print("kill process{}". format(exe_name)) 
def get_json_value(json_data,key_name): 
    '''Get the value of any key in json, and the result is in list format''' 
    # key_value = jsonpath.jsonpath(json_data,'$..(key_name)'. format(key_name=key_name)) 
    # https://blog.csdn.net/cling_snail/article/details/80980296 
    key_value = jsonpath.jsonpath(json_data,'$[*].{key_name}'.format(key_name=key_name)) 
    #key's value is not an empty string or empty (empty is always written as empty in the use case) return the corresponding value, otherwise return empty
    
    return key_value
def getChapterTitle(html):
        soup = BeautifulSoup(html, 'html.parser')
        
        
        tilename = soup.find('h1', attrs={"class": "nowrap"})  # 查找span class为red的字符串
        return tilename.text
def IdmDownLoad(DownloadUrl, Mp3Name):
    call([IdmPath, '/d',DownloadUrl,'/p',FilePath,'/f',Mp3Name,'/n'])
def IdmDownLoadChangeName(DownloadUrl, Mp3Name):
    # call([IdmPath, '/d',DownloadUrl,'/p',FilePath,'/n']) 
    begin=DownloadUrl.rfind('/')
    end=DownloadUrl.rfind('?')
    src_name=DownloadUrl[begin+1:end]
    os.rename(FilePath+"\\"+src_name,FilePath+"\\"+Mp3Name) 
def ChangeFileName(filename):
    filename = filename.replace('\\','')
    filename = filename.replace('/','')
    filename = filename.replace(':','')
    filename = filename.replace('*','')
    filename = filename.replace('“','')
    filename = filename.replace('”','')
    filename = filename.replace('<','')
    filename = filename.replace('>','')
    filename = filename.replace('|','')
    filename = filename.replace('?','?')
    filename = filename.replace('(','(')
    filename = filename.replace(chr(65279),'') # UTF-8+BOM
#    print(ord(filename[0]))
    filename = filename.split('(')[0]
    return html.unescape(filename)
if __name__ == "__main__":
    Mp3ListJsonUrl = 'https://www.lrts.me/ajax/album/{0}/{1}/{2}'.format (BookID, Sections, Chapters)
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'}
    TitleUrl = 'https://www.lrts.me/album/{0}'.format(BookID)
    conn = requests.session()
    Mp3ListDic={}
    Mp3ListJson = conn.get(Mp3ListJsonUrl, headers = headers)
    Mp3TitleJson = conn.get(TitleUrl, headers = headers)
    
    titlename=getChapterTitle(Mp3TitleJson.text)
    if titlename is None:
        print("没找到这个题目")
        exit() 
    print(titlename)
    delDir(titlename)
    Mp3ListJson = json.loads(Mp3ListJson.text)
    
    # print(Mp3ListJson["data"]["data"])
    Josndata=Mp3ListJson["data"]["data"]
    Mp3IdList=get_json_value(Josndata,"id")
    # print(Mp3IdList)
    Mp3IdNameList=get_json_value(Josndata,"name")
    for Item in Mp3IdNameList:
        index=Mp3IdNameList.index(Item)
        Mp3ListDic[html.unescape(Item)]=Mp3IdList[index]
        # Mp3ListDic[Mp3IdList[index]]=html.unescape(Item)
    # print(Mp3ListDic)
    Mp3List = Josndata
    Mp3NameList = [Mp3dict['name'] for Mp3dict in Mp3List]
    Mp3NameList = [ChangeFileName(i) for i in Mp3NameList]
    # print(Mp3NameList)
    AlreadyDown = [FileName.replace('.mp3','') for FileName in os.listdir(FilePath)]
    Count = 0
    os.startfile(exe_name)
    time.sleep(2)
    for Mp3Name in Mp3NameList :
        Count+=1
        # if Count==2:
        #     break
        if Mp3Name in AlreadyDown :
            continue
        #type=1&resourcesid=99144&sections=1\
        Mp3JsonUrl="https://www.lrts.me/ajax/playlist/{0}/{1}/{2}".format(type,BookID,Count)
        Mp3Url = conn.get(Mp3JsonUrl, headers = headers)
        # print(Mp3Url.text)
        html=Mp3Url.text
        soup = BeautifulSoup(html, 'html.parser')
        mp3id="section"+str(Mp3ListDic[Mp3Name])
        print(mp3id)
        s1 = soup.find('li', attrs={"id": mp3id}) # Find the string whose span class is red 
        DownloadUrl = s1.find('input')["value"] 
        print(DownloadUrl) 
        try: 
            
            IdmDownLoad (downloadUrl, Mp3Name + '. MP3') 
            the time.sleep (2) 
        the except:
            print('%s, not purchased, skipped...'%Mp3Name) 
    kill_exe(exe_name) 
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757787980818123118103104105888107132109116134127112130118103104105888107

Then use autoit to write a program to close the annoying prompt box of idm

$winTitle="Internet Download Manager"
While 1
	$winHandle = WinGetHandle($winTitle);
	ConsoleWrite(WinGetTitle($winHandle))
	If WinGetTitle($winHandle)=="Internet Download Manager" Then
	WinActivate($winHandle)
	EndIf
	If  WinWaitActive("Internet Download Manager") Then
		;ConsoleWrite($winHandle)
	ControlClick($winHandle, "", "[ID:7]")
	EndIf
	
	
	Sleep(100)
	WEnd

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/109073518
Recommended