View difference between Paste ID: wnaedSV7 and jtRYUDwd
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/env python3.7
2
"""
3
getimg.py
4
5
Gets the current image of the day from NASA and saves this as
6
current_background.jpg. The summary / description text is written
7
to the image.
8
9
Requires:
10
    PIL             (apt-get install python-imaging or pip install PIL)
11
    feedparser      (apt-get install python-feedparser or pip install feedparser)
12
13
Christian Stefanescu
14
http://0chris.com
15
16
Based on the bash script of Jessy Cowan-Sharp - http://jessykate.com
17
http://blog.quaternio.net/2009/04/13/nasa-image-of-the-day-as-gnome-background/
18
19
intelli_draw method from:
20
http://mail.python.org/pipermail/image-sig/2004-December/003064.html
21
22
Made it compatible with Python 3.7
23
"""
24
from pathlib import Path
25
from urllib.request import urlopen
26
import shutil
27
import os
28
29
from PIL import (
30
    Image,
31
    ImageDraw,
32
    ImageFont,
33
)
34
import feedparser
35
36
37
# using configparser to save a initial configuration beside the script
38
# to edit the settings without touching the source code..
39
40
HOME = Path.home()
41
DOWNLOAD_FOLDER = HOME / 'backgrounds/'
42
LINK_PATH = DOWNLOAD_FOLDER / 'current_background.jpg'
43
# Edit the font Path. FiraCode is a free font for programmers with ligatures
44
FONT_PATH = r'C:\Users\Admin\AppData\Local\Microsoft\Windows\Fonts\FiraCode-Regular.ttf'
45
FONT_SIZE = 20
46
EMPTY_ROWS = 3
47
48
# Don't change stuff beyond this point
49
# Updated the rss-feed url
50
FEED_URL = 'https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss'
51
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
52
53
54
def get_latest_entry():
55
    """
56
    Get URL and description of the latest entry in the feed
57
    """
58
    feed = feedparser.parse(FEED_URL)
59
    return (feed.entries[0].enclosures[0].href, feed.entries[0].summary)
60
61
62
def download_file(url):
63
    """
64
    Get the latest NASA image of the day from the feed, returns the name
65
    of the downloaded file.
66
    """
67
    remote_file = urlopen(url)
68
    local_name = url.split('/')[-1]
69
    local_path = DOWNLOAD_FOLDER / local_name
70
    local_path.write_bytes(remote_file.read())
71
    remote_file.close()
72
    return local_path
73
74
75
def intelli_draw(drawer, text, font, containerWidth):
76
    """
77
    Figures out how many lines (and at which height in px) are needed to print
78
    the given text with the given font on an image with the given size.
79
80
    Source:
81
    http://mail.python.org/pipermail/image-sig/2004-December/003064.html
82
83
    # Todo
84
    # This function is too complicated
85
    # Try to remove the nested loops
86
    """
87
    words = text.split()
88
    lines = []
89
    lines.append(words)
90
    finished = False
91
    line = 0
92
    while not finished:
93
        thistext = lines[line]
94
        newline = []
95
        innerFinished = False
96
        while not innerFinished:
97
            if drawer.textsize(' '.join(thistext), font)[0] > containerWidth:
98
                newline.insert(0, thistext.pop(-1))
99
            else:
100
                innerFinished = True
101
        if len(newline) > 0:
102
            lines.append(newline)
103
            line = line + 1
104
        else:
105
            finished = True
106
    tmp = []
107
    for i in lines:
108
        tmp.append(' '.join(i))
109
    lines = tmp
110
    (width, height) = drawer.textsize(lines[0], font)
111
    return (lines, width, height)
112
113
114
def write_description(img_path, text):
115
    """
116
    Write the given text to the given imagefile and overwrite it.
117
118
    # choose better variable names
119
    """
120
    img = Image.open(img_path)
121
    (img_width, img_height) = img.size
122
    draw = ImageDraw.Draw(img)
123
    lines, tmp, h = intelli_draw(draw, text, font, img_width)
124
    j = EMPTY_ROWS
125
    for i in lines:
126
        draw.text((0, 0 + j * h), i, font=font)
127
        j = j + 1
128
    img.save(img_path, 'JPEG')
129
130
131
if __name__ == '__main__':
132
    # argparse ?
133
    if not DOWNLOAD_FOLDER.exists():
134
        DOWNLOAD_FOLDER.mkdir()
135
    url, text = get_latest_entry()
136
    img_path = download_file(url)
137
    write_description(img_path, text)
138
    shutil.copy(img_path, LINK_PATH)