you2peer/src/mirror.py

71 lines
2.3 KiB
Python
Executable File

#!/usr/bin/python3
"""
Mirror YouTube playlist
(all channel content for instance)
into PeerTube playlist
using PeerTube API and youtube-dl
"""
import requests
import json
import os
with open("config.json", 'r') as f:
config = json.load(f)
def update_mirror(channel):
name = channel['name']
playlist_url = channel['url']
os.system(f"youtube-dl '{playlist_url}' --flat-playlist --dump-single-json | jq . > 'data/{name}.playlist.last.json'")
base_yt_url = "https://www.youtube.com/watch?v="
added_previous = []
if os.path.exists(f"data/{name}.playlist.previous.json"):
with open(f"data/{name}.playlist.previous.json", 'r') as f:
playlist_previous = json.load(f)
for entry in playlist_previous['entries']:
video_id = entry['id']
added_previous.append(video_id)
urls = []
with open(f"data/{name}.playlist.last.json", 'r') as f:
playlist = json.load(f)
for entry in playlist['entries']:
if not entry['id'] in added_previous:
url = base_yt_url + entry['url']
urls.append(url)
os.rename(f"data/{name}.playlist.last.json", f"data/{name}.playlist.previous.json")
# print(urls)
TOKEN = config['peertube']['access_token']
hed = {'Authorization': 'Bearer ' + TOKEN}
i = 1
n = len(urls)
for url in urls:
payload = {
'channelId': channel['channel_id'],
'targetUrl': url,
'privacy': 1,
'waitTranscoding': True
}
req = f"https://{config['peertube']['base_url']}/api/v1/videos/imports"
response = requests.post(req, json=payload, headers=hed)
# print(response.content)
video_data = response.json()
video_id = video_data['video']['id']
url = f"https://{config['peertube']['base_url']}/api/v1/video-playlists/{channel['playlist_id']}/videos"
payload = {
'videoId': video_id
}
response = requests.post(url, json=payload, headers=hed)
print(response.status_code)
print("{0}/{1} done ({2:.2f}%)".format(i, n, i/n * 100))
i += 1
def main():
channels = config['you2peer']['channels']
for channel in channels:
update_mirror(channel)
if __name__ == "__main__":
main()