Skip to content

Commit

Permalink
New Version 2 commit
Browse files Browse the repository at this point in the history
Changed from text recognition to getting the song link and text from the spotify application itself
  • Loading branch information
Xavlume committed Oct 13, 2023
1 parent 61706cf commit 2815690
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 136 deletions.
20 changes: 3 additions & 17 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
[Coordinates]
x1 = 1
y1 = 2
x2 = 3
y2 = 4
x3 = 5
y3 = 6
x4 = 7
y4 = 8

[Path]
tesseract = C:\\Program Files\\Tesseract-OCR\\tesseract.exe

[Other]
debug = False
savetolog = False
savetoclipboard = True
[Config]
savetolog = True
savetoclipboard = False

[Text]
prefix = I'm Listening to
Expand Down
163 changes: 44 additions & 119 deletions stt.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,72 @@
import PySimpleGUI as sg

from PIL import ImageGrab
from pytesseract import pytesseract
from win32clipboard import OpenClipboard, EmptyClipboard, SetClipboardText, CloseClipboard
from pyautogui import position
import pymem
import requests
import html
import configparser

from win32clipboard import OpenClipboard, EmptyClipboard, SetClipboardText, CloseClipboard

config = configparser.ConfigParser()
config.read('config.ini')

coords = config["Coordinates"]
x1, y1, x2, y2, x3, y3, x4, y4 = int(coords['x1']), int(coords['y1']), int(coords['x2']), int(coords['y2']), int(coords['x3']), int(coords['y3']), int(coords['x4']),int(coords['y4'])

# Defining paths to tesseract.exe
# and the image we would be using
path_to_tesseract = config["Path"]["tesseract"]
pytesseract.tesseract_cmd = path_to_tesseract

debug = config.getboolean("Other", "debug")
saveToLog = config.getboolean("Other", "saveToLog")
saveToClipboard = config.getboolean("Other", "saveToClipboard")
saveToLog = config.getboolean("Config", "saveToLog")
saveToClipboard = config.getboolean("Config", "saveToClipboard")

prefix, linker, suffix = config["Text"]["prefix"], config["Text"]["linker"], config["Text"]["suffix"]

def validValues(xyValues: list[int]) -> bool:
if xyValues[0] >= xyValues[2] or xyValues[1] >= xyValues[3]:
sg.popup_error("Invalid Title Coordinates")
return False
if xyValues[4] >= xyValues[6] or xyValues[5] >= xyValues[7]:
sg.popup_error("Invalid Artist Coordinates")
return False

return True

def getText(firstBox, secondBox, prefix: str = "", linker = " - ", suffix: str = "", debug: bool = False) -> str:

img = ImageGrab.grab(all_screens=True)
img1 = img.crop(firstBox)
img2 = img.crop(secondBox)

if debug:
img1.save("title.png")
img2.save("artist.png")
# Providing the tesseract executable
# location to pytesseract library

def getTitleAndArtistFromHtml(html: str):
title = ""
artist = ""
html = html.split("</title>")[0]
html = html.split("<title>")[1]
title, artist = html.split("song and lyrics")
title = title[:-3]
artist = artist[4:]
artist = artist.replace(" | Spotify", "")

title = pytesseract.image_to_string(img1)
artist = pytesseract.image_to_string(img2)
return (title, artist)

title = title.replace("\n", "")
title = title.replace(" ", "")
#chrome_elf.dll+14FA3E string
pm = pymem.Pymem('Spotify.exe')
client = pymem.process.module_from_name(pm.process_handle, "chrome_elf.dll").lpBaseOfDll
headers = {'Accept-Encoding': 'identity'}

artist = artist.replace("\n", "")
artist = artist.replace(" ", "")
def getStuffs():
songthing = pm.read_string(client + 0x14FA3E)
link = "https://open.spotify.com/track/" + songthing
r = requests.get(link, headers=headers)
content = r.content[:1000]
content = str(content.decode('utf-8'))
content = html.unescape(content)
# content = content.replace("&#x27;", "'")
# content = content.replace("&quot;", "\"")

text = prefix + " " + title + " " + linker + " " + artist + suffix
title, artist = getTitleAndArtistFromHtml(content)

return text
# Displaying the extracted text
return title, artist

def textToClipboard(text: str):
OpenClipboard()
EmptyClipboard()
SetClipboardText(text)
CloseClipboard()

import PySimpleGUI as sg

sg.theme('Dark Blue 3') # please make your windows colorful

