[SCRIPT] Randomizer Hypnotube GER/ENG v1.1

This is the place for all suggestions, releases and feedback regarding Milovana Hardware efforts.
Post Reply
VirusHD
Explorer
Explorer
Posts: 12
Joined: Fri Jan 08, 2021 9:02 am

[SCRIPT] Randomizer Hypnotube GER/ENG v1.1

Post by VirusHD »

Deutsche version
Spoiler: show
Moin Zusammen,

gerne möchte ich mit euch ein Script Teilen welches ihr fix selber aufbauen könnt.
Umgang mit Python vorausgesetzt.

Dieses Script ist ein Webcrawler mit der Funktion Videos direkt von der Webseite abzuspielen.
Die Aufgerufen Webseite ist https://hypnotube.com/ Sissy Content


Vorwort:
Spoiler: show
Was macht dieses Script:
1) Suche & Abspielen: Fordere nach einem Suchbegriff und spiele dann ein zufälliges Video ab. Die Suchliste wird dann gelöscht.
2) Suche & Speichern: Fragt nach einem Suchbegriff und speichert die Liste unter dem Namen der Suche (kein Video wird abgespielt).
3) Auswählen & Abspielen: Fordere eine Linkliste an (z.B. von Option 2) und spiele ein zufälliges Video daraus ab.
4) Marathon: Fragt nach einem Suchbegriff und spielt ein zufälliges Video ab, das dann von der Liste gelöscht wird. Du wirst dann gefragt, ob du fortfahren möchtest. Das Ganze kann wiederholt werden, bis die Liste leer ist.

5) Beenden: Sollte klar sein, oder?
Script:
Spoiler: show
Denkt an die Abhängigkeiten (os, requests, bs4, random, webbrowser, urllib.parse) diese können mit pip nachinstalliert werden.

Code: Select all

import os
import requests
from bs4 import BeautifulSoup
import random
import webbrowser
import urllib.parse

def get_video_links(url, output_file):
    video_links = []
    try:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        for source_tag in soup.find_all('source', src=True):
            src = source_tag.get('src')
            if src.endswith('.mp4'):
                video_links.append(src)
    except Exception as e:
        print(f"Error fetching video links from {url}: {e}")
    return video_links

def get_search_results(search_term):
    search_term_encoded = urllib.parse.quote_plus(search_term)
    main_url = f"https://hypnotube.com/search/{search_term_encoded}/page{{}}.html"
    all_links = []
    try:
        for page_num in range(1, 21):
            page_url = main_url.format(page_num)
            response = requests.get(page_url)
            soup = BeautifulSoup(response.text, 'html.parser')
            for link in soup.find_all('a', href=True):
                href = link.get('href')
                if href.startswith('https://hypnotube.com/video/'):
                    all_links.append(href)
    except Exception as e:
        print(f"Error fetching search results for {search_term}: {e}")
    return all_links

def play_random_video(links):
    if links:
        random_link = random.choice(links)
        print(f"Playing random video: {random_link}")
        try:
            response = requests.get(random_link)
            soup = BeautifulSoup(response.text, 'html.parser')
            for source_tag in soup.find_all('source', src=True):
                src = source_tag.get('src')
                if src.endswith('.mp4'):
                    print(f"Playing video: {src}")
                    webbrowser.open(src)
                    return
            print("No video found on the selected page.")
        except Exception as e:
            print(f"Error playing video from {random_link}: {e}")
    else:
        print("No video links found.")

def save_search_results(search_term, links):
    filename = f"{search_term}.txt"
    with open(filename, "w") as f:
        for link in links:
            f.write(link + '\n')
    print(f"Search results have been saved to '{filename}'.")

def select_and_play_video():
    filename = input("Enter the name of the file containing video links: ")
    if not os.path.exists(filename):
        print("File not found.")
        return

    with open(filename, "r") as f:
        links = f.read().splitlines()

    play_random_video(links)

