[PYTHON] [LIVE] I tried to deliver the sunrise and sunset times nationwide every day

Motive

When I notice it, I often stay at home in the evening, so I want to act when it's bright. Therefore, as a preventive measure against depression peculiar to winter, I will update and display the sunrise and sunset times nationwide every day.

Method

It's sunrise and sunset, but I'm using Ohakon address !? API.

It's a script, but ...

--Create a Qiita article

--Update Qiita articles using api

Divide it into two parts and use cron to set it to run at about 12:00 am.

Development

Script to create Qiita article

import requests
import datetime
from bs4 import BeautifulSoup
from collections import namedtuple
import asyncio
import os

State = namedtuple('State', ['name', 'id', 'lat', 'lng'])

#dataset
states = [
	State("Sapporo",    1, 43.06417,141.34694),
	State("Aomori City",    2, 40.82444,140.74),
	State("Morioka City",    3, 39.70361,141.1525),
	State("Sendai city",    4, 38.26889,140.87194),
	State("Akita City",    5, 39.71861,140.1025),
	State("Yamagata City",    6, 38.24056,140.36333),
	State("Fukushima City",    7, 37.75,140.46778),
	State("Mito City",    8, 36.34139,140.44667),
	State("Utsunomiya City",   9, 36.56583,139.88361),
	State("Maebashi",    10, 36.39111,139.06083),
	State("Saitama City", 11, 35.85694,139.64889),
	State("Chiba",    12, 35.60472,140.12333),
	State("Shinjuku ward",    13, 35.68944,139.69167),
	State("Yokohama",    14, 35.44778,139.6425),
	State("Niigata City",    15, 37.90222,139.02361),
	State("Toyama City",    16, 36.69528,137.21139),
	State("Kanazawa",    17, 36.59444,136.62556),
	State("Fukui City",    18, 36.06528,136.22194),
	State("Kofu City",    19, 35.66389,138.56833),
	State("Nagano city",    20, 36.65139,138.18111),
	State("Gifu City",    21, 35.39111,136.72222),
	State("Shizuoka City",    22, 34.97694,138.38306),
	State("Nagoya city",  23, 35.18028,136.90667),
	State("Tsu city",     24, 34.73028,136.50861),
	State("Otsu City",   25, 35.00444,135.86833),
	State("Kyoto City",   26,35.02139,135.75556),
	State("Osaka City",   27,34.68639,135.52),
	State("Kobe City",   28,34.69139,135.18306),
	State("Nara city",   29,34.68528,135.83278),
	State("Wakayama City",  30, 34.22611,135.1675),
	State("Tottori City",   31,35.50361,134.23833),
	State("Matsue",   32,35.47222,133.05056),
	State("Okayama City",   33,34.66167,133.935),
	State("Hiroshima city",   34,34.39639,132.45944),
	State("Yamaguchi City",   35,34.18583,131.47139),
	State("Tokushima City",   36,34.06583,134.55944),
	State("Takamatsu City",   37,34.34028,134.04333),
	State("Matsuyama City",   38,33.84167,132.76611),
	State("Kochi City",   39,33.55972,133.53111),
	State("Fukuoka City",   40,33.60639,130.41806),
	State("Saga City",   41,33.24944,130.29889),
	State("Nagasaki City",   42,32.74472,129.87361),
	State("Kumamoto City",   43,32.78972,130.74167),
	State("Oita City",   44,33.23806,131.6125),
	State("Miyazaki City",   45,31.91111,131.42389),
	State("Kagoshima City", 46,31.56028,130.55806),
	State("Naha city",   47,26.2125,127.68111)
]

