weather/script.py

58 lines
2.0 KiB
Python
Raw Permalink Normal View History

2022-11-04 23:43:25 +07:00
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from datetime import datetime
import base64
from selenium.webdriver.chrome.options import Options
key = "API_KEY"
while(1):
try:
now = datetime.now()
dt_string = now.strftime("%d-%m-%Y %H:%M:%S")
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", chrome_options=options)
browser.delete_all_cookies()
browser.get("https://www.google.com/search?q=weather+yogyakarta")
time.sleep(5)
suhu = browser.find_element_by_xpath("//*[@id=\"wob_tm\"]").get_attribute('textContent')
kelembapan = browser.find_element_by_xpath("//*[@id=\"wob_hm\"]").get_attribute('textContent')
presipitasi = browser.find_element_by_xpath("//*[@id=\"wob_pp\"]").get_attribute('textContent')
angin = browser.find_element_by_xpath("//*[@id=\"wob_ws\"]").get_attribute('textContent')
cuaca = browser.find_element_by_xpath("//*[@id=\"wob_dc\"]").get_attribute('textContent')
last_update = browser.find_element_by_xpath("//*[@id=\"wob_dts\"]").get_attribute('textContent')
2022-11-05 09:26:25 +07:00
suhu = suhu + "℃"
2022-11-04 23:43:25 +07:00
2022-11-05 09:26:25 +07:00
payload = suhu + "=" + kelembapan + "=" + presipitasi + "=" + angin + "=" + cuaca + "=" + last_update + "=" + dt_string + " GMT+7" + "=" + key
2022-11-04 23:43:25 +07:00
print(payload)
payload_encoded = base64.b64encode(bytes(payload, 'utf-8'))
print(payload_encoded.decode('utf-8'))
response = requests.get('https://api.gabrielkheisa.xyz/weather/yogyakarta/index.php?q='+str(payload_encoded.decode('ascii')))
browser.quit()
print("Sleep for 1 hour")
time.sleep(60*60)
except:
print("Error gak jelas, skip")
2022-11-05 09:26:25 +07:00
browser.quit()
print("Sleep for 1 hour")
time.sleep(60*60)