Last active
February 20, 2019 14:48
-
-
Save ivang7/a20b50a952548e250c7a16ced6c948e3 to your computer and use it in GitHub Desktop.
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
import pprint | |
from lxml import html | |
import requests | |
import urllib | |
import base64 | |
import json | |
def GetInformationByPublicLink(link): | |
cloudPage = requests.get(link) | |
if cloudPage.status_code != 200: | |
raise ConnectionError('Error get page, with code ' + str(cloudPage.status_code)) | |
scriptBlocks = html.fromstring(cloudPage.content.decode('utf-8', 'ignore')).xpath("//script") | |
for scriptBlock in scriptBlocks: | |
if "cloudSettings" in str(scriptBlock.text): | |
jsonFromPage = scriptBlock.text | |
break | |
if jsonFromPage is None: | |
raise ValueError('Page not have block with files') | |
jsonFromPage = str(jsonFromPage[jsonFromPage.find("{"):]) | |
jsonFromPage = jsonFromPage.replace("\\x3c", "<") | |
if jsonFromPage[len(jsonFromPage)-1] == ";" : | |
jsonFromPage = jsonFromPage[:-1] | |
jsonFromPage = json.loads(jsonFromPage) | |
jsonFromPage = jsonFromPage['folders']['folder']['list'] | |
return jsonFromPage | |
def GetFilesByExtension(listOfFiles, extension="mkv"): | |
extension = "." + extension | |
extLen = len(extension) * -1 | |
return [cloudFile for cloudFile in listOfFiles | |
if cloudFile["type"] == "file" and | |
extension == cloudFile["name"][extLen:] | |
] | |
def GetPlaylistsDifferentQuality(cloudRecord, server = "https://cloclo14.cloud.mail.ru"): | |
weblink = cloudRecord["weblink"] | |
urlEncodeWeblink = urllib.parse.quote(weblink) | |
b64Weblink = base64.b64encode(bytes(urlEncodeWeblink,'utf-8')).decode('utf-8', 'ignore') | |
playlistUrl = server + "/videowl/0p/" + b64Weblink + ".m3u8?double_encode=1" | |
playlistContent = requests.get(playlistUrl).content.decode('utf-8', 'ignore') | |
resolution = "" | |
playlists = {} | |
for line in playlistContent.split("\n"): | |
if line == "" or line == "#EXTM3U": | |
continue | |
elif "#EXT-X-STREAM-INF" in line and resolution == "": | |
for param in line.split(" "): | |
if "RESOLUTION" in param: | |
resolution = param.split("=")[1] | |
elif line[0] == "/" and resolution != "": | |
playlists.update({resolution : server + line}) | |
resolution = "" | |
else: | |
raise ValueError("Error, uncorrect playlist, cannot parse, content:\n" + playlistContent) | |
return playlists | |
def GetCleanPlaylistContent(linkToPlaylist): | |
url = list(linkToPlaylist.values())[0] | |
playlistContent = requests.get(url).content.decode('utf-8', 'ignore') | |
urlBase = url[9:] | |
urlBase = url[:urlBase.find("/")+9] | |
content = "" | |
for line in playlistContent.split("\n"): | |
if line != "#EXT-X-DISCONTINUITY": | |
if len(line) > 0 and line[0] != "#": | |
line = urlBase + line | |
content += line + "\n" | |
return content[:-1] | |
if __name__ == '__main__': | |
listCloudFiles = GetInformationByPublicLink("https://cloud.mail.ru/public/9NT8/cdboEh7SY") | |
listOfMkv = GetFilesByExtension(listCloudFiles) | |
print("all files " + str(len(listCloudFiles))) | |
print("mkv file "+ str(len(listOfMkv))) | |
allPlayList = "#EXTM3U" | |
for videoFile in listOfMkv: | |
name = videoFile["name"] | |
playlists = GetPlaylistsDifferentQuality(videoFile) | |
for quality,url in playlists.items(): | |
if "x240" not in quality: continue | |
#lowQualityVideo = [{key:value} for key,value in playlists.items() if "x240" in key][0] | |
content = GetCleanPlaylistContent({quality:url}) | |
filename = quality + "_" + name + ".m3u" | |
f = open(filename, "w") | |
f.write(content) | |
f.close() | |
allPlayList += "\n#EXTINF:-1," + name + "\n" | |
allPlayList += filename | |
f = open("allPlayList.m3u", "w") | |
f.write(allPlayList) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment