· 4 years ago · Aug 24, 2021, 06:40 AM
1// ==UserScript==
2// @name Nepse Data To API
3// @namespace http://tampermonkey.net/
4// @version 0.1
5// @description try to take over the world!
6// @author Kiran
7// @match https://newweb.nepalstock.com/*
8// @icon https://www.google.com/s2/favicons?domain=burgeon.com.np
9// @grant none
10// ==/UserScript==
11
12//scrapper api authorization token keep it safe, is a vurnable if exposed outside
13const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYwYWVhMDBmZjRiMzI1NWE0NDBkYzE3ZSIsImVtYWlsIjoic2NyYXBAZ21haWwuY29tIiwibmFtZSI6IktpcmFuIE5ldXBhbmUiLCJyb2xlIjpbIlNDUkFQUEVSIl0sImlhdCI6MTYyMjA1NzA5MjIwM30._6yhwVNXiF0owHkJbI6VBnY23NjnhwovBoJ1Al0clvM';
14
15// Utility functions
16const sendData = async (url, data) =>{
17 const res = await fetch(url, {
18 method: 'POST',
19 headers: new Headers({
20 'Content-Type': 'application/json',
21 'Authorization': 'Bearer ' + token,
22 }),
23 body: JSON.stringify(data)
24 })
25 const res2 = await res.json();
26 console.log(url, res2);
27}
28const fetchUrl= async (url)=>{
29 const res = await fetch(url);
30 return res.json();
31}
32
33//Main function
34const scrapeAndSendToApi = async () => {
35 try{
36 const [sector, nepse_index, market_open, top_gainer, top_looser, top_turnover, top_volume] = await Promise.all([
37 fetchUrl("https://newweb.nepalstock.com/api/nots"),
38 fetchUrl("https://newweb.nepalstock.com/api/nots/nepse-index"),
39 fetchUrl("https://newweb.nepalstock.com/api/nots/nepse-data/market-open"),
40 fetchUrl("https://newweb.nepalstock.com/api/nots/top-ten/top-gainer?all=true"),
41 fetchUrl("https://newweb.nepalstock.com/api/nots/top-ten/top-loser?all=true"),
42 fetchUrl("https://newweb.nepalstock.com/api/nots/top-ten/turnover?all=false"),
43 fetchUrl("https://newweb.nepalstock.com/api/nots/top-ten/trade?all=false"),
44 ]);
45 // https://api.buggged.com/puzi/nepse_dump
46 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "sector_wise_data", value: sector});
47 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "nepse_index", value: nepse_index});
48 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "market_open", value: market_open});
49 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "top_gainer", value: top_gainer});
50 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "top_looser", value: top_looser});
51 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "top_turnover", value: top_turnover});
52 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "top_volume", value: top_volume});
53
54 // live market ko api banda hune raxa after 3PM so separate last ma scrape garne
55 // aaru lai effect na garos vanera
56 const live_market = await fetchUrl("https://newweb.nepalstock.com/api/nots/lives-market");
57 sendData("https://api.buggged.com/puzi/nepse_dump", {key: "live_market", value: live_market});
58 }catch(err){
59 console.error("Error while scrapping", err);
60 }
61}
62
63(function() {
64 'use strict';
65 scrapeAndSendToApi();
66 setInterval(()=>{
67 scrapeAndSendToApi();
68 }, 60000);
69})();