Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for unicode save_location on windows and python 2 #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions change_wallpaper_reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def load_config():
default["nsfw"] = "False"
default["time"] = "day"
default["display"] = "0"
default["output"] = "Pictures/Wallpapers"
default["output"] = os.path.join("Pictures", "Wallpapers")

config_path = os.path.expanduser("~/.config/change_wallpaper_reddit.rc")
section_name = "root"
Expand Down Expand Up @@ -178,24 +178,22 @@ def detect_desktop_environment():
if response.status_code == requests.codes.ok:
# Get home directory and location where image will be saved
# (default location for Ubuntu is used)
home_dir = os.path.expanduser("~")
save_location = "{home_dir}/{save_dir}/{subreddit}-{id}.jpg".format(home_dir=home_dir, save_dir=save_dir,
subreddit=subreddit,
id=image["id"])
save_location = os.path.join(os.path.expanduser("~"), save_dir, "{}-{}.jpg".format(subreddit, image["id"]))

if os.path.isfile(save_location):
sys.exit("Info: Image already exists, nothing to do, the program is" \
" now exiting")
print("Info: Image already exists")
else:
# Create folders if they don't exist
dir = os.path.dirname(save_location)
if not os.path.exists(dir):
os.makedirs(dir)

# Create folders if they don't exist
dir = os.path.dirname(save_location)
if not os.path.exists(dir):
os.makedirs(dir)
# Write to disk
with open(save_location, "wb") as fo:
for chunk in response.iter_content(4096):
fo.write(chunk)

# Write to disk
with open(save_location, "wb") as fo:
for chunk in response.iter_content(4096):
fo.write(chunk)
print("Setting wallpaper to {}".format(save_location))

# Check OS and environments
platform_name = platform.system()
Expand All @@ -210,12 +208,23 @@ def detect_desktop_environment():

# Windows
if platform_name.startswith("Win"):
SPI_SETDESKWALLPAPER = 0x0014
SPIF_UPDATEINIFILE = 0x01
SPIF_SENDCHANGE = 0x02
# Python 3.x
if sys.version_info >= (3, 0):
ctypes.windll.user32.SystemParametersInfoW(20, 0, save_location, 3)
if isinstance(save_location, bytes):
# ANSI string (bytes)
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, save_location, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)
else:
# Unicode string (str)
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, save_location, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)
# Python 2.x
else:
ctypes.windll.user32.SystemParametersInfoA(20, 0, save_location, 3)
if isinstance(save_location, str):
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, save_location, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)
else:
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, save_location, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE)

# OS X/macOS
if platform_name.startswith("Darwin"):
Expand Down