|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import streamlit as st |
|
import requests |
|
import pandas as pd |
|
import pymongo |
|
import datetime |
|
from pymongo import MongoClient |
|
import matplotlib.pyplot as plt |
|
import os |
|
|
|
|
|
|
|
|
|
Mongo_ip = os.getenv("Mongo_IP") |
|
|
|
|
|
|
|
client = MongoClient(Mongo_ip) |
|
db = client.GoldRates |
|
collection = db['GoldRates'] |
|
|
|
|
|
def jina(url): |
|
base_url = "https://r.jina.ai/" |
|
url = base_url + url |
|
response = requests.get(url) |
|
return response.text |
|
|
|
def price_cities(url): |
|
text = jina(url) |
|
pos1 = text.find('**') |
|
new = text[:pos1] |
|
|
|
twentytwok = new[int(new.find('22K')):int(new.find('24K'))] |
|
value_22k = twentytwok[int(twentytwok.find('\n\n') + 1): int(twentytwok.find('\n\n+'))][3:] |
|
value_22k = value_22k.split('\n')[0] |
|
|
|
twentyfourk = new[int(new.find('24K')):int(new.find('18K'))] |
|
value_24k = twentyfourk[int(twentyfourk.find('\n\n') + 1): int(twentyfourk.find('\n\n+'))][3:] |
|
value_24k = value_24k.split('\n')[0] |
|
|
|
eighteenk = new[int(new.find('18K')):] |
|
value_18k = eighteenk[int(eighteenk.find('\n\n') + 1): int(eighteenk.find('\n\n+'))][3:] |
|
value_18k = value_18k.split('\n')[0] |
|
|
|
return value_24k, value_22k, value_18k |
|
|
|
|
|
def insert_data_if_not_exists(city, date, value_24k, value_22k, value_18k): |
|
query = {"Date": date, "Place": city} |
|
if not collection.find_one(query): |
|
document = { |
|
"Date": date, |
|
"Place": city, |
|
"GoldRate_24k": float(value_24k.replace(',', '')), |
|
"GoldRate_22k": float(value_22k.replace(',', '')), |
|
"GoldRate_18k": float(value_18k.replace(',', '')) |
|
} |
|
collection.insert_one(document) |
|
|
|
|
|
|
|
def fetch_weekly_data(city): |
|
today = datetime.datetime.today() |
|
start_date = today - datetime.timedelta(days=7) |
|
query = {"Place": city, "Date": {"$gte": start_date.strftime("%Y-%m-%d")}} |
|
return list(collection.find(query).sort("Date", -1)) |
|
|
|
|
|
def is_first_run_after_1230(): |
|
today = datetime.datetime.today() |
|
time_check = today.replace(hour=12, minute=30, second=0, microsecond=0) |
|
date_check = today.strftime("%Y-%m-%d") |
|
return today >= time_check and not collection.find_one({"Date": date_check}) |
|
|
|
|
|
def fetch_and_save_all_cities(): |
|
date_today = datetime.datetime.today().strftime("%Y-%m-%d") |
|
for city in cities: |
|
city_url = f"https://www.goodreturns.in/gold-rates/{city}.html" |
|
try: |
|
value_24k, value_22k, value_18k = price_cities(city_url) |
|
insert_data_if_not_exists(city, date_today, value_24k, value_22k, value_18k) |
|
except Exception as e: |
|
st.error(f"Could not fetch the gold rates for {city}. {e}") |
|
|
|
|
|
cities = ['Hyderabad', 'Ahmedabad', 'Ayodhya', 'Bangalore', 'Bhubaneswar', 'Chandigarh', 'Chennai', |
|
'Coimbatore', 'Delhi', 'Jaipur', 'Kerala', 'Kolkata', 'Lucknow', |
|
'Madurai', 'Mangalore', 'Mumbai', 'Mysore', 'Nagpur', 'Nashik', 'Patna', |
|
'Pune', 'Rajkot', 'Salem', 'Surat', 'Trichy', 'Vadodara', 'Vijayawada', 'Visakhapatnam'] |
|
|
|
|
|
st.title('Gold Rates in Indian Cities') |
|
st.subheader('Select a city to view the current gold rates and a weekly trend.') |
|
st.sidebar.title("About the Project") |
|
st.sidebar.write("This project fetches current gold rates for 24K, 22K, and 18K gold from GoodReturns for 28 Indian states. The rates for 1g, 8g, and 10g are displayed.") |
|
st.sidebar.write("**Developed by:**") |
|
st.sidebar.write("[Srish Rachamalla](https://www.linkedin.com/in/srishrachamalla/)") |
|
st.sidebar.write("[Sai Teja Pallerla](https://www.linkedin.com/in/saiteja-pallerla-668734225/)") |
|
|
|
|
|
selected_city = st.selectbox('Select a City', cities) |
|
|
|
|
|
if st.button("Generate Gold Rates"): |
|
|
|
if is_first_run_after_1230(): |
|
fetch_and_save_all_cities() |
|
st.success("Gold rates for all cities have been fetched and saved.") |
|
|
|
|
|
if selected_city: |
|
date_today = datetime.datetime.today().strftime("%Y-%m-%d") |
|
city_url = f"https://www.goodreturns.in/gold-rates/{selected_city}.html" |
|
|
|
try: |
|
value_24k, value_22k, value_18k = price_cities(city_url) |
|
insert_data_if_not_exists(selected_city, date_today, value_24k, value_22k, value_18k) |
|
|
|
|
|
current_data = { |
|
'Gold Purity': ['24K', '22K', '18K'], |
|
'1g Price (₹)': [float(value_24k.replace(',', '')), float(value_22k.replace(',', '')), float(value_18k.replace(',', ''))], |
|
'8g Price (₹)': [float(value_24k.replace(',', '')) * 8, float(value_22k.replace(',', '')) * 8, float(value_18k.replace(',', '')) * 8], |
|
'10g Price (₹)': [float(value_24k.replace(',', '')) * 10, float(value_22k.replace(',', '')) * 10, float(value_18k.replace(',', '')) * 10] |
|
} |
|
|
|
|
|
df = pd.DataFrame(current_data) |
|
st.write(f"Gold rates in {selected_city} as of {date_today}:") |
|
st.dataframe(df.style.format(precision=2).set_properties(**{ |
|
'background-color': 'black', |
|
'color': 'white', |
|
'border-color': 'gray' |
|
})) |
|
|
|
|
|
weekly_data = fetch_weekly_data(selected_city) |
|
if weekly_data: |
|
dates = [doc["Date"] for doc in weekly_data] |
|
rates_24k = [doc["GoldRate_24k"] for doc in weekly_data] |
|
rates_22k = [doc["GoldRate_22k"] for doc in weekly_data] |
|
rates_18k = [doc["GoldRate_18k"] for doc in weekly_data] |
|
|
|
|
|
plt.figure(figsize=(10, 5)) |
|
plt.plot(dates, rates_24k, label="24K Gold", color="gold", marker='o') |
|
plt.plot(dates, rates_22k, label="22K Gold", color="red", marker='o') |
|
plt.plot(dates, rates_18k, label="18K Gold", color="brown", marker='o') |
|
plt.title(f"Gold Rates Trend in {selected_city} (Past Week)") |
|
plt.xlabel("Date") |
|
plt.ylabel("Price (₹)") |
|
plt.legend() |
|
plt.xticks(rotation=45) |
|
st.pyplot(plt) |
|
|
|
except Exception as e: |
|
st.error(f"Could not fetch the gold rates. Please try again. {e}") |
|
|
|
|
|
st.markdown("<br><hr><center><p style='color: grey;'>© 2024 All Rights Reserved</p></center><br>", unsafe_allow_html=True) |
|
|
|
|