lbry.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #!/usr/bin/env python
  2. import requests
  3. import json
  4. import os
  5. import subprocess
  6. import sys
  7. import re
  8. import platform
  9. import time
  10. import tempfile
  11. lbrynet = "lbrynet"
  12. downloader = "yt-dlp"
  13. temp_dir = tempfile.TemporaryDirectory().name
  14. url = "https://invidio.xamh.de/channel/UCo8bcnLyZH8tBIH9V1mLgqQ"
  15. # COLORS
  16. bold="\033[01m"
  17. norm="\033[00m"
  18. bright_cyan="\033[45m"
  19. def mini_help():
  20. print('''An incorrect or no argument was passed.
  21. +---------------+---------------+-----------------+
  22. |Short options: | Long options: | Meaning: |
  23. +---------------+---------------+-----------------+
  24. | -s | --search | Search LBRY |
  25. | -y | --yt | YT to LBRY |
  26. | -mu | --massupload | Upload directory|
  27. | -u | --upload | Upload file |
  28. | -c | --convert | LBC to USD |
  29. | -cl | --claim-list | List claims |
  30. | -h | --help | Full Help |
  31. +---------------+---------------+-----------------+''')
  32. def check():
  33. if subprocess.getoutput(f"{lbrynet} version") == "Could not connect to daemon. Are you sure it's running?":
  34. print('It looks like lbrynet has not started yet. In another terminal window/tab do "lbrynet start" and rerun this script.')
  35. quit()
  36. if len(sys.argv) == 1:
  37. mini_help()
  38. elif sys.argv[1] == "--search" or sys.argv[1] == "-s":
  39. query = ""
  40. try:
  41. query = sys.argv[2:]
  42. query = ' '.join(query)
  43. print(query)
  44. except:
  45. while not query:
  46. query = input("Searching for: ")
  47. size = str(30)
  48. search = 'https://lighthouse.lbry.com/search?s=' + query + '&include=channel,channel_claim_id,title&size=' + size
  49. lbry = "https://lbry.ix.tc/"
  50. command = "C:\\SGZ_Pro\\z-apps_drivers\\mpv\\mpv.exe "
  51. data = requests.get(search)
  52. json_stuff = json.loads(data.text)
  53. for i, x in enumerate(json_stuff):
  54. pre = "lbry://"
  55. if x["channel"]:
  56. pre += x["channel"] + "/"
  57. url = pre + x["name"]
  58. print(i, bright_cyan+x["title"]+norm+"\n"+url)
  59. c = 100000
  60. while not c >= 0 or not c <= 29:
  61. c = input('Number from 1-' + size + " of the URL you want to open: ")
  62. try:
  63. c = int(c)
  64. except:
  65. c = 100000
  66. selected_url = json_stuff[c]
  67. channel_name = selected_url["channel"]
  68. channel_ID = selected_url["channel_claim_id"]
  69. claim_ID = selected_url["claimId"]
  70. url = str(lbry + "api/comments?claim_id=" + claim_ID + "&channel_id=" + channel_ID + "&channel_name=" + channel_name + "&page=1&page_size=15")
  71. comments = requests.get(url)
  72. json_comments = json.loads(comments.text)
  73. for i, x in enumerate(json_comments["comments"]):
  74. print(i, bright_cyan+x["Channel"]["Name"]+norm+"\n"+x["Comment"])
  75. url = "https://odysee.com/" + selected_url["channel"] + "/" + selected_url["name"]
  76. os.system(command + url)
  77. elif sys.argv[1] == "--convert" or sys.argv[1] == "-c":
  78. lbc = input("How much LBC? ")
  79. print(requests.get("https://rate.sx/" + lbc + "LBC").text.rstrip())
  80. elif sys.argv[1] == "--upload" or sys.argv[1] == "-u":
  81. # Got from: https://notabug.org/jyamihud/FastLBRY-terminal/src/master/flbry/variables.py#L502
  82. licenses = [
  83. # NAME , URL , COMMENT
  84. ["GNU General Public License Version 3 (or later)",
  85. "https://www.gnu.org/licenses/gpl-3.0.html",
  86. "Strong Copyleft. Recommended for Software."],
  87. ["GNU General Public License Version 3 (only)",
  88. "https://www.gnu.org/licenses/gpl-3.0.html",
  89. "Strong Copyleft."],
  90. ["GNU Free Documentation License",
  91. "https://www.gnu.org/licenses/fdl-1.3.html",
  92. "Strong Copyleft. Recommended for books."],
  93. ["Creative Commons Attribution-ShareAlike 4.0 International",
  94. "https://creativecommons.org/licenses/by-sa/4.0/",
  95. "Copylefted, Recommended for Art."],
  96. ["Creative Commons Attribution 4.0 International",
  97. "https://creativecommons.org/licenses/by/4.0/",
  98. "Non Copylefted, Free License."],
  99. ["Creative Commons Zero 1.0 International",
  100. "https://creativecommons.org/publicdomain/zero/1.0/",
  101. "Public Domain"],
  102. ["Creative Commons Attribution-NoDerivatives 4.0 International",
  103. "https://creativecommons.org/licenses/by-nd/4.0/",
  104. "Does not allow changes. Recommended for opinion pieces."]
  105. ]
  106. check()
  107. # Channel
  108. channels = subprocess.getoutput(f"{lbrynet} channel list")
  109. json_stuff = json.loads(channels)
  110. for i, channel in enumerate(json_stuff["items"]):
  111. print(i, "|", channel["name"])
  112. c = 100000
  113. while not c >= 0 or not c <= i:
  114. c = input('Select a channel from 0-'+str(i)+': ')
  115. try:
  116. c = int(c)
  117. except:
  118. c = 100000
  119. channel = json_stuff["items"][c]["name"]
  120. print(f"Uploading to {channel}.\n---")
  121. # Title
  122. title = input("Title for the publication: ")
  123. # Url
  124. print("---\nPressing enter will make it the title just with removed special characters.")
  125. url = input("Custome lbry url name: ")
  126. if url == "":
  127. name = re.sub(r'[\W_]+','', str(title))
  128. # Bid
  129. try:
  130. print("---\nCould be costly to do a upload, press enter and bid will be 0.1")
  131. bid = str(input("Per upload, how much bid do you want? "))
  132. except:
  133. bid = str(0.1)
  134. # Description
  135. try:
  136. print("---\nPressing enter will make the publication not have a description")
  137. description = input("Description for publication: ")
  138. #description = description.replace("\n","\\n")
  139. description = description.replace('"','\\"')
  140. description = description.replace("'","\\'")
  141. except:
  142. description = ""
  143. # Thumbnail
  144. try:
  145. print("---\nIf you want, a thumbnail can be uploaded to lbry. It will have all atributes of the video just the lbry name will be plus 123. Press enter for no thumbnail.")
  146. thumbnail = input("Thumbnail file location: ")
  147. except:
  148. thumbnail = ""
  149. name_thumb = re.sub(r'[\W_]+','', str(title)) + str(123)
  150. # Publication
  151. print("---\nFinally we are at the last step!")
  152. publication = input("Publication file location: ")
  153. # License
  154. for i, license in enumerate(licenses):
  155. print(i,*licenses[i],sep='\n')
  156. l = 100000
  157. while not l >= 0 or not l <= 7:
  158. l = input('Select a license from 0-7: ')
  159. try:
  160. l = int(l)
  161. except:
  162. l = 100000
  163. license = licenses[l][0]
  164. license_url = licenses[l][1]
  165. if thumbnail == "":
  166. print("---\nUploading puplication to LBRY!\n---")
  167. command = f'{lbrynet} publish --name={name} --bid={bid} --file_path="{publication}" --title="{title}" --description="{description}" --channel_name={channel} --license="{license}" --license_url="{license_url}"'
  168. os.system(command)
  169. else:
  170. print("\n---\nUploading thumbnail to LBRY!")
  171. thumbnail_command = f'{lbrynet} publish --name={name_thumb} --bid={bid} --file_path="{thumbnail}" --title="{title}" --description="{description}"'
  172. thumbnail_data = subprocess.getoutput(thumbnail_command)
  173. json_stuff = json.loads(thumbnail_data)
  174. thumbnail_url = json_stuff["outputs"][0]["permanent_url"].replace("lbry:/","https://spee.ch")
  175. print("\n---\nUploading puplication to LBRY!\n---")
  176. command = f'{lbrynet} publish --name={name} --bid={bid} --file_path="{publication}" --title="{title}" --description="{description}" --channel_name={channel} --thumbnail="{thumbnail_url}" --license="{license}" --license_url="{license_url}"'
  177. os.system(command)
  178. print("\n---\nLINK:\n---")
  179. print(f"https://spee.ch/{channel}/{name}")
  180. elif sys.argv[1] == "--massupload" or sys.argv[1] == "-mu":
  181. files = os.listdir()
  182. if platform.system() == "Windows":
  183. slash = "\\"
  184. else:
  185. slash = "/"
  186. file_path = os.getcwd() + slash
  187. check()
  188. channels = subprocess.getoutput(f"{lbrynet} channel list")
  189. json_stuff = json.loads(channels)
  190. for i, channel in enumerate(json_stuff["items"]):
  191. print(i, "|", channel["name"])
  192. c = 100000
  193. while not c >= 0 or not c <= i:
  194. c = input('Select a channel from 0-'+str(i)+': ')
  195. try:
  196. c = int(c)
  197. except:
  198. c = 100000
  199. channel = json_stuff["items"][c]["name"]
  200. print(f"Mass uploading to {channel}.")
  201. try:
  202. print("---\nCould be costly to do a mass upload, press enter and bid will be 0.1")
  203. bid = str(input("Per upload, how much bid do you want? "))
  204. except:
  205. bid = str(0.1)
  206. #try:
  207. # print('---\nDo you want a special description? Press enter and the description will be "mass upload"')
  208. # description = input('Description for every upload: ')
  209. #except:
  210. # description = "mass upload"
  211. for image in files:
  212. if sys.argv[0] in image:
  213. print("Not going to upload {sys.argv[0]}")
  214. pass
  215. else:
  216. os.system(f'{lbrynet} publish --name={image} --bid=0.1 --file_path="{file_path + image}" --title="{image}" --channel_name={channel}')
  217. time.sleep(30)
  218. for image in files:
  219. print(f"https://spee.ch/{channel}/{image}")
  220. elif sys.argv[1] == "--notifications" or sys.argv[1] == "-n":
  221. with open('auth_token.json', 'r') as f:
  222. json_stuff = json.load(f)
  223. auth_token = json_stuff["auth_token"]
  224. notifications = requests.post("https://api.odysee.com/notification/list", data={"auth_token":auth_token}).json()
  225. #print(json.dumps(notifications, indent=4))
  226. for i in notifications["data"]:
  227. if i["is_read"] == "true":
  228. print("--")
  229. print(i["notification_parameters"]["device"]["title"])
  230. print(i["notification_parameters"]["device"]["target"])
  231. print(i["notification_parameters"]["device"]["text"])
  232. else:
  233. print("No new notifications!")
  234. break
  235. elif sys.argv[1] == "--yt" or sys.argv[1] == "--y":
  236. check()
  237. url = ""
  238. try:
  239. url = sys.argv[2]
  240. print(url)
  241. except:
  242. while not url:
  243. url = input("Searching for: ")
  244. split_url = url.split("/")
  245. if "channel" in url:
  246. channel_id = split_url[4]
  247. command = f"{downloader} --get-title --get-description --get-thumbnail --get-id https://youtube.com/channel/{channel_id} --playlist-end 1"
  248. elif "watch" in url:
  249. id = split_url[3].replace("watch?v=","")
  250. command = f"{downloader} --get-title --get-description --get-thumbnail --get-id https://youtube.com/watch?v={id}"
  251. else:
  252. print("This URL isn't supported yet :(")
  253. quit()
  254. video_data = subprocess.getoutput(command)
  255. video_data = video_data.splitlines()
  256. title = video_data[0]
  257. id = video_data[1]
  258. thumbnail_url = video_data[2].replace("hqdefault","maxresdefault")
  259. thumbnail_data = requests.get(thumbnail_url)
  260. with open(temp_dir, 'wb') as f:
  261. f.write(thumbnail_data.content)
  262. description = video_data[3:]
  263. description = '\n'.join(description)
  264. name_thumb = re.sub(r'[\W_]+','', str(title)) + str(123)
  265. name = re.sub(r'[\W_]+','', str(title))
  266. channels = subprocess.getoutput(f"{lbrynet} channel list")
  267. json_stuff = json.loads(channels)
  268. for i, channel in enumerate(json_stuff["items"]):
  269. print(i, "|", channel["name"])
  270. c = 100000
  271. while not c >= 0 or not c <= i:
  272. c = input('Select a channel from 0-'+str(i)+': ')
  273. try:
  274. c = int(c)
  275. except:
  276. c = 100000
  277. channel = json_stuff["items"][c]["name"]
  278. print(f"Uploading to {channel}.")
  279. try:
  280. print("---\nCould be costly to do a upload, press enter and bid will be 0.1")
  281. bid = str(input("Per upload, how much bid do you want? "))
  282. except:
  283. bid = str(0.1)
  284. description = (f"""---
  285. This is a LBRY mirror of of this video:
  286. {title}
  287. Original YT URL (THIS IS SPYWARE): https://youtube.com/watch?v={id}
  288. ---
  289. {description}""")
  290. print(description)
  291. description = description.replace("\n","\\n")
  292. description = description.replace('"','\\"')
  293. description = description.replace("'","\\'")
  294. print("\n---\nYT download starting:")
  295. os.system(f"{downloader} https://youtube.com/watch?v={id}")
  296. print("\n---\nUploading thumbnail to LBRY!")
  297. thumbnail_command = f'{lbrynet} publish --name={name_thumb} --bid={bid} --file_path="{temp_dir}" --title="{title}" --description="{description}"'
  298. #os.system(thumbnail_command)
  299. thumbnail_data = subprocess.getoutput(thumbnail_command)
  300. json_stuff = json.loads(thumbnail_data)
  301. thumbnail_url = json_stuff["outputs"][0]["permanent_url"].replace("lbry:/","https://spee.ch")
  302. print(thumbnail_url)
  303. if platform.system() == "Windows":
  304. slash = "\\"
  305. else:
  306. slash = "/"
  307. cwd = os.getcwd()
  308. print("\n---\nUploading video to LBRY!\n---")
  309. 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}"'
  310. os.system(command)
  311. print("\n---\nLINK:\n---")
  312. print(f"https://spee.ch/{channel}/{name}")
  313. elif sys.argv[1] == "-cl" or sys.argv[1] == "--claim-list":
  314. check()
  315. data = subprocess.getoutput(f"{lbrynet} claim list")
  316. json_stuff = json.loads(data)
  317. for i, pub in enumerate(json_stuff["items"]):
  318. try:
  319. print(i, 'lbry://'+pub["name"]+" ("+pub["value"]["stream_type"]+")"+"\n"+pub["value"]["title"])
  320. except:
  321. print(i, 'lbry://'+pub["name"]+" ("+pub["value"]["stream_type"]+")")
  322. elif sys.argv[1] == "-h" or sys.argv[1] == "--help":
  323. print('''Command:
  324. python lbry.py <arg>
  325. Search the LBRY network:
  326. -s or --search for lighthouse (searching), librarian api (comments)
  327. Convert LBC to USD
  328. -c or --convert for rate.sx
  329. Mass upload:
  330. -mu or --massupload using the LBRY sdk to mass upload all files in directory to LBRY.
  331. Upload:
  332. -u or --upload using the LBRY sdk to upload a file to LBRY.
  333. Upload youtube to LBRY:
  334. -y or --yt using the LBRY sdk and yt-dlp to upload new video from a youtube channel to LBRY.''')
  335. else:
  336. mini_help()
  337. quit()