-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote a bunch of code Renamed 'preferences.csv' to 'options.txt' Removed unused import, added sys to exit() Added creation of default options.txt on first launch Added path reading from options.txt Added hotkey reading from options.txt Added customisable hotkeys from options.txt [Mistakenly messed a lot of things in repository in the process =S]
- Loading branch information
Showing
1 changed file
with
89 additions
and
53 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,121 @@ | ||
# Spotify controlled by Global Hotkeys. | ||
# This program registers key presses and uses pywinauto | ||
# to send equivalent hotkey command to Spotify application. | ||
|
||
# Hotkey in Program = Expected Command | ||
# SHIFT + 8 = Volume Up | ||
# SHIFT + 2 = Volume Down | ||
# SHIFT + 4 = Previous Track | ||
# SHIFT + 6 = Next Track | ||
# SHIFT + 5 = Play/Pause | ||
# SHIFT + 1 = Go back 5 seconds | ||
# SHIFT + 3 = Go forward 5 seconds | ||
# SHIFT + 7 = Like/Dislike Track | ||
# SHIFT + 9 = QUIT PROGRAM | ||
# Registers key presses with 'keyboard' and uses 'pywinauto' | ||
# to send equivalent hotkey to Spotify application. | ||
|
||
from pywinauto.application import Application | ||
import csv | ||
import os | ||
import keyboard | ||
import sys | ||
import time | ||
from pywinauto.application import Application | ||
import keyboard | ||
|
||
|
||
# Default hotkeys # If you want to change hotkeys, please refer to options.txt | ||
default_hotkeys =[ | ||
"VolUp=shift+8", | ||
"VolDown=shift+2", | ||
"PrevTrack=shift+4", | ||
"NextTrack=shift+6", | ||
"PlayPause=shift+5", | ||
"Back5s=shift+1", | ||
"Forward5s=shift+3", | ||
"Like=shift+7", | ||
"Quit=shift+9" | ||
] | ||
|
||
|
||
def main(): | ||
# For some reason trying to use pywinauto.Application().connect() doesn't work; | ||
# For some reason trying to use pywinauto's Application().connect() doesn't work; | ||
# Spotify's executable name simply disappears in thin air to never be found | ||
# and I couldn't figure out a way to retrieve it so I could use an already | ||
# opened instance, so on this program's execution it also starts Spotify. | ||
# ¯\_(ツ)_/¯ | ||
|
||
try: | ||
# Try to find user path in preferences | ||
bundle_dir = os.path.abspath(os.path.dirname(__file__)) | ||
preferences = os.path.join(bundle_dir, "preferences.csv") | ||
with open(f"{preferences}", "r") as file: | ||
buffer = csv.reader(file) | ||
for row in buffer: | ||
user_path = "".join(row) | ||
except: | ||
# If no preferences, input user for path | ||
user_path = str("path=") + input("Type your Spotify path\n\ | ||
# Set path for options.txt file along SpotifyGlobal | ||
bundle_dir = os.path.abspath(os.path.dirname(__file__)) | ||
options_file = os.path.join(bundle_dir, "options.txt") | ||
|
||
# Check if options.txt exists | ||
if os.path.exists(options_file) is True: | ||
# If yes open options.txt and read contents to a list | ||
with open(options_file, "r") as file: | ||
file_reader = file.readlines() | ||
options_exists = True | ||
else: | ||
options_exists = False | ||
|
||
if options_exists: | ||
# Try to find user path in options | ||
config = "path=" | ||
for lines in file_reader: | ||
if config in lines: | ||
value = lines[len(config):-1] | ||
break | ||
user_path = value | ||
user_inputed_path = False | ||
else: | ||
# If no options.txt, input user for path | ||
user_path = input("Type your Spotify path\n\ | ||
Paths should look something like:\n\ | ||
C:\\Users\\YOU\\AppData\\Roaming\\Spotify\\Spotify.exe\n") | ||
user_path = "".join(user_path) | ||
print() | ||
# And then create user default path with input | ||
with open(f"{preferences}", "w") as file: | ||
file.write(user_path) | ||
user_inputed_path = True | ||
|
||
try: | ||
# Try to open Spotify | ||
sp = Application().start(rf"{user_path[5:]}") | ||
# Try to open Spotify | ||
sp = Application().start(rf"{user_path}") | ||
sp = sp["Chrome_Widget_Win0"] | ||
print() | ||
except: | ||
# Error message | ||
print("\nCouldn't open Spotify.\n\ | ||
Either PATH typed is wrong or\n\ | ||
preferences.csv file has the wrong PATH.\n\n\ | ||
options.txt has the wrong PATH.\n\n\ | ||
Try again") | ||
# Delete preferences file if invalid path | ||
if os.path.exists(preferences) and os.path.isfile(preferences): | ||
os.remove(preferences) | ||
time.sleep(4) | ||
exit(1) | ||
sys.exit(1) | ||
|
||
# If user inputed path, save it in options.txt | ||
if user_inputed_path is True: | ||
with open(options_file, "a") as file: | ||
file.write(f"path={user_path}\n") | ||
|
||
# If user first time opening, add default hotkeys to options.txt | ||
if options_exists is False: | ||
with open(options_file, "a") as file: | ||
for i in range(len(default_hotkeys)): | ||
file.write(f"{default_hotkeys[i]}\n") | ||
|
||
# If user first time, read options.txt again to get recently added hotkeys | ||
if options_exists is False: | ||
with open(options_file, "r") as file: | ||
file_reader = file.readlines() | ||
|
||
hk = {} # Dict of Hotkeys to use | ||
# Read options.txt for hotkeys and append key:value to hk{} | ||
hotkey_commands = ["VolUp", "VolDown", "PrevTrack", "NextTrack", "PlayPause", "Back5s", "Forward5s", "Like", "Quit"] | ||
for i in range(len(hotkey_commands)): | ||
config = f"{hotkey_commands[i]}=" | ||
for lines in file_reader: | ||
if config in lines: | ||
value = lines[len(config):-1] | ||
hk.update({config: value}) | ||
break | ||
|
||
# Set up hotkeys | ||
keyboard.add_hotkey('shift+8', lambda: sp.send_keystrokes('^{UP}'), time.sleep(1)) | ||
keyboard.add_hotkey('shift+2', lambda: sp.send_keystrokes('^{DOWN}'), time.sleep(1)) | ||
keyboard.add_hotkey('shift+4', lambda: sp.send_keystrokes('^{LEFT}'), time.sleep(1)) | ||
keyboard.add_hotkey('shift+6', lambda: sp.send_keystrokes('^{RIGHT}'), time.sleep(1)) | ||
keyboard.add_hotkey('shift+5', lambda: sp.send_keystrokes('{SPACE}'), time.sleep(1)) | ||
keyboard.add_hotkey('shift+1', lambda: sp.send_keystrokes('+{LEFT}'), time.sleep(1)) | ||
keyboard.add_hotkey('shift+3', lambda: sp.send_keystrokes('+{RIGHT}'), time.sleep(1)) | ||
keyboard.add_hotkey('shift+7', lambda: sp.send_keystrokes('%+{B}'), time.sleep(1)) | ||
|
||
# Running message | ||
print("Aplication is up and running, keep this window open.\n\n\ | ||
keyboard.add_hotkey(hk['VolUp='], lambda: sp.send_keystrokes('^{UP}'), time.sleep(1)) | ||
keyboard.add_hotkey(hk['VolDown='], lambda: sp.send_keystrokes('^{DOWN}'), time.sleep(1)) | ||
keyboard.add_hotkey(hk['PrevTrack='], lambda: sp.send_keystrokes('^{LEFT}'), time.sleep(1)) | ||
keyboard.add_hotkey(hk['NextTrack='], lambda: sp.send_keystrokes('^{RIGHT}'), time.sleep(1)) | ||
keyboard.add_hotkey(hk['PlayPause='], lambda: sp.send_keystrokes('{SPACE}'), time.sleep(1)) | ||
keyboard.add_hotkey(hk['Back5s='], lambda: sp.send_keystrokes('+{LEFT}'), time.sleep(1)) | ||
keyboard.add_hotkey(hk['Forward5s='], lambda: sp.send_keystrokes('+{RIGHT}'), time.sleep(1)) | ||
keyboard.add_hotkey(hk['Like='], lambda: sp.send_keystrokes('%+{B}'), time.sleep(1)) | ||
|
||
# Program running message | ||
print("\nAplication is up and running, keep this window open.\n\n\ | ||
To quit application press SHIFT + 9 or close this window.\n\ | ||
Spotify will stay open.") | ||
|
||
# Keep program running on background until quit hotkey | ||
keyboard.wait(hotkey="shift+9") # Quit on hotkey | ||
keyboard.wait(hotkey=hk['Quit=']) # Quit on hotkey | ||
|
||
|
||
main() |