Script for searching YouTube through the invidious API, LBRY through lighthouse API and peertube through the SepiaSearch API. Played with mpv
requests
for fetching data from API'sjson
for making json betteros
for launching mpvimport requests
import json
import os
import sys
This is not at all necasary but I think it makes the script more fun
print('''
/-------------------------------\\
| vids |
\\-------------------------------/
''')
command
What the video link will get launched in most likely MPV or a browserThe colors aren't organized well there just different python color codes. All terminals are configured differently making colors show not always perfect. Better than no color I think.
# Use either a browser or mpv
= "mpv "
command = "\033[01m"
bold = "\033[00m"
norm = "\033[46m"
bright_cyan = "\033[45m"
colora = "\033[44m" colorb
The first thing that happens is this script is put in a try
function, if a argument is given to the script e.g. -i
it will start doing things with it. If no, it will print out the banner.
The if statement detects if by sys.argv[1]
value if it's -i
. The invidiousinstance is set and can be changed to a invidious instance that I like.
The try
and except
statement checks to see if possibly the user did a command like:
python vids.py -i "search query"
if they didn't give that 2nd argument of the search it will just prompt them.
try:
if sys.argv[1] == "-i":
= "https://invidio.xamh.de/"
invidious_instance try:
= sys.argv[2]
query except:
= input("Searching for: ")
query = str(query) query
Using the invidious search API we fetch the search results and print them out numbered in a for loop.
= str(19)
size = invidious_instance + "api/v1/search?q=" + query
invidious_search = "https://scrap.madiator.com/api/get-lbry-video?url="
wol_api
= requests.get(invidious_search)
data = json.loads(data.text)
json_stuff for i, vid in enumerate(json_stuff):
print(i, colora+vid["title"]+norm+"\n"+colorb+vid["author"]+norm+"\n"+bright_cyan+vid["videoId"]+norm)
This while loop asks the user for a video they want to play and see the comments of. Storing the number they give in a variable so that later it can be used when parsing the json for specific stuff. The reason for a while loop with the special argument is to keep prompting the user over and over to pick the result. It has to be either greater than or equal to 0 or less than or equal to 19.
= 100000
c while not c >= 0 or not c <= 19:
= input('Number from 1-' + size + " of the URL you want to open: ")
c try:
= int(c)
c except:
= 100000 c
Using the invidious comment api we fetch the comments of a video with the videoId, there maybe a error in the resulting json, most likely because there are no comments. Because of this the if statement prints could not fetch comments
.
= invidious_instance + "/api/v1/comments/" + json_stuff[c]["videoId"]
comments = requests.get(comments)
data_comment = json.loads(data_comment.text)
json_comment if "error" in json_comment:
print("could not fetch comments")
else:
for i, comment in enumerate(json_comment["comments"]):
print(i, colora+comment["author"]+norm+"\n"+colorb+comment["content"]+norm)
Using the WOL-API we can check if the video is available on LBRY and play with mpv from a odysee.com link over using youtube/invidious. If the lbry check gave null
or what python calls it None
, the link will be a youtube link, since it's what yt-dlp uses anyways. If the lbry check gave a value that's a lbry url it will play with mpv a odysee link.
# wol-api check
= requests.get(wol_api + json_stuff[c]["videoId"])
lbry_check = json.loads(lbry_check.text)
lbry_check # For now using odysee because yt-dlp doesn't support librarian
= "https://odysee.com/"
librarian_instance
if lbry_check["lbryurl"] == None:
# Using youtube.com since yt-dlp on any given invidious url redirects to youtube.com anyways.
= "https://youtube.com/watch?v=" + json_stuff[c]["videoId"]
selected_url else:
print("Playing with LBRY!")
= librarian_instance + lbry_check["lbryurl"]
selected_url = selected_url.replace("#", ":") selected_url
# Do stuff with it.
+ selected_url)
os.system(command quit()
A lot of what I discussed in the invidious part of the script applies to the PeerTube part, there are just different values and API's that are worked with.
elif sys.argv[1] == "-p":
try:
= sys.argv[2]
query except:
= input("Searching for: ")
query = str(query)
query = str(19)
size = "https://sepiasearch.org/api/v1/search/videos?search=" + query
search = requests.get(search)
data = json.loads(data.text)
json_stuff for i, vid in enumerate(json_stuff["data"]):
print(i, colora+vid["name"]+norm+"\n"+colorb+vid["channel"]["displayName"]+norm+"\n"+bright_cyan+vid["url"]+norm)
# Choose a result
= 100000
c while not c >= 0 or not c <= 19:
= input('Number from 1-' + size + " of the URL you want to open: ")
c try:
= int(c)
c except:
= 100000
c
= json_stuff["data"][c]["url"] selected_url
This is a lot of work in progress code, it's very inefficient and only acompleshes:
pass
and don't deal with it.= "https://" + json_stuff["data"][c]["account"]["host"] + "/api/v1/videos/" + json_stuff["data"][c]["uuid"] + "/comment-threads/"
comments = requests.get(comments)
data_comment = json.loads(data_comment.text)
json_comment # PRINT COMMENTS!
for i, comment in enumerate(json_comment["data"]):
# Sometimes peertube likes to give nonsese json
if comment["account"] == None:
pass
# This detects if a comment has replies.
elif comment["totalReplies"] > 0:
= comments + str(comment["id"])
replies = requests.get(replies)
data_replies = json.loads(data_replies.text)
json_replies = str(json_replies["comment"]["totalReplies"])
total_replies # Print out that this comment has replies and also say how many
print(i, colora+comment["account"]["displayName"]+norm+"\n"+colorb+comment["text"]+norm+bright_cyan+"\nREPLIES: "+total_replies+norm)
# Here in this for loop inside of a for loop for replys
for i, reply in enumerate(json_replies["children"]):
# Same thing can happen where it gives nonsense json.
if reply["comment"]["account"] == None:
pass
# This prints out the first reply.
else:
print(" " + str(i) + " " + colora+reply["comment"]["account"]["displayName"]+norm+"\n "+colorb+reply["comment"]["text"]+norm)
# This is the final thing, the comment has no replys so it just
# prints it as a comment.
else:
print(i, colora+comment["account"]["displayName"]+norm+"\n"+colorb+comment["text"]+norm)
+ selected_url) os.system(command
This section is for LBRY, and printing out the search results of the search.
elif sys.argv[1] == "-l":
try:
= sys.argv[2]
query except:
= input("Searching for: ")
query = str(query)
query = str(30)
size = 'https://lighthouse.lbry.com/search?s=' + query + '&include=channel,channel_claim_id,title&size=' + size
search = "https://lbry.ix.tc/"
lbry
= requests.get(search)
data = json.loads(data.text)
json_stuff
# Results
for i, x in enumerate(json_stuff):
= "lbry://"
pre if x["channel"]:
+= x["channel"] + "/"
pre = pre + x["name"]
url print(i, bright_cyan+x["title"]+norm+"\n"+url)
Using the librarian comment api after gathering a lot of information about the publication it is possible to get the comments.
# Choose a result
= 100000
c while not c >= 0 or not c <= 29:
= input('Number from 1-' + size + " of the URL you want to open: ")
c try:
= int(c)
c except:
= 100000
c = json_stuff[c]
selected_url # Do stuff with it.
= selected_url["channel"]
channel_name = selected_url["channel_claim_id"]
channel_ID
= selected_url["claimId"]
claim_ID = str(lbry + "api/comments?claim_id=" + claim_ID + "&channel_id=" + channel_ID + "&channel_name=" + channel_name + "&page=1&page_size=15")
url
= requests.get(url)
comments = json.loads(comments.text)
json_comments for i, x in enumerate(json_comments["comments"]):
print(i, bright_cyan+x["Channel"]["Name"]+norm+"\n"+x["Comment"])
= "https://odysee.com/" + selected_url["channel"] + "/" + selected_url["name"]
url + url)
os.system(command quit()
Using the -h argument the user can get some help aobut how to use the program.
elif sys.argv[1] == "-h":
print('''
Command:
python vids.py <arg>
After doing this you will be prompted to make a search
If you want you can make the search in the command by doing
python vids.py <arg> "search"
-l for lighthouse (LBRY network)
-p for sepia (Peertube)
-i for invidious) (YouTube)
NOTE: All youtube links will be checked with the Watch on LBRY API. If
the video is available on the lbry network, the youtube search result
will be opened in a odysee.com link.
''')
except:
print('')