Added base daun functionality

This commit is contained in:
BarsTiger
2022-04-08 21:27:26 +03:00
parent da79421bc1
commit 57087f2607
10 changed files with 206 additions and 1 deletions

4
.gitignore vendored
View File

@@ -1 +1,3 @@
/.idea/
/.idea/
*.spec
/build/

62
daun.py
View File

@@ -0,0 +1,62 @@
import argparse
parser = argparse.ArgumentParser(prog='daun',
description='Dumb Additional Util Nativeier - if you see this but didn\'t download '
'it, you may have a virus or your friend is motherhacker :)')
parser.add_argument('--add-path', help='folder to add to path',
metavar='P:/ath/To/Folder', dest='add_path')
parser.add_argument('--add-var', help='add variable to environment and its value',
nargs=2, dest='add_var', metavar=('VAR', 'value'))
parser.add_argument('--screenshot', help='make a screenshot of all screens, and save it to specified file or to imgur'
' if no file specified and client id is specified',
metavar='P:/ath/To/Folder/With/screenshot.jpg', const='imgur', nargs='?')
parser.add_argument('--imgur', help='imgur app client id',
metavar='69zxc420228wtf', dest='imgur_client_id')
parser.add_argument('--wp-control', help='control command for wallpaper engine',
choices=['pause', 'stop', 'play', 'mute', 'unmute'],
dest='wp_control')
parser.add_argument('--set-wallpaper', help='link (web image url or file path) to wallpaper to be set',
metavar='P:/ath/To/wallpaper.jpg', dest='set_wallpaper')
parser.add_argument('-d', '--download', help='download file from url to specified',
nargs=2, metavar=('https://sample.url/to/file.ext', 'P:/ath/To/Folder/With/file.ext'),
dest='download')
args = parser.parse_args()
if args.add_path:
from modules import path
path.add_to_path(program_path=args.add_path)
print('Added {} to path'.format(args.add_path))
if args.add_var:
from modules import path
path.add_var(program_path=args.add_var[1], name=args.add_var[0])
print('Added {0} to environment with value {1}'.format(args.add_var[0], args.add_var[1]))
if args.screenshot:
from modules import screenshot
if args.screenshot == 'imgur':
if args.imgur_client_id:
print(screenshot.upload_to_imgur(client_id=args.imgur_client_id))
else:
print('You need to specify client id to upload to imgur')
else:
print(screenshot.save_screenshot(filename=args.screenshot))
if args.wp_control:
from modules.wallpaperengine import control_we
control_we(args.wp_control)
if args.set_wallpaper:
from modules.wallpaper import set_wallpaper
set_wallpaper(args.set_wallpaper)
if args.download:
from modules.download import download
download(args.download[0], args.download[1])

10
modules/download.py Normal file
View File

@@ -0,0 +1,10 @@
# Module for downloading files from the internet
import requests
def download(url: str, file_name: str) -> None:
"""
Downloads a file from the internet and saves it to the specified file
"""
with open(file_name, 'wb') as f:
f.write(requests.get(url).content)

41
modules/path.py Normal file
View File

@@ -0,0 +1,41 @@
# Module for PATH actions
import os
import winreg
def get_path() -> str:
"""
Returns the current PATH variable
"""
existing_path_value_s = set()
for i in os.popen('PATH').read()[5:].replace('\n', '').split(';'):
existing_path_value_s.add(i + ';')
existing_path_value = str()
for i in sorted(list(existing_path_value_s)):
existing_path_value += i
return existing_path_value
def add_to_path(program_path: str):
"""
Adds a program to the PATH variable
"""
existing_path_value = get_path()
with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as root: # Get the current user's registry
with winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key: # Open the environment key
new_path_value = \
existing_path_value + \
f'{";" if existing_path_value[-1] != ";" else ""}' + \
program_path + ';' # Create new path value
winreg.SetValueEx(key, "PATH", 0, winreg.REG_EXPAND_SZ, new_path_value) # Update the path value
def add_var(program_path: str, name: str):
"""
Creates a new environment variable
"""
import winreg
with winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) as root: # Get the current user's registry
with winreg.OpenKey(root, "Environment", 0, winreg.KEY_ALL_ACCESS) as key: # Open the environment key
winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, program_path) # Update the path value

31
modules/process.py Normal file
View File

@@ -0,0 +1,31 @@
# Module for windows process related functions
import psutil
def get_pid(process_name: str) -> int:
"""
Get the PID by name
"""
if process_name:
for proc in psutil.process_iter():
if process_name == proc.name():
return proc.pid
def get_location(process_name: str = None, pid: int = None) -> str:
"""
Get the location of a process
Use either process_name or pid
"""
if process_name:
pid = get_pid(process_name)
return psutil.Process(pid).exe() if psutil.Process(pid).exe() != psutil.Process().exe() else None
def kill(process_name: str = None, pid: int = None) -> None:
"""
Stop a process by name or PID
"""
if process_name:
pid = get_pid(process_name)
psutil.Process(pid).kill()

23
modules/screenshot.py Normal file
View File

@@ -0,0 +1,23 @@
# Module for screenshotting
from mss import mss
import tempfile
import pyimgur
import os
def save_screenshot(filename='C:/ProgramData/screenshot.jpg') -> str:
"""
Saves a screenshot to the specified directory and filename
"""
mss().shot(mon=-1, output=filename)
return filename
def upload_to_imgur(client_id: str) -> str:
"""
Saves a screenshot and uploads it to imgur
"""
tempdir = tempfile.TemporaryDirectory()
temp = os.path.join(tempdir.name)
save_screenshot(temp + '\screenshot.jpg')
return pyimgur.Imgur(client_id).upload_image(temp + '\screenshot.jpg').link

18
modules/wallpaper.py Normal file
View File

@@ -0,0 +1,18 @@
# Module to change wallpapers
import ctypes
import os
import requests
def set_wallpaper(link: str) -> None:
"""
Set the wallpaper to the given link or file
"""
if link.startswith('http'):
with open(str(os.getenv('TEMP') + 'wallpaper' + link.split('.')[-1]), 'wb') as f:
f.write(requests.get(link).content)
path = os.getenv('TEMP') + 'wallpaper' + link.split('.')[-1]
else:
path = link
ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 0)

View File

@@ -0,0 +1,11 @@
# Module to enable and disable wallpaper engine
from modules.process import get_location
import os
def control_we(command: str) -> None:
"""
Control wallpaper engine,
pause, stop, play, mute, unmute
"""
os.system(str(get_location("wallpaper64.exe") or get_location("wallpaper32.exe")) + ' -control ' + command)

5
requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
mss
pyimgur
psutil
requests
pyinstaller

2
sizes.txt Normal file
View File

@@ -0,0 +1,2 @@
base - 7 776 328 b - 7.6 Mb
path - 1 089 b - 1 Kb