]>
Commit | Line | Data |
---|---|---|
1 | import | |
2 | httpClient, | |
3 | json, | |
4 | re, | |
5 | strformat, | |
6 | strutils | |
7 | ||
8 | let PEERTUBE_REGEX = re"w\/[0-9a-zA-z]{22}" | |
9 | ||
10 | proc getPeerTubeMagnetLink*(url: string, musicOnly: bool): string = | |
11 | ## Gets the magnet link of the best possible resolution from PeerTube | |
12 | let uuid = url.substr(find(url, PEERTUBE_REGEX) + "w/".len) | |
13 | let domainName = url.substr(8, find(url, '/', start=8) - 1) | |
14 | let apiURL = &"https://{domainName}/api/v1/videos/{uuid}" | |
15 | let client = newHttpClient() | |
16 | let response = get(client, apiURL) | |
17 | let jsonNode = parseJson($response.body) | |
18 | var files = jsonNode["files"] | |
19 | if len(jsonNode["files"]) == 0: | |
20 | files = jsonNode["streamingPlaylists"][0]["files"] | |
21 | if musicOnly: | |
22 | files[len(files)-1]["magnetUri"].getStr() | |
23 | else: | |
24 | files[0]["magnetUri"].getStr() |