pythonのlibraryの格納場所

そもそも、import urllibとかしているけど、どこにあるの?

.__file__でわかる。

import datetime
import sys
print(datetime.__file__)
print(sys.__file__)

え、隠しファイル.pyenvの下にあるの? マジかよ!!!
[vagrant@localhost python]$ python3 app.py
/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/datetime.py
Traceback (most recent call last):
File “app.py”, line 4, in
print(sys.__file__)
AttributeError: module ‘sys’ has no attribute ‘__file__’

urllibの下にrequest.pyがありますね。なるほど。

request.pyの中身をみてみます。2674行ですね。
なるほど、これはすげーや。

郵便番号から住所を取得

Google Map Apiにパラメーターを送ります。

import urllib.request
import urllib.parse

API = "https://maps.googleapis.com/maps/api/geocode/json"

values = {
	"address": "160-0002",
	"language": "ja",
	"sensor": "false"
}
params = urllib.parse.urlencode(values)

url = API + "?" + params
print("url=", url)

data = urllib.request.urlopen(url).read()
text = data.decode("utf-8")
print(text)

url= https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=160-0002&language=ja
{
“results” : [
{
“address_components” : [
{
“long_name” : “160-0002”,
“short_name” : “160-0002”,
“types” : [ “postal_code” ]
},
{
“long_name” : “四谷坂町”,
“short_name” : “四谷坂町”,
“types” : [ “political”, “sublocality”, “sublocality_level_2” ]
},
{
“long_name” : “新宿区”,
“short_name” : “新宿区”,
“types” : [ “locality”, “political” ]
},
{
“long_name” : “東京都”,
“short_name” : “東京都”,
“types” : [ “administrative_area_level_1”, “political” ]
},
{
“long_name” : “日本”,
“short_name” : “JP”,
“types” : [ “country”, “political” ]
}
],
“formatted_address” : “日本 〒160-0002”,
“geometry” : {
“bounds” : {
“northeast” : {
“lat” : 35.6920455,
“lng” : 139.7292123
},
“southwest” : {
“lat” : 35.6885298,
“lng” : 139.7245306
}
},
“location” : {
“lat” : 35.6907555,
“lng” : 139.7272033
},
“location_type” : “APPROXIMATE”,
“viewport” : {
“northeast” : {
“lat” : 35.6920455,
“lng” : 139.7292123
},
“southwest” : {
“lat” : 35.6885298,
“lng” : 139.7245306
}
}
},
“place_id” : “ChIJoebNl_SMGGAR4LtICJbkh5I”,
“types” : [ “postal_code” ]
}
],
“status” : “OK”
}

ビットコインでいきたい。
bitflyerから取得する。

import urllib.request
import urllib.parse

url = "https://api.bitflyer.jp/v1/getboard"

# values = {
# 	"product_code": "BTC_JPY",
# }
# params = urllib.parse.urlencode(values)

# url = API + "?" + params
# print("url=", url)

data = urllib.request.urlopen(url).read()
text = data.decode("utf-8")
print(text)

Traceback (most recent call last):
File “app.py”, line 14, in
data = urllib.request.urlopen(url).read()
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/urllib/request.py”, line 163, in urlopen
return opener.open(url, data, timeout)
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/urllib/request.py”, line 472, in open
response = meth(req, response)
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/urllib/request.py”, line 582, in http_response
‘http’, request, response, code, msg, hdrs)
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/urllib/request.py”, line 510, in error
return self._call_chain(*args)
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/urllib/request.py”, line 444, in _call_chain
result = func(*args)
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/urllib/request.py”, line 590, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

HTTP Error 403: Forbiddenと出ています。
>特定のアクセス者にページを表示する権限が付与されず、アクセスが拒否されたことを示すもの。また、サイトの制作者側の設計ミスによる障害やサイトが非常に混雑している時、URLが間違っている場合にも表示される事がある。

なに!!!!!!!?

CCさん。大塚さん、顔が広いらしいですね。

url = "https://coincheck.com/api/ticker"

あら、いけますね♪ 403は、後で確認しましょう。
[vagrant@localhost python]$ python3 app.py
{“last”:911398.0,”bid”:911266.0,”ask”:911422.0,”high”:916800.0,”low”:895000.0,”volume”:2483.42296672,”timestamp”:1532828344}

ec2でpython3のcrontabの設定

[ec2-user@hoge ~]$ crontab -e

毎時10分に実行とします。

10 * * * * cd /var/local/amedas; /usr/bin/python3 app.py

[ec2-user@hoge ~]$ crontab -e
crontab: installing new crontab

さあ、反映されるかな。しかし、実感値としては、pythonはvagratよりec2の方がサクサク動きますね。

おおおおおおおおおおおおお、ちゃんと自動で実行されてますね♪

ec2にpython3をインストールする

puttyでec-2にログインして、localでpython app.pyと打つ

[ec2-user@ip-hoge amedas]$ python app.py
File “app.py”, line 18
SyntaxError: Non-ASCII character ‘\xe4’ in file app.py on line 18, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
[ec2-user@ip-172-31-24-16 amedas]$ python app.py
Traceback (most recent call last):
File “app.py”, line 1, in
import urllib.request
ImportError: No module named request

