python-vids.org 11 KB

What is this script?

Imports

    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's
  • json for making json better
  • os for launching mpv

import requests import json import os import sy


Banner

This is not at all necasary but I think it makes the script more fun


print('''
/-------------------------------\\
|             vids              |
\\-------------------------------/
''')

Variables

  • command What the video link will get launched in most likely MPV or a browser
  • The 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
command = "mpv "
bold = "\033[01m"
norm = "\033[00m"
bright_cyan = "\033[46m"
colora = "\033[45m"
colorb = "\033[44m"

Invidious search

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 invidious_instance 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":
        invidious_instance = "https://invidio.xamh.de/"
        try:
            query = sys.argv[2]
        except:
            query = input("Searching for: ")
            query = str(query)

Print out search results

Using the invidious search API we fetch the search results and print them out numbered in a for loop.


        size = str(19)
        invidious_search = invidious_instance + "api/v1/search?q=" + query
        wol_api = "https://scrap.madiator.com/api/get-lbry-video?url="

        data = requests.get(invidious_search)
        json_stuff = json.loads(data.text)
        for i, vid in enumerate(json_stuff):
            print(i, colora+vid["title"]+norm+"\n"+colorb+vid["author"]+norm+"\n"+bright_cyan+vid["videoId"]+norm)

Pick a search result

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.


        c = 100000
        while not c >= 0 or not c <= 19:
            c = input('Number from 1-' + size + " of the URL you want to open: ")
            try:
                    c = int(c)
            except:
                    c = 100000

Comments

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.


  comments = invidious_instance + "/api/v1/comments/" + json_stuff[c]["videoId"]
  data_comment = requests.get(comments)
  json_comment = json.loads(data_comment.text)
  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)

WOL-api check

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
  lbry_check = requests.get(wol_api + json_stuff[c]["videoId"])
  lbry_check = json.loads(lbry_check.text)
  # For now using odysee because yt-dlp doesn't support librarian
  librarian_instance = "https://odysee.com/"

  if lbry_check["lbryurl"] == None:
      # Using youtube.com since yt-dlp on any given invidious url redirects to youtube.com anyways.
      selected_url = "https://youtube.com/watch?v=" + json_stuff[c]["videoId"]
  else:
      print("Playing with LBRY!")
      selected_url = librarian_instance + lbry_check["lbryurl"]
      selected_url = selected_url.replace("#", ":")

Launch in MPV!


        # Do stuff with it.
        os.system(command + selected_url)
        quit()

Peertube

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:
            query = sys.argv[2]
        except:
            query = input("Searching for: ")
            query = str(query)
        size = str(19)
        search = "https://sepiasearch.org/api/v1/search/videos?search=" + query
        data = requests.get(search)
        json_stuff = json.loads(data.text)
        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
        c = 100000
        while not c >= 0 or not c <= 19:
            c = input('Number from 1-' + size + " of the URL you want to open: ")
            try:
                    c = int(c)
            except:
                    c = 100000

        selected_url = json_stuff["data"][c]["url"]

PeerTube Comments

    This is a lot of work in progress code, it's very inefficient and only acompleshes:
  • Printing out comments
  • Detect if a video has replies
  • Tell how many replies
  • Print just 1 of those replies
  • Detect if one of the json groups is a deleted comment and if so pass and don't deal with it.

comments = "https://" + json_stuff["data"][c]["account"]["host"] + "/api/v1/videos/" + json_stuff["data"][c]["uuid"] + "/comment-threads/" data_comment = requests.get(comments) json_comment = json.loads(data_comment.text) # PRINT COMMENTS! for i, comment in enumerate(json_comment["data"]): # Sometimes peertube likes to give nonsese json if comment["account"] == None: pas


            # This detects if a comment has replies.
            elif comment["totalReplies"] > 0:
                replies = comments + str(comment["id"])
                data_replies = requests.get(replies)
                json_replies = json.loads(data_replies.text)
                total_replies = str(json_replies["comment"]["totalReplies"])
                # 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)

        os.system(command + selected_url)

LBRY

This section is for LBRY, and printing out the search results of the search.


    elif sys.argv[1] == "-l":
        try:
            query = sys.argv[2]
        except:
            query = input("Searching for: ")
            query = str(query)
        size = str(30)
        search = 'https://lighthouse.lbry.com/search?s=' + query + '&include=channel,channel_claim_id,title&size=' + size
        lbry = "https://lbry.ix.tc/"

        data = requests.get(search)
        json_stuff = json.loads(data.text)

        # Results
        for i, x in enumerate(json_stuff):
            pre = "lbry://"
            if x["channel"]:
                pre += x["channel"] + "/"
            url = pre + x["name"]
            print(i, bright_cyan+x["title"]+norm+"\n"+url)

LBRY comments

Using the librarian comment api after gathering a lot of information about the publication it is possible to get the comments.


        # Choose a result
        c = 100000
        while not c >= 0 or not c <= 29:
            c = input('Number from 1-' + size + " of the URL you want to open: ")
            try:
                    c = int(c)
            except:
                    c = 100000
        selected_url = json_stuff[c]
        # Do stuff with it.
        channel_name = selected_url["channel"]
        channel_ID = selected_url["channel_claim_id"]

        claim_ID = selected_url["claimId"]
        url = str(lbry + "api/comments?claim_id=" + claim_ID + "&channel_id=" + channel_ID + "&channel_name=" + channel_name + "&page=1&page_size=15")

        comments = requests.get(url)
        json_comments = json.loads(comments.text)
        for i, x in enumerate(json_comments["comments"]):
            print(i, bright_cyan+x["Channel"]["Name"]+norm+"\n"+x["Comment"])

        url = "https://odysee.com/" + selected_url["channel"] + "/" + selected_url["name"]
        os.system(command + url)
        quit()
        #+END_SRC

* HELP
Using the -h argument the user can get some help aobut how to use the program.
#+BEGIN_SRC python
    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('')
    #+END_SRC

#+BEGIN_EXPORT html 
<hr> 
<footer> 
<a rel='license' href='http://creativecommons.org/licenses/by-sa/4.0/'><img alt='Creative Commons License' style='border-width:0' width='88' height='31' src='../images/cc-by-sa.png' /></a><br> 
Unless otherwise noted, all content on this website is Copyright Zortazert 2021-2022 and is licensed under <a rel='license' href='http://creativecommons.org/licenses/by-sa/4.0/'>CC BY-SA 4.0</a>. 
</footer>