~josealberto4444/apodnasabot

ref: fa638d8f2d185847bb2a64d31e4b72cec373f3b9 apodnasabot/api.py -rw-r--r-- 3.3 KiB
fa638d8fJosé Alberto Orejuela García Add usage of basic functions 2 years ago
                                                                                
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
89
90
91
92
93
94
# Copyright © 2019 José Alberto Orejuela García (josealberto4444)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import configparser
import os.path
import re
import requests
import youtube_dl

def read_config(section, key):
    config = configparser.ConfigParser()
    config.read('config.cfg')
    return config[section][key]

def ask_api(api_key, date):
    baseurl = 'https://api.nasa.gov/planetary/apod'
    payload = {'api_key': api_key, 'date': date}
    r = requests.get(baseurl, params=payload)
    return r.json()

def save_json(filename, api_response):
    with open(filename + '.json', 'wt') as f:
        f.write(str(api_response))

def save_media(filename, api_response):
    if api_response['media_type'] == 'image':
        r = requests.get(api_response['hdurl'])
        filename = filename + '.jpg'
        with open(filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=128):
                f.write(chunk)
    elif api_response['media_type'] == 'video':
        filename = filename + '.%(ext)s'
        ydl_opts = {'outtmpl': filename, 'quiet': True}
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([api_response['url']])

def save_api_response(api_response):
    filename = 'data/' + api_response['date']
    save_json(filename, api_response)
    save_media(filename, api_response)

def intl_to_short(date):
    shortdate = date.replace('-', '')
    shortdate = shortdate[2:]
    return shortdate

def get_userpage(date):
    shortdate = intl_to_short(date)
    url = 'https://apod.nasa.gov/apod/ap' + shortdate + '.html'
    payload = {}
    r = requests.get(url, params=payload)
    return r.text

def scrap_description(pagesource):
    re_description = re.compile("Explanation: </b> (.*?)<p>", flags=re.DOTALL)
    description = re_description.search(pagesource).groups()[0]
    description = description.replace('\n>', '>')
    description = description.replace('\n', ' ')
    description = re.sub('\s+', ' ', description).strip()
    return description

def save_description(date, description):
    filename = 'data/' + date
    with open(filename + '.html', 'wt') as f:
        f.write(description)

date = '2019-07-10'

if not os.path.exists('data/' + date + '.json'):
    api_key = read_config('NASA_API', 'api_key')
    api_response = ask_api(api_key, date)
    if 'code' in api_response:
        if api_response['code'] == 400:
            print(api_response['msg'])
    else:
        save_api_response(api_response)
        userpage = get_userpage(date) # TO-DO: This line and the following are prone to errors, use a try-catch structure.
        description = scrap_description(userpage)
        save_description(date, description)
else:
    print('It was already downloaded.')