first
This commit is contained in:
commit
5f21810bcc
52
README.md
Normal file
52
README.md
Normal file
@ -0,0 +1,52 @@
|
||||
<h1>Get weather data from Google Search using Selenium</h1>
|
||||
<h2>Requirements:</h2>
|
||||
<ul>
|
||||
<li>Python 3.6 or Python 3.7</li>
|
||||
<li>Pip3</li>
|
||||
<li>Selenium Python</li>
|
||||
<li>Chromium driver binary</li>
|
||||
<li>Jupyter Notebook (optional)</li>
|
||||
</ul>
|
||||
<h2>Trivia:</h2>
|
||||
<ul>
|
||||
<li>Suhu = Temperature</li>
|
||||
<li>Kelembapan = Humidity</li>
|
||||
<li>Angin = Wind speed</li>
|
||||
<li>Cuaca = Weather</li>
|
||||
</ul>
|
||||
<h2>Example:</h2>
|
||||
<ul>
|
||||
<li>
|
||||
Yogyakarta (Indonesia)
|
||||
https://api.gabrielkheisa.xyz/weather/yogyakarta/
|
||||
<pre>
|
||||
{
|
||||
"suhu": "24",
|
||||
"kelembapan": "100%",
|
||||
"presipitasi": "14%",
|
||||
"angin": "2 km\/h",
|
||||
"cuaca": "Berawan GMT+7",
|
||||
"last_update": "Jumat 23.00",
|
||||
"server_update": "04-11-2022 23:30:55"
|
||||
}
|
||||
</pre>
|
||||
</li>
|
||||
<li>
|
||||
Yogyakarta (Singapore)
|
||||
https://api.dev.gabrielkheisa.xyz/stocks/usdidr/
|
||||
<pre>
|
||||
{
|
||||
"suhu": "24",
|
||||
"kelembapan": "100%",
|
||||
"presipitasi": "14%",
|
||||
"angin": "2 km\/h",
|
||||
"cuaca": "Cloudy GMT+7",
|
||||
"last_update": "Friday 11:00 pm",
|
||||
"server_update": "04-11-2022 23:21:06"
|
||||
}
|
||||
</pre>
|
||||
</li>
|
||||
|
||||
|
||||
</ul>
|
||||
|
35
index.php
Normal file
35
index.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$q = $_REQUEST["q"];
|
||||
$key = "API_KEY";
|
||||
|
||||
if($q == ""){
|
||||
$myfile = fopen("cache.txt", "r") or die("Unable to open file!");
|
||||
$teks = fread($myfile,filesize("cache.txt"));
|
||||
fclose($myfile);
|
||||
|
||||
$pieces = explode("=", $teks);
|
||||
|
||||
$marks = array("suhu"=>$pieces[0], "kelembapan"=>$pieces[1], "presipitasi"=>$pieces[2], "angin"=>$pieces[3],"cuaca"=>$pieces[4], "last_update"=>$pieces[5], "server_update"=>$pieces[6]);
|
||||
|
||||
echo json_encode($marks, JSON_PRETTY_PRINT);
|
||||
}
|
||||
else {
|
||||
$txt = base64_decode($q);
|
||||
|
||||
$pieces = explode("=", $txt);
|
||||
|
||||
if(strcmp($pieces[7], $key) == 0) {
|
||||
$myfile = fopen("cache.txt", "w") or die("Unable to open file!");
|
||||
fwrite($myfile, $txt);
|
||||
fclose($myfile);
|
||||
echo "Done!";
|
||||
}
|
||||
else {
|
||||
echo "Key Invalid";
|
||||
fclose($myfile);
|
||||
}
|
||||
}
|
||||
?>
|
55
script.py
Normal file
55
script.py
Normal file
@ -0,0 +1,55 @@
|
||||
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')
|
||||
|
||||
|
||||
|
||||
|
||||
payload = suhu + "=" + kelembapan + "=" + presipitasi + "=" + angin + "=" + cuaca + " GMT+7" + "=" + last_update + "=" + dt_string + "=" + key
|
||||
|
||||
|
||||
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")
|
||||
browser.quit()
|
Loading…
Reference in New Issue
Block a user