Created
July 28, 2022 18:23
-
-
Save yportne8/9a41cba45ecfdc607fe72fee30c0ef9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import ( | |
Tk, Button, Canvas, Entry, | |
PhotoImage, StringVar, filedialog) | |
from multiprocessing import Process | |
from random import shuffle | |
from pathlib import Path | |
from time import sleep | |
import os, sys | |
from win32gui import GetWindowText, GetForegroundWindow | |
from win32api import GetSystemMetrics, SendMessage | |
from WindowsWindow import Controller, KEYMAP | |
from win32con import WM_KEYUP, WM_KEYDOWN | |
from youtube_search import YoutubeSearch | |
from infi.systray import SysTrayIcon | |
from music_artist import MusicArtist | |
from youtube_dl import YoutubeDL | |
from pyNotifier import Notifyer | |
from darkassets import ( | |
tanjant_btn, artist_btn, | |
video_btn, searchbar_background) | |
MEDIUM = \ | |
"""<!DOCTYPE html> <html lang="en-US"> | |
<meta charset="utf-8"> | |
<head> | |
<title>PIP PLAYER</title> | |
</head> | |
<body> | |
<div> | |
<video name="media" width=400 controls controlslist="nofullscreen nodownload" autoplay src="%s"></video> | |
</div> | |
<script> | |
function closewindow() { | |
window.close(); | |
}; | |
const media = document.querySelector('video'); | |
media.addEventListener("enterpictureinpicture", media.play); | |
media.addEventListener("ended", closewindow); | |
media.addEventListener("playing", media.requestPictureInPicture); | |
media.addEventListener("leavepictureinpicture", closewindow); | |
window.resizeTo(200, 200); | |
window.moveTo(1720, 1050); | |
</script> | |
</body> | |
</html>""" | |
ROOT, PLAYER = None, None | |
NOTIFYER = Notifyer() | |
def playvideo(TITLES): | |
for title in TITLES: | |
with open(Path(os.environ["TEMP"], "now.playing"), "w+") as f: | |
f.write(title) | |
try: | |
query = f"ytsearch: {title}" | |
with YoutubeDL() as ydl: | |
info = ydl.extract_info(query, download=False) | |
url = info["entries"][0]["formats"][-1]["url"] | |
html = MEDIUM % url | |
with open(Path(os.environ["TEMP"], "index.html"), "w+") as f: | |
f.write(html) | |
uri = Path(os.environ["TEMP"], "index.html").as_uri() | |
cmd = f"start msedge --app={uri}" | |
title = GetWindowText(GetForegroundWindow()) | |
active = Controller(title) | |
os.system(cmd) | |
sleep(5) | |
while True: | |
try: | |
c = Controller("PIP PLAYER") | |
SendMessage(c.hwnd, WM_KEYDOWN, KEYMAP["TAB"], 0) | |
SendMessage(c.hwnd, WM_KEYUP, KEYMAP["TAB"], 0) | |
SendMessage(c.hwnd, WM_KEYDOWN, KEYMAP["SPACE"], 0) | |
SendMessage(c.hwnd, WM_KEYUP, KEYMAP["SPACE"], 0) | |
break | |
except: | |
pass | |
active.focus() | |
try: | |
sleep(2) | |
assert Controller("Picture in picture") | |
Controller("PIP PLAYER").hide() | |
NOTIFYER.notify(title, "Now Playing") | |
active.focus() | |
except: | |
Controller("PIP PLAYER").close() | |
active.focus() | |
except: | |
try: | |
Controller("PIP PLAYER").close() | |
except: | |
pass | |
os._exit(0) | |
def playartist(ARTISTS): | |
while True: | |
try: | |
ARTIST = ARTISTS.pop(0) | |
query = f"%s by {ARTIST}" | |
artist = MusicArtist(ARTIST) | |
tracks = [hit.split("|")[0].strip() for hit in artist.tophits()][:3] | |
shuffle(tracks) | |
track = query % tracks[0] | |
with open(Path(os.environ["TEMP"], "now.playing"), "w+") as f: | |
f.write(f"{track}\n") | |
query = f"ytsearch: {track}" | |
with YoutubeDL() as ydl: | |
info = ydl.extract_info(query, download=False) | |
url = info["entries"][0]["formats"][-1]["url"] | |
html = MEDIUM % url | |
with open(Path(os.environ["TEMP"], "index.html"), "w+") as f: | |
f.write(html) | |
uri = Path(os.environ["TEMP"], "index.html").as_uri() | |
cmd = f"start msedge --app={uri}" | |
title = GetWindowText(GetForegroundWindow()) | |
active = Controller(title) | |
os.system(cmd) | |
sleep(5) | |
while True: | |
try: | |
c = Controller("PIP PLAYER") | |
SendMessage(c.hwnd, WM_KEYDOWN, KEYMAP["TAB"], 0) | |
SendMessage(c.hwnd, WM_KEYUP, KEYMAP["TAB"], 0) | |
SendMessage(c.hwnd, WM_KEYDOWN, KEYMAP["SPACE"], 0) | |
SendMessage(c.hwnd, WM_KEYUP, KEYMAP["SPACE"], 0) | |
sleep(2) | |
break | |
except: | |
active.focus() | |
pass | |
try: | |
assert Controller("Picture in picture") | |
active.focus() | |
NOTIFYER.notify(track, "Now Playing") | |
c.hide() | |
except: | |
Controller("PIP PLAYER").close() | |
active.focus() | |
while True: | |
assert Controller("Picture in picture") | |
except: | |
try: | |
Controller("PIP PLAYER").close() | |
except: | |
pass | |
if not ARTISTS: | |
os._exit(0) | |
def playplaylist(TRACKS): | |
while True: | |
try: | |
track = TRACKS.pop(0) | |
title = f"ytsearch: {track}" | |
with YoutubeDL() as ydl: | |
info = ydl.extract_info(title, download=False) | |
url = info["entries"][0]["formats"][-1]["url"] | |
global MEDIA | |
html = MEDIA % url | |
with open(Path(os.environ["TEMP"], "index.html"), "w+") as f: | |
f.write(html) | |
uri = Path(os.environ["TEMP"], "index.html").as_uri() | |
cmd = f"start msedge --app={uri}" | |
title = GetWindowText(GetForegroundWindow()) | |
active = Controller(title) | |
os.system(cmd) | |
sleep(5) | |
while True: | |
try: | |
c = Controller("PIP PLAYER") | |
SendMessage(c.hwnd, WM_KEYDOWN, KEYMAP["TAB"], 0) | |
SendMessage(c.hwnd, WM_KEYUP, KEYMAP["TAB"], 0) | |
SendMessage(c.hwnd, WM_KEYDOWN, KEYMAP["SPACE"], 0) | |
SendMessage(c.hwnd, WM_KEYUP, KEYMAP["SPACE"], 0) | |
sleep(2) | |
break | |
except: | |
active.focus() | |
pass | |
try: | |
assert Controller("Picture in picture") | |
active.focus() | |
NOTIFYER.notify(track, "Now Playing") | |
c.hide() | |
except: | |
Controller("PIP PLAYER").close() | |
active.focus() | |
while True: | |
assert Controller("Picture in picture") | |
except: | |
try: | |
Controller("PIP PLAYER").close() | |
except: | |
pass | |
if not TRACKS: | |
os._exit(0) | |
def main(query, query_type): | |
if query_type == "artist": | |
try: | |
similar_artists = \ | |
[artist.split("~")[0].strip() \ | |
for artist in MusicArtist(query).similar()] | |
similar_artists.extend([query]*int(len(similar_artists)/6)) | |
shuffle(similar_artists) | |
query = similar_artists | |
action = playartist | |
except: | |
msg = "Failed to identify similar artists." | |
print(msg) | |
sys.exit(0) | |
elif query_type == "video": | |
action = playvideo | |
query = [d["title"] for d in YoutubeSearch(query, max_results=20).to_dict()] | |
shuffle(query) | |
elif query_type == "playlist": | |
action = playplaylist | |
shuffle(query) | |
global PLAYER | |
PLAYER = Process(target=action, args=(query,)) | |
PLAYER.start() | |
if __name__ == "__main__": | |
def fav(sysTrayIcon): | |
try: | |
path = Path(os.environ["TEMP"], "now.playing") | |
with open(path, "r") as f: | |
track_by_artist = f.read().strip() | |
path = Path(Path.home(), "Music", "tanjant.favs") | |
if not path.exists(): | |
path.touch() | |
with open(path, "a") as f: | |
f.write(f"{track_by_artist}\n") | |
global NOTIFYER | |
NOTIFYER.notify("Added to Music\tanjant.favs.", track_by_artist) | |
except: | |
pass | |
def quit(sysTrayIcon): | |
global PLAYER | |
try: | |
PLAYER.terminate() | |
PLAYER.join() | |
except: | |
pass | |
PLAYER = None | |
hover_text = "Tanjant Radio" | |
menu_options = ( | |
('Favorite', None, fav),) | |
systray_ico = str(Path( | |
Path.home(), | |
"TanjantRadio", | |
"tanjant.ico")) | |
root = Tk() | |
query, query_type = StringVar(), "artist" | |
try: | |
Controller("Tanjant Radio").hide() | |
except: | |
pass | |
def enter(*args): | |
global query | |
query = query.get() | |
root.destroy() | |
def artist_click(*args): | |
global query_type | |
query_type = "artist" | |
enter() | |
def video_click(*args): | |
global query_type | |
query_type = "video" | |
enter() | |
def get_playlist(*args): | |
playlist = filedialog.askopenfilename() | |
global query, query_type | |
if playlist: | |
try: | |
with open(playlist, "r") as f: | |
playlist = [line.strip() for line in f.readlines()] | |
try: | |
query = [artist.split("by")[1].strip() for artist in playlist] | |
query_type = "artist" | |
except: | |
query_type = "playlist" | |
except: | |
query_type = "video" | |
if not type(playlist) == list: | |
msg = "Failed to parse playlist. " | |
msg += "Please enjoy a video about the Amen Break." | |
NOTIFYER.notify(msg) | |
playlist = ["Nate Harrison Amen Break"] | |
query = playlist | |
root.destroy() | |
root.geometry(f"579x54+15+{GetSystemMetrics(1)-134}") | |
root.configure(bg = "#222222") | |
canvas = Canvas(root, bg = "#222222", | |
height = 54, width = 579, | |
bd = 0, highlightthickness = 0, | |
relief = "flat") | |
canvas.place(x = 0, y = 0) | |
bkgd_img = PhotoImage(data=searchbar_background) | |
bkgd = canvas.create_image(289.0, 27.0, image=bkgd_img) | |
btn_artist_img = PhotoImage(data=artist_btn) | |
btn_artist = Button( | |
image=btn_artist_img, borderwidth=0, | |
highlightthickness=0, command=artist_click, | |
relief="flat") | |
btn_artist.place(x=476.0, y=11.0, width=45.0, height=32.0) | |
btn_video_img = PhotoImage(data=video_btn) | |
btn_video = Button( | |
image=btn_video_img, borderwidth=0, | |
highlightthickness=0, command=video_click, | |
relief="flat") | |
btn_video.place(x=526.0, y=11.0, width=45.0, height=32.0) | |
btn_tanjant_img = PhotoImage(data=tanjant_btn) | |
btn_tanjant = Button( | |
image=btn_tanjant_img, borderwidth=0, | |
highlightthickness=0, command=get_playlist, | |
relief="flat") | |
btn_tanjant.place(x=12.0, y=6.0, width=34.0, height=38.0) | |
inputbox = Entry( | |
bd=0, font=("Verdana", 12, "bold"), justify="center", | |
bg="#FFFFFF", highlightthickness=0, textvariable=query) | |
inputbox.place(x=62, y=8, width=398, height=36) | |
inputbox.bind("<Return>", enter) | |
root.overrideredirect(True) | |
root.resizable(False, False) | |
root.attributes('-topmost',True) | |
root.wm_attributes('-transparentcolor','#222222') | |
root.mainloop() | |
main(query, query_type) | |
sysTrayIcon = SysTrayIcon( | |
systray_ico, hover_text, | |
menu_options, | |
on_quit=quit, | |
default_menu_index=1) | |
sysTrayIcon.start() | |
if not PLAYER: | |
try: | |
Controller("Picture in picture").close() | |
except: | |
pass | |
try: | |
Controller("PIP PLAYER").close() | |
except: | |
pass | |
try: | |
Controller("Tanjant Radio").close() | |
except: | |
pass | |
try: | |
sys.exit(0) | |
except: | |
os._exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment