yt-to-lbry.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. # This program gathers data about a YT channel's videos or any YT link
  3. # using yt-dl or yt-dlp and then mirrors it to LBRY. yt-dlp
  4. # theoretticaly can be used to mirror from sites like peertube but it
  5. # probalby won't work as well. So right now, this program tries to
  6. # only accept YT channel links or YT watch links. Most invidious links
  7. # should also work.
  8. # Commands:
  9. # python yt-to-lbry.py <YT CHANNEL URL>
  10. # mirror all of the videos from the channel to LBRY
  11. # WARNING: Takes a while and requires a lot of space depending on the
  12. # size of the channel
  13. #
  14. # python yt-to-lbry.py <YT VIDEO URL>
  15. # mirror the video to LBRY
  16. # in the future make it so you can get just *1* of the new videos
  17. # from a channel
  18. #command = f"{downloader} --get-title --get-description --get-thumbnail --get-id https://youtube.com/channel/{channel_id} --playlist-end 1"
  19. import requests
  20. import sys
  21. import json
  22. import os
  23. import subprocess
  24. import re
  25. import tempfile
  26. import platform
  27. import random
  28. import time
  29. downloader = "yt-dlp"
  30. lbrynet = "lbrynet"
  31. temp_dir = tempfile.TemporaryDirectory().name
  32. if subprocess.getoutput(f"{lbrynet} version") == "Could not connect to daemon. Are you sure it's running?":
  33. print('It looks like lbrynet has not started yet. In another terminal window/tab do "lbrynet start" and rerun this script.')
  34. quit()
  35. url = ""
  36. try:
  37. url = sys.argv[1]
  38. except:
  39. while not url:
  40. url = input("YT URL: ")
  41. channels = subprocess.getoutput(f"{lbrynet} channel list")
  42. json_stuff = json.loads(channels)
  43. for i, channel in enumerate(json_stuff["items"]):
  44. print(i, "|", channel["name"])
  45. c = 100000
  46. while not c >= 0 or not c <= i:
  47. c = input('Select a channel from 0-'+str(i)+': ')
  48. try:
  49. c = int(c)
  50. except:
  51. c = 100000
  52. channel = json_stuff["items"][c]["name"]
  53. print(f"Uploading to {channel}.")
  54. try:
  55. print("---\nCould be costly to do a upload, press enter and bid will be 0.1")
  56. bid = str(input("Per upload, how much bid do you want? "))
  57. except:
  58. bid = str(0.1)
  59. def upload(command):
  60. video_data = subprocess.getoutput(command)
  61. video_data = video_data.splitlines()
  62. title = video_data[0]
  63. id = video_data[1]
  64. thumbnail_url = video_data[2]
  65. thumbnail_data = requests.get(thumbnail_url)
  66. with open(temp_dir, 'wb') as f:
  67. f.write(thumbnail_data.content)
  68. description = video_data[3:]
  69. description = '\n'.join(description)
  70. keys = ("abcdefghijklmnopqrxtuvwsyz" + "ABCDEFGHIJKLMNOPQRXTUV" + "1234567890")
  71. name_thumb = ("".join(random.sample(keys,50)))
  72. name = re.sub(r'[\W_]+','', str(title))
  73. description = (f"""---
  74. This is a LBRY mirror of of this video:
  75. {title}
  76. Original YT URL (THIS IS SPYWARE): https://youtube.com/watch?v={id}
  77. ---
  78. {description}""")
  79. print(description)
  80. description = description.replace("\n","\\n")
  81. description = description.replace('"','\\"')
  82. description = description.replace("'","\\'")
  83. print("\n---\nYT download starting:")
  84. os.system(f"{downloader} https://youtube.com/watch?v={id} --format=mp4")
  85. print("\n---\nUploading thumbnail to LBRY!")
  86. thumbnail_command = f'{lbrynet} publish --name={name_thumb} --bid={bid} --file_path="{temp_dir}" --title="{title}" --description="{description}"'
  87. thumbnail_data = subprocess.getoutput(thumbnail_command)
  88. json_stuff = json.loads(thumbnail_data)
  89. thumbnail_url = json_stuff["outputs"][0]["permanent_url"].replace("lbry:/","https://spee.ch")
  90. print(thumbnail_url)
  91. if platform.system() == "Windows":
  92. slash = "\\"
  93. else:
  94. slash = "/"
  95. cwd = os.getcwd()
  96. print("\n---\nUploading video to LBRY!\n---")
  97. command = f'{lbrynet} publish --name={name} --bid={bid} --file_path="{cwd}{slash}{title} [{id}].mp4" --title="{title}" --description="{description}" --channel_name={channel} --thumbnail="{thumbnail_url}"'
  98. os.system(command)
  99. print("\n---\nLINK:\n---")
  100. print(f"https://spee.ch/{channel}/{name}")
  101. split_url = url.split("/")
  102. if "channel" in url:
  103. channel_id = split_url[4]
  104. print("Getting data from the channel. This might take a while...")
  105. data = subprocess.getoutput(f"{downloader} --get-id https://youtube.com/channel/{channel_id}")
  106. data = data.splitlines()
  107. for id in data:
  108. print(id)
  109. upload(f"{downloader} --get-title --get-description --get-thumbnail --get-id https://youtube.com/watch?v={id}")
  110. # Lbrynet might break from a lot of uploading. So got to put a
  111. # 20 second delay.
  112. time.sleep(20)
  113. elif "watch" in url:
  114. id = split_url[3].replace("watch?v=","")
  115. upload(f"{downloader} --get-title --get-description --get-thumbnail --get-id https://youtube.com/watch?v={id}")
  116. else:
  117. print("This URL isn't supported yet :(")
  118. quit()