あら、python2系だと駄目のようです。
yum listでPython3がコレクションされているのがわかります。
$ yum list | grep python35
mod24_wsgi-python35.x86_64 3.5-1.25.amzn1 amzn-updates
python35.x86_64 3.5.5-1.12.amzn1 amzn-updates
python35-devel.x86_64 3.5.5-1.12.amzn1 amzn-updates
python35-libs.i686 3.5.5-1.12.amzn1 amzn-updates
python35-libs.x86_64 3.5.5-1.12.amzn1 amzn-updates
python35-pip.noarch 9.0.3-1.26.amzn1 amzn-updates
python35-setuptools.noarch 36.2.7-1.33.amzn1 amzn-main
python35-test.x86_64 3.5.5-1.12.amzn1 amzn-updates
python35-tools.x86_64 3.5.5-1.12.amzn1 amzn-updates
python35-virtualenv.noarch 15.1.0-1.14.amzn1 amzn-main

入れていきます。
$ yum install python35-devel python35-libs python35-setuptools
$ /usr/bin/easy_install-3.5 pip

もう一度コマンドを打ちます。

wow はや
[ec2-user@hoge amedas]$ python3 app.py
finish

ec-2でもpython35が実行されているのがわかります。

index.html, style.cssものせると、awsにのりました。

centosの時刻を合わせる

[vagrant@localhost python]$ date
2018年 7月 28日 土曜日 22:37:15 JST

そんなあほな。
[vagrant@localhost python]$ sudo yum -y install ntp
[vagrant@localhost python]$ sudo ntpdate ntp.nict.jp
29 Jul 07:45:47 ntpdate[4594]: step time server 133.243.238.244 offset 32583.641597 sec
[vagrant@localhost python]$ date
2018年 7月 29日 日曜日 07:46:03 JST

pugを使う

コマンドを立ち上げると、pugが見つかりませんとなるので
[vagrant@localhost python]$ pug index.pug
-bash: pug: コマンドが見つかりません

[vagrant@localhost python]$ source ~/.nvm/nvm.sh

sassも起動しておく
sass –watch css/style.sass:css/style.css

ベクターでリストの画像を作成

photoshopで16 x 16 pixelで新規を作り、pencilで色を塗っていく。

あれ、すげー真ん中からズレてるやんけ。

背景を表示したい。

やり直します。

書き出し。

あれ、あれ?
つうか、デザイナーってこう言う事やってんだ。ある意味凄いな。
あああああああああ、aiもやらなきゃ。なんなんだろう。

python, pug, sassで温度、降水量、風量・風向き

python3

import urllib.request
from datetime import datetime

time = datetime.strftime(datetime.now(), "%Y%m%d%H")

url1 = "https://www.jma.go.jp/jp/amedas/imgs/temp/000/"+ time +"00-00.png"
savename = "image/temp.png"
urllib.request.urlretrieve(url1, savename)

url2 = "https://www.jma.go.jp/jp/amedas/imgs/rain/000/"+ time +"00-00.png"
savename = "image/rain.png"
urllib.request.urlretrieve(url2, savename)

url3 = "https://www.jma.go.jp/jp/amedas/imgs/wind/000/"+ time +"00-00.png"
savename = "image/wind.png"
urllib.request.urlretrieve(url3, savename)

print("保存しました")

pug

doctype html
html(lang="ja")
	head
		meta(charset="utf-8")
		link(rel='stylesheet', href="css/style.css")
		title アメダス
	body
		h1 アメダス全国
		hr
		section#top
			h2 降水量
			img.image(
				src="/image/rain.png"
			)
		section#top
			h2 気温
			img.image(
				src="/image/temp.png"
			)
		section#top
			h2 風向・風速
			img.image(
				src="/image/wind.png"
			)

sass

body
	margin: 5px
h1, h2
	margin-top: 0px
	margin-bottom: 0px
#top
	width: 320px
	float: left
.image
	width: 300px
	height: 225px

– jsで日時を表示したい
– illustratorでアイコンを作りたい
– crontabを設定したい
– awsに載せたい
– RSSのニュースを取得して表示したい

あれ、pugは変数もたせて、foreachとかできるんだっけ?できれば、繰り返し処理にしたいところ。

pugでstyle(src=”css/style.css”)だと効かない

pugでstyle(src=”css/style.css”)と書いても効かない。

doctype html
html(lang="ja")
	head
		meta(charset="utf-8")
		style(src="css/style.css")
		title アメダス
	body
		h1 アメダス
		p.image: img(
			src="/image/rain.png"
		)

なので、このように書きます。

link(rel='stylesheet', href="css/style.css")

[vagrant@localhost python]$ pug index.pug

rendered index.html

body
	background-color: lightgray
	color: blue

おう、それっぽくなった。

あ、pythonのコマンドで画像をアップデートします。
[vagrant@localhost python]$ python app.py
保存しました

いずれは,crontabで自動化する。