-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (92 loc) · 3.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const axios = require('axios');
const cheerio = require('cheerio');
const ttBase = "https://www.tickertape.in/stocks?filter=";
const alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
const ALLOW_DELAY = false;
const DELAY_TIME = 10;
const MIN_RATING = 90;
const MAX_RATING = 1000;
const ALLOW_DEBUG = false
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const FINAL_ARRAY = []
async function fetchWebsiteData(url, page) {
if (ALLOW_DEBUG) {
// console.log("fetchWebsiteData", url)
}
// make http call to url
let response = await axios(url).catch(() => {
// console.log("Error occurred while fetching data", url, page);
});
if (response?.status !== 200) {
return;
}
response.page = page
return response;
}
async function fetchTTScore(url) {
if (ALLOW_DEBUG) {
console.log("fetchTTScore:", url)
}
if (ALLOW_DELAY) {
await delay(DELAY_TIME);
}
const res = await fetchWebsiteData(url)
// console.log("fetchTTScore", res?.data)
if (res?.data) {
const html = res.data;
const $ = cheerio.load(html);
const stock = $(".security-name").text();
const scoreText = $('.percBuyReco-value')
const score = $(scoreText).text().replace("%", '');
// console.log("stock:" + stock)
// console.log("score:" + score)
// console.log("MIN_RATING:" + MIN_RATING, score >= MIN_RATING)
// console.log("MAX_RATING:" + MAX_RATING, score < MAX_RATING)
if (!isNaN(score) & score > 10) {
// console.log("score:" + score)
if (score >= MIN_RATING && score < MAX_RATING) {
console.log(url + "," + stock + "," + score)
FINAL_ARRAY.push(url + "," + stock + "," + score)
}
// else {
// console.log(url + "," + stock + "," + score)
// }
}
}
}
async function fetchStocks(url) {
if (ALLOW_DEBUG) {
console.log("fetchStocks:", url)
}
if (ALLOW_DELAY) {
await delay(DELAY_TIME);
}
const res = await fetchWebsiteData(url)
// console.log("fetchStocks", res?.data)
// fetchTTScore("https://www.tickertape.in/stocks/adani-ports-and-special-economic-zone-APSE")
// fetchTTScore("https://www.tickertape.in/stocks/zen-technologies-ZETE")
if (res?.data) {
const html = res.data;
const $ = cheerio.load(html);
const stocksList = $('.index-page a')
// fetchTTScore(stocksList[0].attribs.href)
for (let i = 0; i < stocksList.length; i++) {
// console.log("stocksList: ", stocksList[index].attribs.href)
await fetchTTScore("https://www.tickertape.in" + stocksList[i].attribs.href)
};
}
}
async function start() {
// fetchStocks(ttBase + 'z')
for (let i = 0; i < alphabets.length; i++) {
// console.log(val);
// const href = alphabeticalList?.[index].attribs.href
// console.log("alphabeticalList: ", alphabeticalList?.[index].attribs.href)
// console.log("alphabets[i]", alphabets[i]);
await fetchStocks(ttBase + alphabets[i])
};
console.log("FINAL_ARRAY", FINAL_ARRAY)
}
start()