Random1231/Postgres
0
1from fastapi import FastAPI2import requests3from datetime import datetime, timedelta4from io import BytesIO5import pandas as pd6from zipfile import ZipFile7 8header = {9 "Connection": "keep-alive",10 "Cache-Control": "max-age=0",11 "DNT": "1",12 "Upgrade-Insecure-Requests": "1",13 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "14 "Chrome/111.0.0.0 Safari/537.36",15 "Sec-Fetch-User": "?1", "Accept": "*/*", "Sec-Fetch-Site": "none", "Sec-Fetch-Mode": "navigate",16 "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-US,en;q=0.9,hi;q=0.8"17 }18 19 20app = FastAPI()21 22@app.get("/")23def read_root():24 return {"Hello": "World!"}25 26# def nse_urlfetch(url):27# r_session = requests.session()28# nse_live = r_session.get("http://nseindia.com", headers=header)29# return r_session.get(url, headers=header)30 31 32@app.get("/test1")33def read_root():34 Date = "12-07-2024"35 COMMON_REQUEST_HEADERS = {36 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.45"37 }38 try:39 dateObj = datetime.strptime(Date, "%d-%m-%Y")40 except ValueError:41 return {"Error": "Invalid date format. Please use dd-mm-yyyy"}42 r_session = requests.session()43 nse_live = r_session.get("http://nseindia.com", headers=header)44 FileDate = dateObj.strftime("%Y%m%d")45 FileName = f"BhavCopy_NSE_CM_0_0_0_{FileDate}_F_0000.csv"46 zipFileName = f"{FileName}.zip"47 r = r_session.get(f"https://nsearchives.nseindia.com/content/cm/{zipFileName}", headers=header )48 if r.status_code == 200:49 try:50 # Create in-memory file from response51 zip_data = BytesIO(r.content)52 with ZipFile(zip_data) as zip_file:53 # Check if there's only one file54 if len(zip_file.namelist()) == 1 and zip_file.namelist()[0] == FileName:55 df = pd.read_csv(zip_file.open(FileName))56 return df.to_json() 57 else:58 return {59 'Error': "Error finding csv inside zip or converting zip to json",60 } 61 except Exception as e :62 return { 'Error': f'Error occurred while processing data. Error message :{str(e)}'}63 else: 64 return {65 'Status': r.status_code, 66 'Error': f'Server Failed To Respond or No Data on the Given Date : {Date}',67 } 68 69@app.get("/test")70def read_root():71 COMMON_REQUEST_HEADERS = {72 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.45"73 }74 Date = "12-07-2024"75 try:76 dateObj = datetime.strptime(Date, "%d-%m-%Y")77 except ValueError:78 return {"Error": "Invalid date format. Please use dd-mm-yyyy"}79 FileDate = dateObj.strftime("%Y%m%d")80 FileName = f"BhavCopy_BSE_CM_0_0_0_{FileDate}_F_0000.csv"81 82 r = requests.get(83 f"https://www.bseindia.com/download/BhavCopy/Equity/{FileName}",84 headers=COMMON_REQUEST_HEADERS,85 )86 if r.status_code == 200:87 if r.content:88 csv_data = BytesIO(r.content)89 df = pd.read_csv(csv_data)90 df_json = df.to_json()91 return df_json92 else:93 return {94 'Error': "Error finding csv in server response",95 } 96 else: 97 return {98 'Status': r.status_code, 99 'Error': f'Server Failed To Respond or No Data on the Given Date : {Date}',100 } 