-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuaca
executable file
·103 lines (85 loc) · 2.8 KB
/
cuaca
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
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
'''
Cuaca - CLI Prakiraan Cuaca
Ambil data dari BMKG
LICENCE MIT
@copyright Rifqi Khoeruman Azam <pravodev@gmail.com>
'''
from pathlib import Path
from bs4 import BeautifulSoup
import requests
import json
import time
import sys
# VARIABLE
URL = "http://www.bmkg.go.id/cuaca/prakiraan-cuaca-indonesia.bmkg"
LOCAL_TIME = time.asctime( time.localtime(time.time()) )
# detect location from an IP Address
def getLocation():
res = requests.get("https://ipinfo.io")
return res.json()
# parse province to dictionary type
def parseProvinces(province):
return { province.get_text(): province.get('href') }
# retrive province
def getProvinces(name = ""):
temp = Path('./temp_provinces.json')
if(temp.is_file()):
data = json.load(temp.open('r'))
else:
res = requests.get(URL)
soup = BeautifulSoup(res.text, 'lxml')
provinces_element = soup.find('div', {'class': 'row list-cuaca-provinsi md-margin-bottom-10'})
data = {}
for provinces in provinces_element.find_all('a'):
data[provinces.get_text()] = provinces.get('href')
f = temp.open('w+')
f.write(json.dumps(data))
if(name):
return data[name]
return data
def getCities(province):
res = requests.get(URL+province)
soup = BeautifulSoup(res.text, 'lxml')
# cities_element = soup.find('')
print(URL+province)
def getWeather(city, province):
res = requests.get(URL+province)
soup = BeautifulSoup(res.text, 'lxml')
list_weathers = soup.find('table', {"class": "table table-hover table-striped table-prakicu-provinsi"})
weather_of_city = list_weathers.find('td', text=city).parent.find_all('td')
times = ['Pagi', 'Siang', 'Malam', 'Dini Hari']
jumlah_td = len(weather_of_city)
total_td = 7
selisih = total_td - jumlah_td
data = {}
index = 0
for time in times[selisih:len(times)]:
data[time] = weather_of_city[index+1].get_text()
index += 1
return data
def _help():
print("perintah tersedia:")
print("- cuaca hari ini")
print("- cuaca sekarang")
def _getWeatherToday():
location = getLocation()
province = getProvinces(location.get("region"))
weathers = getWeather(location.get("city"), province)
print("Prakiraan Cuaca di {} pada {} ".format(location.get('city'), LOCAL_TIME))
for cuaca, title in weathers.items():
print("{}: {}".format(cuaca,title), )
def _getWeatherNow():
location = getLocation()
province = getProvinces(location.get("region"))
weathers = getWeather(location.get("city"), province)
print(list(weathers.values())[0])
argument_value = ' '.join(sys.argv[1:len(sys.argv)])
arguments = {
"hari ini": _getWeatherToday,
"sekarang": _getWeatherNow
}
if(argument_value == ''):
_help()
else:
arguments[argument_value]()