import streamlit as st import feedparser import requests from bs4 import BeautifulSoup from newspaper import Article import time import json import datetime import hashlib import os # --- Page Config --- st.set_page_config( page_title="భారత్ పల్స్ (BharatPulse) - వార్తలు", page_icon="📰", layout="wide", initial_sidebar_state="expanded" ) # --- User Auth Utilities --- USERS_FILE = "users.json" def hash_password(password): return hashlib.sha256(password.encode()).hexdigest() def load_users(): if os.path.exists(USERS_FILE): with open(USERS_FILE, "r") as f: return json.load(f) return {} def save_users(users): with open(USERS_FILE, "w") as f: json.dump(users, f, indent=2) def login_user(username, password): users = load_users() if username in users and users[username]["password"] == hash_password(password): return users[username] return None def register_user(username, email, password, location=""): users = load_users() if username in users: return False, "User already exists" users[username] = { "email": email, "password": hash_password(password), "location": location, "created": datetime.datetime.now().strftime("%Y-%m-%d") } save_users(users) return True, "Registration successful" # --- Login/Register Page --- if "logged_in" not in st.session_state: st.session_state.logged_in = False st.session_state.username = "" def show_login_page(): st.title("🔐 లాగిన్ (Login)") st.write("దయచేసి మీ ఖాతా వివరాలను నమోదు చేయండి.") username = st.text_input("వినియోగదారు పేరు (Username)") password = st.text_input("పాస్‌వర్డ్ (Password)", type="password") if st.button("లాగిన్ చేయండి (Login)"): user = login_user(username, password) if user: st.session_state.logged_in = True st.session_state.username = username st.session_state.user_email = user.get("email", "") st.session_state.user_location = user.get("location", "") st.session_state.account_creation_date = user.get("created", "N/A") st.success("లాగిన్ విజయవంతమైంది!") st.rerun() else: st.error("చెల్లని లాగిన్ వివరాలు. దయచేసి మళ్లీ ప్రయత్నించండి.") st.info("మీకు ఖాతా లేనట్లయితే, దయచేసి క్రింద నమోదు చేసుకోండి.") with st.expander("👉 ఖాతా రిజిస్ట్రేషన్ (Register New Account)"): new_username = st.text_input("కొత్త వినియోగదారు పేరు", key="register_username") new_email = st.text_input("ఇమెయిల్", key="register_email") new_password = st.text_input("పాస్‌వర్డ్", type="password", key="register_password") new_location = st.text_input("స్థానం (ఐచ్ఛికం)", key="register_location") if st.button("రిజిస్టర్ (Register)"): success, message = register_user(new_username, new_email, new_password, new_location) if success: st.success(message + " ఇప్పుడు లాగిన్ చేయండి.") else: st.error(message) # If not logged in, show login page and stop app if not st.session_state.logged_in: show_login_page() st.stop() # --- LOGOUT Button --- st.sidebar.title("భారత్ పల్స్ - సెట్టింగ్‌లు") if st.sidebar.button("లాగ్ అవుట్ (Logout)"): st.session_state.logged_in = False st.session_state.username = "" st.session_state.user_email = "" st.session_state.user_location = "" st.session_state.account_creation_date = "N/A" st.rerun() # Continue with full BharatPulse app... # (Insert your BharatPulse main app code here – you already provided it) # ✅ Tip: Use st.session_state.username, user_email, etc., in Profile tab