def marathon_mode():
    search_term = input("Enter search term: ")
    search_results = get_search_results(search_term)

    if search_results:
        filename = "marathon.txt"
        with open(filename, "w") as f:
            for link in search_results:
                f.write(link + '\n')
        print(f"Marathon list has been saved to '{filename}'.")

        while True:
            if not os.path.exists(filename):
                print("Marathon list file not found.")
                return
            
            with open(filename, "r") as f:
                links = f.read().splitlines()

            if links:
                random_link = random.choice(links)
                print(f"Playing random video: {random_link}")
                try:
                    response = requests.get(random_link)
                    soup = BeautifulSoup(response.text, 'html.parser')
                    for source_tag in soup.find_all('source', src=True):
                        src = source_tag.get('src')
                        if src.endswith('.mp4'):
                            print(f"Playing video: {src}")
                            webbrowser.open(src)
                            links.remove(random_link)
                            with open(filename, "w") as f:
                                for link in links:
                                    f.write(link + '\n')
                            resume = input("Resume marathon? (y/n): ")
                            if resume.lower() != "y":
                                return
                            break
                    else:
                        print("No video found on the selected page.")
                except Exception as e:
                    print(f"Error playing video from {random_link}: {e}")
            else:
                print("No more videos left in the marathon list.")
                break
    else:
        print("No video links found for the search term.")

def main():
    while True:
        print("Select an option:")
        print("1) Search & Play")
        print("2) Search & Save")
        print("3) Select & Play")
        print("4) Marathon")
        print("5) Exit")

        option = input("Enter your choice: ")

        if option == "1":
            search_term = input("Enter search term: ")
            search_results = get_search_results(search_term)
            play_random_video(search_results)
            if os.path.exists("video_links.txt"):
                os.remove("video_links.txt")
        elif option == "2":
            search_term = input("Enter search term: ")
            search_results = get_search_results(search_term)
            save_search_results(search_term, search_results)
        elif option == "3":
            select_and_play_video()
        elif option == "4":
            marathon_mode()
        elif option == "5":
            print("Exiting the script.")
            break
        else:
            print("Invalid option.")

if __name__ == "__main__":
    main()

Script update: Suche konnte nur ein Wort erfassen. (Behoben)

Tweaks:
Spoiler: show
Das Script ist so aufgebaut das es nach dem Filter "Most Relevant" schaut, wollt ihr das Ändern muss folgedende Zeile angepasst werden:

Most Relevant:
"

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/page{{}}.html"
Top Rating:

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/rating/page{{}}.html"
Newest:

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/newest/page{{}}.html"
Most Viewed:

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/views/page{{}}.html"
Das Script darf gerne weiter verwendet werden. Bei fragen könnt ihr gerne schreiben, doch habt nachsicht das ich nicht 24/7 Online bin.
Viel Spaß :-D
Englisch version
Spoiler: show
Hi everyone,

I'd like to share a script with you that you can easily set up yourself.
Python skills required.

This script is a web crawler that allows you to play videos directly from the website.
The website being accessed is https://hypnotube.com/ Sissy Content

Preface:
Spoiler: show
What does this script do:

1) Search & Play Ask for a search term and then play a random video. The crawllist will then be deleted.
2) Search & Save Asks for a search term and saves the list with the name of the search (no video is played)
3) Select & Play Ask for a link list (e.g. from option 2) and play a random video from it.
4) Marathon Asks for a search term and plays a random video, which is then deleted from the list. You will then be asked if you want to continue. The whole thing can be repeated until the list is empty.
5) Exit should be clear right?
Script:
Spoiler: show
Keep in mind the dependencies (os, requests, bs4, random, webbrowser, urllib.parse) which can be installed using pip.

Code: Select all

import os
import requests
from bs4 import BeautifulSoup
import random
import webbrowser
import urllib.parse

def get_video_links(url, output_file):
    video_links = []
    try:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        for source_tag in soup.find_all('source', src=True):
            src = source_tag.get('src')
            if src.endswith('.mp4'):
                video_links.append(src)
    except Exception as e:
        print(f"Error fetching video links from {url}: {e}")
    return video_links

def get_search_results(search_term):
    search_term_encoded = urllib.parse.quote_plus(search_term)
    main_url = f"https://hypnotube.com/search/{search_term_encoded}/page{{}}.html"
    all_links = []
    try:
        for page_num in range(1, 21):
            page_url = main_url.format(page_num)
            response = requests.get(page_url)
            soup = BeautifulSoup(response.text, 'html.parser')
            for link in soup.find_all('a', href=True):
                href = link.get('href')
                if href.startswith('https://hypnotube.com/video/'):
                    all_links.append(href)
    except Exception as e:
        print(f"Error fetching search results for {search_term}: {e}")
    return all_links

def play_random_video(links):
    if links:
        random_link = random.choice(links)
        print(f"Playing random video: {random_link}")
        try:
            response = requests.get(random_link)
            soup = BeautifulSoup(response.text, 'html.parser')
            for source_tag in soup.find_all('source', src=True):
                src = source_tag.get('src')
                if src.endswith('.mp4'):
                    print(f"Playing video: {src}")
                    webbrowser.open(src)
                    return
            print("No video found on the selected page.")
        except Exception as e:
            print(f"Error playing video from {random_link}: {e}")
    else:
        print("No video links found.")

def save_search_results(search_term, links):
    filename = f"{search_term}.txt"
    with open(filename, "w") as f:
        for link in links:
            f.write(link + '\n')
    print(f"Search results have been saved to '{filename}'.")

def select_and_play_video():
    filename = input("Enter the name of the file containing video links: ")
    if not os.path.exists(filename):
        print("File not found.")
        return

    with open(filename, "r") as f:
        links = f.read().splitlines()

    play_random_video(links)

def marathon_mode():
    search_term = input("Enter search term: ")
    search_results = get_search_results(search_term)

    if search_results:
        filename = "marathon.txt"
        with open(filename, "w") as f:
            for link in search_results:
                f.write(link + '\n')
        print(f"Marathon list has been saved to '{filename}'.")

        while True:
            if not os.path.exists(filename):
                print("Marathon list file not found.")
                return
            
            with open(filename, "r") as f:
                links = f.read().splitlines()

            if links:
                random_link = random.choice(links)
                print(f"Playing random video: {random_link}")
                try:
                    response = requests.get(random_link)
                    soup = BeautifulSoup(response.text, 'html.parser')
                    for source_tag in soup.find_all('source', src=True):
                        src = source_tag.get('src')
                        if src.endswith('.mp4'):
                            print(f"Playing video: {src}")
                            webbrowser.open(src)
                            links.remove(random_link)
                            with open(filename, "w") as f:
                                for link in links:
                                    f.write(link + '\n')
                            resume = input("Resume marathon? (y/n): ")
                            if resume.lower() != "y":
                                return
                            break
                    else:
                        print("No video found on the selected page.")
                except Exception as e:
                    print(f"Error playing video from {random_link}: {e}")
            else:
                print("No more videos left in the marathon list.")
                break
    else:
        print("No video links found for the search term.")

def main():
    while True:
        print("Select an option:")
        print("1) Search & Play")
        print("2) Search & Save")
        print("3) Select & Play")
        print("4) Marathon")
        print("5) Exit")

        option = input("Enter your choice: ")

        if option == "1":
            search_term = input("Enter search term: ")
            search_results = get_search_results(search_term)
            play_random_video(search_results)
            if os.path.exists("video_links.txt"):
                os.remove("video_links.txt")
        elif option == "2":
            search_term = input("Enter search term: ")
            search_results = get_search_results(search_term)
            save_search_results(search_term, search_results)
        elif option == "3":
            select_and_play_video()
        elif option == "4":
            marathon_mode()
        elif option == "5":
            print("Exiting the script.")
            break
        else:
            print("Invalid option.")

if __name__ == "__main__":
    main()

