From 69b580bb4e79462776fd2897f723abc2d4ac7803 Mon Sep 17 00:00:00 2001 From: tripathyr Date: Tue, 22 Oct 2024 09:01:51 +0530 Subject: [PATCH] Update price-history.min.js --- routes/price-history.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/price-history.min.js b/routes/price-history.min.js index 216226f..2ec6943 100644 --- a/routes/price-history.min.js +++ b/routes/price-history.min.js @@ -1 +1 @@ -const express=require("express"),router=express.Router(),cron=require("node-cron"),PriceHistory=require("../models/price-history");function loadHistoricToDb(){const now=parseInt(Date.now()/1e3);Promise.all([fetch(`https://query1.finance.yahoo.com/v7/finance/download/BTC-USD?period1=1410912000&period2=${now}&interval=1d&events=history&includeAdjustedClose=true`).then((res=>res.text())),fetch(`https://query1.finance.yahoo.com/v7/finance/download/BTC-INR?period1=1410912000&period2=${now}&interval=1d&events=history&includeAdjustedClose=true`).then((res=>res.text()))]).then((async([usd,inr])=>{const usdData=usd.split("\n").slice(1),inrData=inr.split("\n").slice(1),priceHistoryData=[];for(let i=0;i{console.log(err)}))}loadHistoricToDb(),router.get("/",(async(req,res)=>{console.log("price-history");try{let{from:from,to:to,on:on,limit:limit=100,asset:asset="btc",currency:currency,sort:sort,dates:dates}=req.query;const searchParams={asset:asset};if(from&&to&&(from=new Date(from).getTime(),to=new Date(to).getTime(),from>to)){const temp=from;from=to,to=temp}if(from&&(searchParams.date={$gte:new Date(from).getTime()}),to&&(searchParams.date={...searchParams.date,$lte:new Date(to).getTime()}),dates){const datesArray=dates.split(",");searchParams.date={$in:datesArray.map((date=>new Date(date).getTime()))}}if(on&&(searchParams.date={$eq:new Date(on).getTime()}),currency&&(searchParams[currency]={$exists:!0}),sort){if(!["asc","desc","ascending","descending","1","-1"].includes(sort))return res.status(400).json({error:"Invalid sort. Valid values are asc | desc | ascending | descending | 1 | -1"});sort={date:"asc"===sort||"ascending"===sort||"1"===sort?1:-1}}else sort={date:-1};const dataFormat={_id:0,__v:0,asset:0};"inr"===currency&&(dataFormat.usd=0),"usd"===currency&&(dataFormat.inr=0);const priceHistory=await PriceHistory.find(searchParams,dataFormat).sort(sort).limit("all"===limit?0:parseInt(limit)).lean();res.json(priceHistory)}catch(err){console.log(err),res.status(500).json({error:err})}})),cron.schedule("0 */4 * * *",(async()=>{try{const[usd,inr]=await Promise.all([fetch("https://query1.finance.yahoo.com/v7/finance/download/BTC-USD").then((res=>res.text())),fetch("https://query1.finance.yahoo.com/v7/finance/download/BTC-INR").then((res=>res.text()))]),usdData=usd.split("\n").slice(1),inrData=inr.split("\n").slice(1);for(let i=0;i{const t=[];fs.createReadStream(CSV_FILE_PATH).pipe(csv()).on('data',e=>t.push(e)).on('end',()=>e(t)).on('error',e=>r(e))})}async function loadHistoricToDb(){const e=parseInt(Date.now()/1e3);try{const[r,t]=await Promise.all([fetch(`https://query1.finance.yahoo.com/v7/finance/download/BTC-USD?period1=1410912000&period2=${e}&interval=1d&events=history&includeAdjustedClose=true`).then(e=>e.text()),fetch(`https://query1.finance.yahoo.com/v7/finance/download/BTC-INR?period1=1410912000&period2=${e}&interval=1d&events=history&includeAdjustedClose=true`).then(e=>e.text())]);const a=r.split("").slice(1),i=t.split("").slice(1),n=[];for(let e=0;e({updateOne:{filter:{date:new Date(e.date).getTime(),asset:"btc"},update:{$set:{usd:parseFloat(e.usd),inr:parseFloat(e.inr)}},upsert:!0}}));await PriceHistory.bulkWrite(r),console.log("Data upserted successfully from CSV.")}catch(e){console.error("Error reading CSV file:",e)}}}loadHistoricToDb(),router.get("/",async(e,r)=>{console.log("price-history");try{let{from:t,to:a,on:i,limit:n=100,asset:l="btc",currency:o,sort:c,dates:s}=e.query;const p={asset:l};if(t&&a){if(t=new Date(t).getTime(),a=new Date(a).getTime(),t>a){const e=t;t=a,a=e}}t&&(p.date={$gte:new Date(t).getTime()}),a&&(p.date={...p.date,$lte:new Date(a).getTime()}),s&&(p.date={$in:s.split(",").map(e=>new Date(e).getTime())}),i&&(p.date={$eq:new Date(i).getTime()}),o&&(p[o]={$exists:!0}),c=["asc","desc","ascending","descending","1","-1"].includes(c)?{date:"asc"===c||"ascending"===c||"1"===c?1:-1}:{date:-1};const u={_id:0,__v:0,asset:0};"inr"===o&&(u.usd=0),"usd"===o&&(u.inr=0);const d=await PriceHistory.find(p,u).sort(c).limit("all"===n?0:parseInt(n)).lean();r.json(d)}catch(e){console.log(e),r.status(500).json({error:e})}}),cron.schedule("0 */4 * * *",async()=>{await loadHistoricToDb()}),module.exports=router;