Update app.py
Browse files
app.py
CHANGED
|
@@ -95,15 +95,17 @@ from pymongo import MongoClient
|
|
| 95 |
import matplotlib.pyplot as plt
|
| 96 |
import os
|
| 97 |
import ssl
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
# Fetch the secret key from environment variables
|
| 102 |
Mongo_ip = os.getenv("Mongo_IP")
|
| 103 |
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# Connect to MongoDB
|
| 106 |
-
# client = MongoClient()
|
| 107 |
client = MongoClient(
|
| 108 |
Mongo_ip,
|
| 109 |
ssl=True,
|
|
@@ -111,8 +113,6 @@ client = MongoClient(
|
|
| 111 |
)
|
| 112 |
db = client.GoldRates
|
| 113 |
collection = db['GoldRates']
|
| 114 |
-
db = client.GoldRates
|
| 115 |
-
collection = db['GoldRates']
|
| 116 |
|
| 117 |
# Backend functions
|
| 118 |
def jina(url):
|
|
@@ -152,25 +152,24 @@ def insert_data_if_not_exists(city, date, value_24k, value_22k, value_18k):
|
|
| 152 |
"GoldRate_18k": float(value_18k.replace(',', ''))
|
| 153 |
}
|
| 154 |
collection.insert_one(document)
|
| 155 |
-
# st.success(f"Gold rates for {city} on {date} have been saved to MongoDB.")
|
| 156 |
|
| 157 |
# Function to fetch weekly data for chart
|
| 158 |
def fetch_weekly_data(city):
|
| 159 |
-
today = datetime.datetime.
|
| 160 |
start_date = today - datetime.timedelta(days=7)
|
| 161 |
query = {"Place": city, "Date": {"$gte": start_date.strftime("%Y-%m-%d")}}
|
| 162 |
return list(collection.find(query).sort("Date", -1))
|
| 163 |
|
| 164 |
-
# Function to check if it's the first run of the day after 12:30 PM
|
| 165 |
def is_first_run_after_1230():
|
| 166 |
-
today = datetime.datetime.
|
| 167 |
time_check = today.replace(hour=12, minute=30, second=0, microsecond=0)
|
| 168 |
date_check = today.strftime("%Y-%m-%d")
|
| 169 |
return today >= time_check and not collection.find_one({"Date": date_check})
|
| 170 |
|
| 171 |
# Fetch and save rates for all cities
|
| 172 |
def fetch_and_save_all_cities():
|
| 173 |
-
date_today = datetime.datetime.
|
| 174 |
for city in cities:
|
| 175 |
city_url = f"https://www.goodreturns.in/gold-rates/{city}.html"
|
| 176 |
try:
|
|
@@ -206,7 +205,7 @@ if st.button("Generate Gold Rates"):
|
|
| 206 |
|
| 207 |
# Fetch and display gold rates for the selected city
|
| 208 |
if selected_city:
|
| 209 |
-
date_today = datetime.datetime.
|
| 210 |
city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
|
| 211 |
|
| 212 |
try:
|
|
|
|
| 95 |
import matplotlib.pyplot as plt
|
| 96 |
import os
|
| 97 |
import ssl
|
| 98 |
+
import pytz # Importing pytz for timezone handling
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
# Fetch the secret key from environment variables
|
| 103 |
Mongo_ip = os.getenv("Mongo_IP")
|
| 104 |
|
| 105 |
+
# Setting up IST timezone
|
| 106 |
+
ist_timezone = pytz.timezone("Asia/Kolkata")
|
| 107 |
|
| 108 |
# Connect to MongoDB
|
|
|
|
| 109 |
client = MongoClient(
|
| 110 |
Mongo_ip,
|
| 111 |
ssl=True,
|
|
|
|
| 113 |
)
|
| 114 |
db = client.GoldRates
|
| 115 |
collection = db['GoldRates']
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Backend functions
|
| 118 |
def jina(url):
|
|
|
|
| 152 |
"GoldRate_18k": float(value_18k.replace(',', ''))
|
| 153 |
}
|
| 154 |
collection.insert_one(document)
|
|
|
|
| 155 |
|
| 156 |
# Function to fetch weekly data for chart
|
| 157 |
def fetch_weekly_data(city):
|
| 158 |
+
today = datetime.datetime.now(ist_timezone)
|
| 159 |
start_date = today - datetime.timedelta(days=7)
|
| 160 |
query = {"Place": city, "Date": {"$gte": start_date.strftime("%Y-%m-%d")}}
|
| 161 |
return list(collection.find(query).sort("Date", -1))
|
| 162 |
|
| 163 |
+
# Function to check if it's the first run of the day after 12:30 PM IST
|
| 164 |
def is_first_run_after_1230():
|
| 165 |
+
today = datetime.datetime.now(ist_timezone)
|
| 166 |
time_check = today.replace(hour=12, minute=30, second=0, microsecond=0)
|
| 167 |
date_check = today.strftime("%Y-%m-%d")
|
| 168 |
return today >= time_check and not collection.find_one({"Date": date_check})
|
| 169 |
|
| 170 |
# Fetch and save rates for all cities
|
| 171 |
def fetch_and_save_all_cities():
|
| 172 |
+
date_today = datetime.datetime.now(ist_timezone).strftime("%Y-%m-%d")
|
| 173 |
for city in cities:
|
| 174 |
city_url = f"https://www.goodreturns.in/gold-rates/{city}.html"
|
| 175 |
try:
|
|
|
|
| 205 |
|
| 206 |
# Fetch and display gold rates for the selected city
|
| 207 |
if selected_city:
|
| 208 |
+
date_today = datetime.datetime.now(ist_timezone).strftime("%Y-%m-%d")
|
| 209 |
city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html"
|
| 210 |
|
| 211 |
try:
|