-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_wallpaper
executable file
·88 lines (74 loc) · 2.76 KB
/
change_wallpaper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/opt/homebrew/bin/python3
# Downloads "Astronomy Picture of the Day" apod.nasa.gov and sets as wallpaper
#
# Installation steps:
# - store this file somewhere and take note of the path
# - change WALLPAPER_DIR to some folder in your home directory
# and create the folder
# - make it executable:
# chmod a+x /path/to/change_wallpaper
# - follow instructions given in the .plist file to make it run daily
#
# https://towardsdatascience.com/~
# a-tutorial-on-scraping-images-from-the-web-using-beautifulsoup-206a7633e948
from bs4 import BeautifulSoup
from urllib.request import urlretrieve
from PIL import Image, ImageFont, ImageDraw
import requests
import subprocess
import re
import datetime
import os.path
import os
FEED_URL = "http://apod.nasa.gov/apod/"
WALLPAPER_DIR = "/Users/melsec/Pictures/Wallpapers"
FILE_DATE = str(datetime.date.today())
def main():
html_page = requests.get(FEED_URL)
soup = BeautifulSoup(html_page.content, 'html.parser')
# images = soup.findAll('img')
hrefs = soup.findAll('a', href=True, )
# print (hrefs[1].attrs['href'])
# center = soup.findAll('center')
# iframe = soup.findAll('iframe')
bold = soup.findAll('b')
# imgApod = FEED_URL + images[0].attrs['src']
imgApod = FEED_URL + hrefs[1].attrs['href']
# vidApod = iframe[0].attrs['src']
# youtube_dl video ->
# http://stackoverflow.com/questions/18054500/ddg#18947879
imgTitle = str(bold[0])
imgTitle = imgTitle[3:-4]
imgName = re.sub(r"^.*/", "", imgApod)
imgFilename = "%s/%s-%s" % (WALLPAPER_DIR, FILE_DATE, imgName)
# print ("Image URL:", imgApod)
# print ("Filename:", imgName)
# print ("Title:", imgTitle)
# print ("Target file:", imgFilename)
if not os.path.isfile(imgFilename):
# print("File already exists.")
#else:
# print("Downloading...")
urlretrieve(imgApod, imgFilename)
# set_desktop_background(imgFilename)
imgFile = Image.open(imgFilename)
imgSizeW, imgSizeH = imgFile.size
# print ("Width = ",imgSizeW, end="\t")
# print ("Height = ",imgSizeH)
# Retina display 15,4" = 2880 × 1800 px
title_text = imgTitle
title_fontsize = 20
title_font = ImageFont.truetype('Krungthep', title_fontsize)
image_editable = ImageDraw.Draw(imgFile)
image_editable.text((30, imgSizeH - (10 + title_fontsize)), title_text, (237, 230, 211), font=title_font)
imgFile.save(imgFilename)
set_desktop_background(imgFilename)
SCRIPT = """/usr/bin/osascript -s o<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""
def set_desktop_background(filename):
# print ("Changing wallpaper...", filename)
subprocess.check_call(SCRIPT%filename, shell=True)
main()