software / SONGRECOGNIZER.py
shethjenil's picture
Update SONGRECOGNIZER.py
62bbb8a verified
# from cv2 import imdecode,IMREAD_GRAYSCALE ,TM_CCOEFF_NORMED , matchTemplate
# from numpy import frombuffer,uint8,where
# from io import BytesIO
# from requests import get
# def find_element_coordinatesofmic():
# template = imdecode(frombuffer(BytesIO(get("https://shethjenil-automatic-song-rec.static.hf.space/voiceelement.jpg").content).read(), uint8), IMREAD_GRAYSCALE)
# screen_image = imdecode(frombuffer(device.screencap(), uint8), IMREAD_GRAYSCALE)
# w, h = template.shape[::-1]
# loc = where(matchTemplate(screen_image, template, TM_CCOEFF_NORMED) >= 0.8)
# return [loc[1][0] + w // 2, loc[0][0] + h // 2]
from os import getenv, path, remove, makedirs
import winshell
import sys
from shutil import copyfile , rmtree
def add_to_startmenu(target, shortcut_name):
shortcut = winshell.shortcut(path.join(winshell.start_menu(), f"{shortcut_name}.lnk"))
shortcut.path = target
print(shortcut.show_cmd)
shortcut.write()
def remove_from_startmenu(shortcut_name):
remove(path.join(winshell.start_menu(), f"{shortcut_name}.lnk"))
if len(sys.argv) == 2:
if sys.argv[1] == "--uninstall":
if path.exists(path.join(getenv("LOCALAPPDATA"), "JENILSOFTWARESONGREC")):
rmtree(path.join(getenv("LOCALAPPDATA"), "JENILSOFTWARESONGREC"))
remove_from_startmenu("Song Recognizer")
sys.exit()
if not path.exists(path.join(getenv("LOCALAPPDATA"), "JENILSOFTWARESONGREC")) or (len(sys.argv) == 2 and sys.argv[1] == "--install"):
try:
# Create directory if it doesn't exist
makedirs(path.join(getenv("LOCALAPPDATA"), "JENILSOFTWARESONGREC"))
except OSError as e:
print("Error creating directory:", e)
sys.exit(1)
try:
# Copy file to the created directory
copyfile(sys.argv[0], path.join(getenv("LOCALAPPDATA"), "JENILSOFTWARESONGREC", sys.argv[0].split("\\")[-1]))
add_to_startmenu(path.join(getenv("LOCALAPPDATA"), "JENILSOFTWARESONGREC", sys.argv[0].split("\\")[-1]), "Song Recognizer")
except IOError as e:
print("Error copying file:", e)
sys.exit(1)
sys.exit()
from subprocess import Popen , DEVNULL
from time import sleep
from ppadb.client import Client
from os import remove,chdir
from pytube import Search
from xml.etree.ElementTree import parse
from json import dump ,load
from tkinter import messagebox
from tkinter.simpledialog import askstring
chdir(getattr(sys, '_MEIPASS', path.abspath(path.dirname(__file__))))
#change a path to your project folder
try:
messagebox.showinfo(message="this program is made by Vicky,varshil,jeet(leader),preet to dedicating SCIENCE FEST 2024")
messagebox.showinfo(message="in mobile in setting in system build number tap 7 times to open developer options Enable USB debugging and then connect your mobile to pc and tap ok")
Popen(r"data\adb kill-server", shell=True, stdout=DEVNULL, stderr=DEVNULL)
Popen(r"data\adb start-server", shell=True, stdout=DEVNULL, stderr=DEVNULL)
while True:
global device
try:
device = Client(host="127.0.0.1", port=5037).devices()[0]
break
except:
messagebox.showwarning(message="waiting for device")
pass
messagebox.showwarning(message="please unlock and open your mobile during project")
global screentimeout
while True:
password = askstring("password","enter password")
if password.strip() != "":
if device.shell(f"locksettings clear --old {password}").strip() == "Lock credential cleared":
device.shell(f"#locksettings set-disabled 1")
break
else:
messagebox.showwarning(message="wrong password")
messagebox.showwarning(message="""IF YOU HAVE A PATTERN LOCK THEN ENTER A POINT NUMBER INSTEAD OF PASSWORD LIKE FOR
UPPER LEFT POINT IS 1 AND LAST BOTTOM RIGHT IS 9 MIDDLE POINT IS 5 AND LAST BOTTOM LEFT IS 7""")
else:
if device.shell("locksettings clear").strip() == "Lock credential cleared":
device.shell(f"#locksettings set-disabled 1")
break
else:
messagebox.showwarning(message="wrong password")
messagebox.showwarning(message="""IF YOU HAVE A PATTERN LOCK THEN ENTER A POINT NUMBER INSTEAD OF PASSWORD LIKE FOR
UPPER LEFT POINT IS 1 AND LAST BOTTOM RIGHT IS 9 MIDDLE POINT IS 5 AND LAST BOTTOM LEFT IS 7""")
device.shell("input keyevent KEYCODE_SLEEP")
screentimeout=device.shell("settings get system screen_off_timeout")
device.shell("settings put system screen_off_timeout 1800000")
if device.shell(f"ls /sdcard/cfg.json").strip() != "/sdcard/cfg.json":
device.shell("am start -n com.google.android.googlequicksearchbox/com.google.android.apps.search.googleapp.activity.GoogleAppActivity")
device.shell("uiautomator dump")
device.pull("/sdcard/window_dump.xml", "window_dump.xml")
a = [node.attrib['bounds'] for node in parse('window_dump.xml').getroot().iter() if "text" in node.attrib and node.attrib["content-desc"] == "Voice search"][0].split("][")[0].replace("[","").split(",")
mic = [int(a[0]), int(a[1])]
device.input_tap(mic[0], mic[1])
sleep(1)
device.shell("uiautomator dump")
device.pull("/sdcard/window_dump.xml", "window_dump.xml")
a = [node.attrib['bounds'] for node in parse('window_dump.xml').getroot().iter() if "text" in node.attrib and node.attrib["text"] == "Search for a song"][0].split("][")[0].replace("[","").split(",")
songbutton=[int(a[0]), int(a[1])]
dump({"mic": mic, "songbutton": songbutton}, open("cfg.json", "w+"))
device.push("cfg.json", "/sdcard/cfg.json")
device.shell("am force-stop com.google.android.googlequicksearchbox")
device.shell("rm /sdcard/window_dump.xml")
remove("cfg.json")
remove("window_dump.xml")
else:
device.pull("/sdcard/cfg.json", "cfg.json")
cfg = load(open("cfg.json", "r"))
cfg = load(open("cfg.json", "r"))
mic = cfg["mic"]
songbutton = cfg["songbutton"]
def get_song_artist():
device.shell("svc data enable")
device.shell("input keyevent KEYCODE_WAKEUP")
global mic , songbutton
device.shell("am start -n com.google.android.googlequicksearchbox/com.google.android.apps.search.googleapp.activity.GoogleAppActivity")
sleep(2)
device.input_tap(mic[0],mic[1]) #mic
device.input_tap(songbutton[0],songbutton[1]) #songbutton
sleep(20)
device.shell("uiautomator dump")
device.pull("/sdcard/window_dump.xml", "window_dump.xml")
song, artist = [node.attrib["text"] for node in parse('window_dump.xml').getroot().iter() if "text" in node.attrib and (text := node.attrib["text"].strip()) != ""][1:3]
remove("window_dump.xml")
device.shell("am force-stop com.google.android.googlequicksearchbox")
device.shell("input keyevent KEYCODE_HOME")
device.shell("svc data disable")
device.shell("input keyevent KEYCODE_SLEEP")
Popen(rf'data\vlc -f --audio-visual=goom --no-video-title-show --no-qt-fs-controller "{list(filter(lambda x: x.itag == 251, Search(f"{song} by {artist}").results[0].streams))[0].url}"', shell=True, stdout=DEVNULL, stderr=DEVNULL).communicate()
while messagebox.askretrycancel(message="RECORD START"):
get_song_artist()
device.shell("settings put system screen_off_timeout " + screentimeout)
Popen(r"data\adb kill-server", shell=True)
except Exception as e:
device.shell("settings put system screen_off_timeout " + screentimeout)
Popen(r"data\adb kill-server", shell=True)