Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #1

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions Oasis infobyte project/wheather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import sys
import asyncio
import aiohttp
import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QTableWidget, QTableWidgetItem, QHBoxLayout
from PyQt5.QtCore import Qt, QThread, pyqtSignal

API_KEY = "6ddec2f560686e41e571e8cd7fe99e48"
LAT = "28.6139" # Example: New Delhi Latitude
LON = "77.2090" # Example: New Delhi Longitude

class WorkerThread(QThread):
data_fetched = pyqtSignal(list)

def __init__(self, parent=None):
super().__init__(parent)

async def get_past_temperatures(self):
temperatures = []
today = datetime.datetime.now()

async with aiohttp.ClientSession() as session:
for i in range(5):
past_date = today - datetime.timedelta(days=i + 1)
timestamp = int(past_date.timestamp())
print("Time:",timestamp)
url = f"https://api.openweathermap.org/data/2.5/onecall/timemachine?lat={LAT}&lon={LON}&dt={timestamp}&appid=17cb6e99c3d0250f8a996d4f68e2bafa&units=metric"
print(url)
try:

async with session.get(url) as response:
data = await response.json()
print(data)
if "current" in data:
temp = data["current"]["temp"]
temperatures.append((past_date.strftime("%Y-%m-%d"), temp))
else:
temperatures.append((past_date.strftime("%Y-%m-%d"), "Retry-gotu"))
except Exception as e:
print(f"Error fetching data: {e}")
temperatures.append((past_date.strftime("%Y-%m-%d"), "Retry-gotu"))
return temperatures

def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
temperatures = loop.run_until_complete(self.get_past_temperatures())
self.data_fetched.emit(temperatures)

class WeatherApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Past 5 Days Temperature")
self.setGeometry(100, 100, 600, 400)

# Create layout
layout = QVBoxLayout()

# Title Label
title_label = QLabel("Temperature for Last 5 Days")
title_label.setAlignment(Qt.AlignCenter)
title_label.setStyleSheet("font-size: 18px; font-weight: bold;")
layout.addWidget(title_label)

# Create Table
self.table = QTableWidget(0, 2) # 2 columns (Date and Temperature)
self.table.setHorizontalHeaderLabels(["Date", "Temperature (°C)"])
layout.addWidget(self.table)

# Fetch Data Button
fetch_button = QPushButton("Fetch Data")
fetch_button.clicked.connect(self.fetch_data)
layout.addWidget(fetch_button)

# Set layout
self.setLayout(layout)

# Create WorkerThread for async data fetching
self.worker_thread = WorkerThread()
self.worker_thread.data_fetched.connect(self.update_table)

def fetch_data(self):
self.worker_thread.start() # Start the worker thread

def update_table(self, temperatures):
self.table.setRowCount(len(temperatures)) # Set the number of rows
for row, (date, temp) in enumerate(temperatures):
self.table.setItem(row, 0, QTableWidgetItem(date))
self.table.setItem(row, 1, QTableWidgetItem(str(temp)))

if __name__ == "__main__":
app = QApplication(sys.argv)
weather_app = WeatherApp()
weather_app.show()
sys.exit(app.exec_())