async def get_time_sun_rise_set(year, month, day, state):
    url = 'http://labs.bitmeister.jp/ohakon/api/?'
    payload = {'mode':'sun_moon_rise_set', 'year':year, 'month':month, 'day':day, 'lat':state.lat, 'lng':state.lng}
    response = requests.get(url, params=payload)
    soup = BeautifulSoup(response.content, "html.parser")
    await asyncio.sleep(1)

    sunrise = datetime.datetime.strptime(soup.sunrise_hm.text,'%H:%M')
    sunset = datetime.datetime.strptime(soup.sunset_hm.text,'%H:%M')

    diff_seconds = (sunset - sunrise).total_seconds()
    diff_hours = int(diff_seconds // (60 * 60))
    diff_minutes = int((diff_seconds - diff_hours * (60 * 60)) / 60)

    return state.name, soup.sunrise_hm.text, soup.sunset_hm.text, "{:02d}:{:02d}".format(diff_hours, diff_minutes)

def write_session(foutobj, path):
	with open(path, "r", encoding='utf-8') as fin:
		foutobj.write(fin.read())	


if __name__ == "__main__":
	now = datetime.datetime.now()
	output = "./article.md"
	base_folder = "./templates/"

	loop = asyncio.get_event_loop()
	cors = [get_time_sun_rise_set(now.year, now.month, now.day, s) for s in states]
	result = loop.run_until_complete(
			asyncio.gather(*cors)
		)
	build_parts = ["motive.md", "method.md", "development.md", "contents.md", "future.md", "reference.md"]

	with open("./templates/contents.md", "w", encoding='utf-8') as fout:
		fout.write("# Sunrize/Sunset\n\n")
		fout.write("{}\n\n".format(datetime.datetime.now().strftime("【%Y/%m/%d %H:%Updated at M]")))
		fout.write("|city|sunrize|sunset|hours of sunlight|\n")
		fout.write("|:--|:--|:--|:--|\n")
		for r in result:
			fout.write("|{}|{}|{}|{}|\n".format(*r))
		fout.write("\n\n")

	with open(output, "w", encoding='utf-8') as fout:
		for p in build_parts:
			write_session(fout, os.path.join(base_folder, p))

The time of sunrise and sunset is output using the API with get_time_sun_rise_set (). When setting the request, payload = {'mode':'sun_moon_rise_set','year': year,'month': month,'day': day,'lat': state.lat,'lng': state. Use the date and latitude / longitude parameters as in lng} .

Also, asynchronous processing ʻasyncio` is used just in case to get each api acquisition stably.

	loop = asyncio.get_event_loop()
	cors = [get_time_sun_rise_set(now.year, now.month, now.day, s) for s in states]
	result = loop.run_until_complete(
			asyncio.gather(*cors)
		)

Also, to make it easier to edit later, the sentences in each section are divided and finally aggregated. When simply reading and writing a file, ʻopen (path,'r')was fine, but when executing withcron, an error occurs in the character code. So you need to explicitly set ʻencoding ='utf-8' in the argument. (Refer to What to do when UnicodeDecodeError occurs due to file I / O)

	build_parts = ["motive.md", "method.md", "development.md", "contents.md", "future.md", "reference.md"]
	with open(output, "w", encoding='utf-8') as fout:
		for p in build_parts:
			write_session(fout, os.path.join(base_folder, p))

Update Qiita articles using api

import json
import requests

if __name__ == "__main__":
	BASE_URL = "https://qiita.com/api/v2/items/"
	TOKEN = "--Access token--"
	headers = {"Authorization": f"Bearer {TOKEN}", "Content-Type":"application/json"}

	sentence = ""
	with open("article.md", "r", encoding='utf-8') as fin:
		sentence = fin.read()

	contents = {"title": "[LIVE] I tried to deliver the sunrise and sunset times nationwide every day",
				"tags": [{"name": "Python"},
						{"name":"cron"},
						{"name":"asyncio"},
						{"name":"QiitaAPI"},
						{"name":"api"}],
				"body": sentence,
				"id":"ada82beb3b747b99a05e"
				}

	res = requests.patch(BASE_URL + contents["id"], headers=headers, json=contents)
	print(res)

Issue Access Token is required to use the Qiita API, and is required when requesting the API with requests. You can update the content with requests.patch ({content URL}, {header}, {content content}).

Sunrize/Sunset

[Updated at 01:00 on 2020/06/05]

city sunrize sunset hours of sunlight
Sapporo 3:56 19:10 15:14
Aomori City 4:06 19:05 14:59
Morioka City 4:08 19:00 14:52
Sendai city 4:13 18:57 14:44
Akita City 4:12 19:04 14:52
Yamagata City 4:15 18:59 14:44
Fukushima City 4:16 18:57 14:41
Mito City 4:20 18:53 14:33
Utsunomiya City 4:22 18:56 14:34
Maebashi 4:26 18:59 14:33
Saitama City 4:25 18:55 14:30
Chiba 4:24 18:52 14:28
Shinjuku ward 4:25 18:54 14:29
Yokohama 4:26 18:54 14:28
Niigata City 4:21 19:03 14:42
Toyama City 4:32 19:07 14:35
Kanazawa 4:35 19:09 14:34
Fukui City 4:38 19:09 14:31
Kofu City 4:30 18:59 14:29
Nagano city 4:28 19:03 14:35
Gifu City 4:38 19:05 14:27
Shizuoka City 4:32 18:58 14:26
Nagoya city 4:37 19:04 14:27
Tsu city 4:40 19:04 14:24
Otsu City 4:42 19:08 14:26
Kyoto City 4:43 19:08 14:25
Osaka City 4:44 19:08 14:24
Kobe City 4:46 19:10 14:24
Nara city 4:43 19:07 14:24
Wakayama City 4:47 19:08 14:21
Tottori City 4:47 19:16 14:29
Matsue 4:52 19:20 14:28
Okayama City 4:51 19:15 14:24
Hiroshima city 4:57 19:20 14:23
Yamaguchi City 5:02 19:23 14:21
Tokushima City 4:50 19:10 14:20
Takamatsu City 4:51 19:13 14:22
Matsuyama City 4:58 19:17 14:19
Kochi City 4:55 19:13 14:18
Fukuoka City 5:08 19:26 14:18
Saga City 5:09 19:25 14:16
Nagasaki City 5:12 19:26 14:14
Kumamoto City 5:08 19:22 14:14
Oita City 5:04 19:20 14:16
Miyazaki City 5:08 19:18 14:10
Kagoshima City 5:12 19:20 14:08
Naha city 5:36 19:19 13:43

Future

If you output the sunrise and sunset times nationwide, you can see that the time difference is about 30 to 60 minutes, and the sunshine hours are about 30 to 60 minutes.

Reference

-Ohakon address !? API -QiitaAPI-Post -[How to perform the same process as the curl command in Python](https://intellectual-curiosity.tokyo/2019/08/31/python%E3%81%A7curl%E3%82%B3%E3%83% 9E% E3% 83% B3% E3% 83% 89% E3% 81% A8% E5% 90% 8C% E7% AD% 89% E3% 81% AE% E5% 87% A6% E7% 90% 86% E3% 82% 92% E5% AE% 9F% E8% A1% 8C% E3% 81% 99% E3% 82% 8B% E6% 96% B9% E6% B3% 95 /) -Automate posting with Qiita API

Recommended Posts

[LIVE] I tried to deliver the sunrise and sunset times nationwide every day
I tried LeetCode every day 121 Best Time to Buy and Sell Stock (Python, Go)
I tried LeetCode every day 13. Roman to Integer (Python, Go)
I tried to illustrate the time and time in C language
I tried to display the time and today's weather w
I tried to enumerate the differences between java and python
I displayed the chat of YouTube Live and tried playing
I tried LeetCode every day 122. Best Time to Buy and Sell Stock II (Python, Go)
I tried to move the ball
I tried to estimate the interval.
I tried moving the image to the specified folder by right-clicking and left-clicking
I tried to summarize the umask command
I tried to visualize the age group and rate distribution of Atcoder
I tried to recognize the wake word
I tried to express sadness and joy with the stable marriage problem.
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to extract and illustrate the stage of the story using COTOHA
I tried to touch the COTOHA API
I tried to verify and analyze the acceleration of Python by Cython
I implemented the VGG16 model in Keras and tried to identify CIFAR10
I tried to deliver mail from Node.js and Python using the mail delivery service (SendGrid) of IBM Cloud!
I tried to control the network bandwidth and delay with the tc command
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
I tried to process and transform the image and expand the data for machine learning
[Introduction to AWS] I tried porting the conversation app and playing with text2speech @ AWS ♪
I tried to pass the G test and E qualification by training from 50
I tried web scraping to analyze the lyrics.
I implemented DCGAN and tried to generate apples
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried to touch the API of ebay
I tried to correct the keystone of the image
Qiita Job I tried to analyze the job offer
LeetCode I tried to summarize the simple ones
[Introduction to PID] I tried to control and play ♬
I tried to implement the traveling salesman problem
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I tried to understand how to use Pandas and multicollinearity based on the Affairs dataset.
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried LeetCode every day 108. Convert Sorted Array to Binary Search Tree (Python, Go)
I tried to score the syntax that was too humorous and humorous using the COTOHA API.
I tried to compare the processing speed with dplyr of R and pandas of Python
I tried to learn the sin function with chainer
I tried to graph the packages installed in Python
I tried LeetCode every day 7. Reverse Integer (Python, Go)
I tried to read and save automatically with VOICEROID2 2
I tried to detect the iris from the camera image
I tried to implement and learn DCGAN with PyTorch
I tried to summarize the basic form of GPLVM
I tried to touch the CSV file with Python
I tried to predict the J-League match (data analysis)
I tried to solve the soma cube with python
I tried LeetCode every day 112. Path Sum (Python, Go)
I tried to automatically read and save with VOICEROID2
I tried adding system calls and scheduler to Linux
I tried LeetCode every day 20. Valid Parentheses (Python, Go)
I tried to approximate the sin function using chainer
I tried LeetCode every day 136. Single Number (Python, Go)