layout = [
[sg.Text('Current Mouse Position: ( , )', key="mouseLabel")],

[sg.Text('')],

[sg.Text('Title Coordinates: ')],
[sg.Text('Top Left x and y: ALT-F5', size=(25, 1)), sg.InputText(x1, key='x1',size=(10, 1)), sg.InputText(y1, key='y1', size=(10, 1))],
[sg.Text('Bottom right x and y: ALT-F6', size=(25, 1)), sg.InputText(x2, key='x2',size=(10, 1)), sg.InputText(y2, key='y2', size=(10, 1))],

[sg.Text('Artist Coordinates: ')],
[sg.Text('Top Left x and y: ALT-F7', size=(25, 1)), sg.InputText(x3, key='x3',size=(10, 1)), sg.InputText(y3, key='y3', size=(10, 1))],
[sg.Text('Bottom right x and y: ALT-F8', size=(25, 1)), sg.InputText(x4, key='x4',size=(10, 1)), sg.InputText(y4, key='y4', size=(10, 1))],
[sg.Text('Spotify To Text')],

[sg.Text('')],

[sg.Text('Prefix: ', size=(15, 1)), sg.InputText(prefix, key='prefix',size=(20, 1))],
[sg.Text('Linker: ', size=(15, 1)), sg.InputText(linker, key='linker',size=(20, 1))],
[sg.Text('Suffix: ', size=(15, 1)), sg.InputText(suffix, key='suffix',size=(20, 1))],

[sg.Checkbox("Debug", default=debug, key="debug"), sg.Checkbox("Save Output To Log File",default=saveToLog, key="saveToLog"), sg.Checkbox("Save Output To Clipboard",default=saveToClipboard, key="saveToClipboard")],
[sg.Checkbox("Save Output To Log File",default=saveToLog, key="saveToLog"), sg.Checkbox("Save Output To Clipboard",default=saveToClipboard, key="saveToClipboard")],

[sg.Text('')],

[sg.Button("Apply"), sg.Button("Save to Config")],
[sg.Button("Get Text"), sg.Button("Save to Config")],

[sg.Text('')],

Expand All @@ -101,94 +75,45 @@ def textToClipboard(text: str):

window = sg.Window('Spotify To Text', layout, finalize=True)

window.bind("<Alt_L><F5>", "ALT-f5")
window.bind("<Alt_L><F6>", "ALT-f6")
window.bind("<Alt_L><F7>", "ALT-f7")
window.bind("<Alt_L><F8>", "ALT-f8")

while True:
event, values = window.read(timeout=20)

mousePosition = position()
mousePositionText = f"( {mousePosition[0]} , {mousePosition[1]} )"
mousePositionText = "Current Mouse Position: " + mousePositionText
window["mouseLabel"].update(mousePositionText)

if event == sg.WIN_CLOSED:
break

if event == "__TIMEOUT__":
continue

if event != "Apply" and event != "Save to Config" and "ALT" not in event:
if event != "Get Text" and event != "Save to Config":
continue

try:
debug = window["debug"].Get()
saveToLog = window["saveToLog"].Get()
saveToClipboard = window["saveToClipboard"].Get()

prefix, linker, suffix = values['prefix'], values['linker'], values['suffix']

xyValues = [values['x1'], values['y1'], values['x2'], values['y2'], values['x3'], values['y3'], values['x4'], values['y4']]

invalid = False
for coord in xyValues:
if not coord.isdigit():
sg.popup_error(f"invalid coord: {coord}")
invalid = True

if invalid:
continue

xyValues = [int(value) for value in xyValues]

if "ALT" in event:
if "f5" in event:
window["x1"].update(mousePosition[0])
window["y1"].update(mousePosition[1])
if "f6" in event:
window["x2"].update(mousePosition[0])
window["y2"].update(mousePosition[1])
if "f7" in event:
window["x3"].update(mousePosition[0])
window["y3"].update(mousePosition[1])
if "f8" in event:
window["x4"].update(mousePosition[0])
window["y4"].update(mousePosition[1])
continue

if not validValues(xyValues):
continue

if event == "Save to Config":
if sg.popup_yes_no('Do you really want to save to config?') == 'Yes':
config["Path"]["tesseract"] = path_to_tesseract
config["Other"]["debug"] = str(debug)
config["Other"]["saveToLog"] = str(saveToLog)
config["Other"]["saveToClipboard"] = str(saveToClipboard)
config["Config"]["saveToLog"] = str(saveToLog)
config["Config"]["saveToClipboard"] = str(saveToClipboard)
config["Text"]["prefix"], config["Text"]["linker"], config["Text"]["suffix"] = prefix, linker, suffix

xyStringValues = [str(value) for value in xyValues]

for index, key in enumerate(config["Coordinates"].keys()):
config["Coordinates"][key] = xyStringValues[index]

with open('config.ini', 'w') as configfile:
config.write(configfile)

continue


titleBbox = (xyValues[0], xyValues[1], xyValues[2], xyValues[3])
artistBbox = (xyValues[4], xyValues[5], xyValues[6], xyValues[7])
title, artist = getStuffs()

text = getText(titleBbox, artistBbox, prefix, linker, suffix, debug)
text = prefix + " " + title + " " + linker + " " + artist + suffix

text = str(text)

window["output"].update("Output: " + text)

if saveToLog:
with open("log.txt", 'a') as f:
with open("log.txt", 'a', encoding="utf-8") as f:
f.write(text + "\n")

if saveToClipboard:
Expand Down

0 comments on commit 2815690

Please sign in to comment.