Script update: Could only capture one word in the search term. (Fixed)
Tweaks:
Spoiler: show
The script is set up to look for "Most Relevant" filter, if you want to change it, the following line needs to be adjusted:

Most Relevant:

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/page{{}}.html"
Top Rating:

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/rating/page{{}}.html"
Newest:

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/newest/page{{}}.html"
Most Viewed:

Code: Select all

main_url = f"https://hypnotube.com/search/{search_term_encoded}/views/page{{}}.html"
Feel free to continue using the script. If you have any questions, feel free to ask, but please understand that I'm not online 24/7.
Enjoy! :-D
Changelog:
Spoiler: show
Update Version 1.0.1
Spoiler: show
Script update: Could only capture one word in the search term. (Fixed)
Update Version 1.1
Spoiler: show

menu
There is now a menu in which you return after each action until you say exit or cancel the script.
The following options are now available

Select an option:
1) Search & Play Ask for a search term and then play a random video. The crawllist will then be deleted.
2) Search & Save Asks for a search term and saves the list with the name of the search (no video is played)
3) Select & Play Ask for a link list (e.g. from option 2) and play a random video from it.
4) Marathon Asks for a search term and plays a random video, which is then deleted from the list. You will then be asked if you want to continue. The whole thing can be repeated until the list is empty.
5) Exit should be clear right?
Last edited by VirusHD on Thu Apr 18, 2024 12:14 pm, edited 7 times in total.
User avatar
Augustulus
Explorer At Heart
Explorer At Heart
Posts: 514
Joined: Fri Dec 25, 2015 4:27 pm
Gender: Male
Sexual Orientation: Open to new ideas!
I am a: Switch

Re: [SCRIPT] Randomizer Hypnotube GER

Post by Augustulus »

Hey :wave:,

--- german ---

das sieht ja nice aus. Habe gerade mal einen Test gemacht.
Läuft gut.

Mir kam allerdings gleich Ideen:
  1. Anstelle, dass man die ganze Datei am Ende löscht oder behält, könnte man vielleicht auch nur den Eintrag des aktuell ausgewählten Videos löschen. So könnte man die Kategorie behalten, aber Videos, die einem nicht gefallen einfach rausschmeißen.
  2. Wenn eine Datei mit Links vorhanden ist, sollte man die beim nächsten Start auch auswählen können oder eben man gibt einen neuen Suchbegriff ein.
Zuletzt: da das ganze hier ja ein internationales Forum ist, wäre es vielleicht sinnvoll, deinen Eingangspost auch auf Englisch bereit zu stellen. Dann könntest du mehr Menschen erreichen. :-)

--- english ---

wow, looks nice. Just tested the script and it works really well!

Some ideas came immediately to my mind:
  1. It would be nice to have an option to not fully delete the link-file, but only the currently selected video. This way you can keep your search term, but also get rid of videos you don't like.
  2. When there is a generated link-file, you should be able to decide if you use that on startup or go with a new search term.
Finally: as this is an international forum, I think, it would be nice to have your first post also in English so that the majority of people have access to to :-).

Keep up the good work,
Augustulus
VirusHD
Explorer
Explorer
Posts: 12
Joined: Fri Jan 08, 2021 9:02 am

Re: [SCRIPT] Randomizer Hypnotube GER

Post by VirusHD »

Danke für Feedback. Ich werde die Änderungen gerne übernehmen.
Ich melde mich dann sobald ich es fertig habe.
Auch werde ich das ganze noch mal ins Englische übersetzten.

--- Englisch---

Thanks for feedback. I will happily adopt the changes.
I'll get back to you as soon as I've finished it.
I will also translate the whole thing into English again.
VirusHD
Explorer
Explorer
Posts: 12
Joined: Fri Jan 08, 2021 9:02 am

Re: [SCRIPT] Randomizer Hypnotube GER/ENG

Post by VirusHD »

Update des Script auf Version 1.1
Siehe Changelog für Änderungen
-----------
Update the script to version 1.1
See changelog for changes
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests