I want to find Biggest Changerate in the prices of coins - javascript

i want to find biggests changerate in the prices in my coins data
like if SDT oldprice is 1.00 and
new price is 1.02 = 2%
and if it was the biggests changerate in the coins my script should printing it
but the script dont working
it only keep give me same coin
const math = require('mathjs');
const fetch = require('node-fetch');
get()
async function get() {
const response = await fetch("https://trade.kucoin.com/_api/currency/prices?base=USD&targets=&lang=en_US");
const coin1 = await response.json();
const olddata = coin1.data
const tokens = Object.keys(olddata)
const oldprice = Object.values(olddata)
get1()
async function get1() {
const response = await fetch("https://trade.kucoin.com/_api/currency/prices?base=USD&targets=&lang=en_US");
const coin2 = await response.json();
const newdata = coin2.data
const tokens = Object.keys(newdata)
const newprice = Object.values(newdata)
function findLargestDifference() {
var large = null;
var index = 0;
for (var i = 0; i < oldprice.length; i++) {
var change = tokens[i].newprice / oldprice[i].oldprice;
if (change > large) {
large = change;
index = i;
}
}
console.log(tokens[index])
return tokens[index];
}
findLargestDifference()
}
}
here how data looks https://prnt.sc/19syjjg

Related

Puppeteer: Save data from a for loop in database

I'm web-scraping a site and managed to extract data in the for loop.
However, I don't know how can I save it to my MongoDB database as I'm receiving an error ReferenceError: nameElement is not defined .
How can I save the results from my for loop as an object to my database?
const kclResults = [];
async function scrapeInfiniteScrollItems(
page,
scrollDelay = 10000
) {
try {
const html = await page.content();
const $ = cheerio.load(html);
await page.evaluate(() => {
let elements = $("[role='listitem']")
.find("._2DX0iPG8PDF3Si_o5PlzIj")
.toArray();
for (i = 0; i < elements.length; i++) {
$(elements[i]).click();
const nameElement = $("[data-log-name='PersonName']").text();
const emailElement = $("[data-log-name='Email']").text();
const allElements = $("[aria-label='Contact information']").text();
const officeLocation = $("[data-log-name='OfficeLocation']").text();
const position = $("[data-log-name='Company']").text();
const jobTitle = $("[data-log-name='JobTitle']").text();
const departament = $("[data-log-name='Department']").text();
console.log(
`email: ${emailElement} name: ${nameElement} allElements: ${allElements} \n office location: ${officeLocation} \n position: ${position} \n jobTitle: ${jobTitle} \n departament: ${departament}`
);
}
});
let kclResult = new KingsDB({
nameElement,
emailElement,
allElements,
officeLocation,
position,
jobTitle,
departament,
});
kclResults.push(kclResult);
console.log(kclResults);
kclResult.save();
return kclResults;
} catch (error) {
console.log(error);
}
}
You are declaring nameElement (and other variables) in for loop scope and trying to access it outside that scope.
Just create an array of "elements" and iterate over it when you're writing it to your DB. This code below should work:
const kclResults = [];
async function scrapeInfiniteScrollItems(
page,
scrollDelay = 10000
) {
try {
const html = await page.content();
const $ = cheerio.load(html);
const resultArr = await page.evaluate(() => {
let elements = $("[role='listitem']")
.find("._2DX0iPG8PDF3Si_o5PlzIj")
.toArray();
const resultArr = [];
for (i = 0; i < elements.length; i++) {
$(elements[i]).click();
const nameElement = $("[data-log-name='PersonName']").text();
const emailElement = $("[data-log-name='Email']").text();
const allElements = $("[aria-label='Contact information']").text();
const officeLocation = $("[data-log-name='OfficeLocation']").text();
const position = $("[data-log-name='Company']").text();
const jobTitle = $("[data-log-name='JobTitle']").text();
const departament = $("[data-log-name='Department']").text();
resultArr.push({
nameElement,
emailElement,
allElements,
officeLocation,
position,
jobTitle,
departament
});
console.log(
`email: ${emailElement} name: ${nameElement} allElements: ${allElements} \n office location: ${officeLocation} \n position: ${position} \n jobTitle: ${jobTitle} \n departament: ${departament}`
);
}
return resultArr;
});
const kclResults = [];
for (let result of resultArr) {
const {
nameElement,
emailElement,
allElements,
officeLocation,
position,
jobTitle,
departament
} = result;
let kclResult = new KingsDB({
nameElement,
emailElement,
allElements,
officeLocation,
position,
jobTitle,
departament,
});
kclResults.push(kclResult);
console.log(kclResults);
kclResults.push(kclResult.save());
}
return kclResults;
} catch (error) {
console.log(error);
}
}
PS: The function passed to pageEvaluate runs in browser context and thus doesn't have access to your node variables, until they are explicitly passed as an argument.

Async await on rendering component react

the js code below performs the loading of a series of fields remotely and then displays them on a map, the problem is that it calls it does not wait for the await and I proceed so the data is not displayed on the map which is redirected by the component , how can I do to make the chaima wait for the response of the function before proceeding to render the map
import {getmarcaturegiornaliere} from "module.js";
MyComponent.js
class Am4chartMap extends Component {
async componentDidMount() {
let map = am4core.create("map", am4maps.MapChart);
//Load values from response this function->
var marcature=await getmarcaturegiornaliere();
map.geodata = am4geodata_usaHigh;
map.percentHeight = 90;
map.dy = 10;
let polygonSeries = map.series.push(new am4maps.MapPolygonSeries());
polygonSeries.useGeodata = true;
map.homeZoomLevel = 1.2;
map.zoomControl = new am4maps.ZoomControl();
map.zoomControl.layout = 'horizontal';
map.zoomControl.align = 'left';
map.zoomControl.valign = 'bottom';
map.zoomControl.dy = -10;
map.zoomControl.contentHeight = 20;
map.zoomControl.minusButton.background.fill = am4core.color("#C7D0FF");
map.zoomControl.minusButton.background.stroke = am4core.color("#6979C9");
map.zoomControl.minusButton.label.fontWeight = 600;
map.zoomControl.minusButton.label.fontSize = 22;
map.zoomControl.minusButton.scale = .75;
map.zoomControl.minusButton.label.scale = .75;
map.zoomControl.plusButton.background.fill = am4core.color("#C7D0FF");
map.zoomControl.plusButton.background.stroke = am4core.color("#6979C9");
map.zoomControl.plusButton.label.fontWeight = 600;
map.zoomControl.plusButton.label.fontSize = 22;
map.zoomControl.plusButton.label.align = "center";
map.zoomControl.plusButton.scale = .75;
map.zoomControl.plusButton.label.scale = .75;
map.zoomControl.plusButton.dx = 5;
let plusButtonHoverState = map.zoomControl.plusButton.background.states.create("hover");
plusButtonHoverState.properties.fill = am4core.color("#354D84");
let minusButtonHoverState = map.zoomControl.minusButton.background.states.create("hover");
minusButtonHoverState.properties.fill = am4core.color("#354D84");
let polygonTemplate = polygonSeries.mapPolygons.template;
polygonTemplate.tooltipText = "{name}";
polygonTemplate.fill = am4core.color("#474D84");
polygonTemplate.stroke = am4core.color("#6979C9")
let hs = polygonTemplate.states.create("hover");
hs.properties.fill = am4core.color("#354D84");
let citySeries = map.series.push(new am4maps.MapImageSeries());
citySeries.data = marcature;
citySeries.dataFields.value = "size";
let city = citySeries.mapImages.template;
city.nonScaling = true;
city.propertyFields.latitude = "latitude";
city.propertyFields.longitude = "longitude";
let circle = city.createChild(am4core.Circle);
circle.fill = am4core.color("#C7D0FF");
circle.strokeWidth = 0;
let circleHoverState = circle.states.create("hover");
circleHoverState.properties.strokeWidth = 1;
circle.tooltipText = '{tooltip}';
circle.propertyFields.radius = 'size';
this.map = map;
}
componentWillUnmount() {
if(this.map) {
this.map.dispose();
}
}
render() {,.
module.js
//Setup server
import server from "./settings";
//Rest call axios module
const axios = require('axios').default;
export function getmarcaturegiornaliere() {
var date="03-02-2021";
console.log("Data: "+date);
var listmarcature=[];
axios.post(server.url+'/Utente/CaricamentoStoricoGiornonaliero', {
Data: date,
IdUtente: '3',
CalcoloOreGiornaliere: true
})
.then(function (response) {
console.log("Response: "+response.data);
//Vado ad iterare la response
let data = response.data;
for (let index in data){
console.log("Stato: "+data[index].Longitudine);
var datatemp={
"latitude" : data[index].Latitudine,
"longitude" : data[index].Longitudine,
"size" : 5,
"tooltip" : data[index].Stato,
};
listmarcature.push(datatemp);
}
}).catch(function (error) {
console.log("Errore: "+error);
});
return listmarcature;
}
Your function getmarcaturegiornaliere is not async, and this is the problem.
It will always return you an empty array [], because the return listmarcature; is executed before the .then() is. Therefore you return an empty array, and somewhere in the future the .then() will happen (and it will do nothing because you already returned).
The solution would be: Make it async.
There are some syntaxes available for that, I will bring you the best for this case:
Put async in the funciton: export async function getmarcaturegiornaliere() {
Add await before the axios call: await axios.post(...)

Prevent duplicate E-mail Google Spreadsheets script

i'm currently able to send E-mail with Google Spreadsheets script. But my trigger and if condition didn't prevent E-mail sending as i wish :
Here is my code :
'''
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("MASTER");
const h3 = 'SPP Proyek JIS Tanggal xx dari xxx';
const headers = ws.getRange("A2:M2").getValues();
const item = headers[0][4];
const spec = headers[0][5];
const sat = headers[0][6];
const qty = headers[0][7];
const price = headers[0][8];
const total = headers[0][9];
const tujuan = headers[0][10];
const lr = ws.getLastRow();
const tableRangeValues = ws.getRange(3, 5,lr-2,7).getDisplayValues();
const trigger = ws.getRange(3, 1,lr-2).getValues();
const statusEmail = ws.getRange(3, 13,lr-2).getValues();
const htmlTemplate = HtmlService.createTemplateFromFile("Email");
htmlTemplate.h3 = h3;
htmlTemplate.headers = headers;
htmlTemplate.item = item;
htmlTemplate.spec = spec;
htmlTemplate.sat = sat;
htmlTemplate.qty = qty;
htmlTemplate.price = price;
htmlTemplate.total = total;
htmlTemplate.tujuan = tujuan;
htmlTemplate.tableRangeValues = tableRangeValues;
htmlTemplate.trigger = trigger;
htmlTemplate.statusEmail = statusEmail;
const htmlForEmail = htmlTemplate.evaluate().getContent();
if ((trigger != 'FALSE') && (statusEmail != 'EMAIL_SENT')); {
GmailApp.sendEmail(
"sistem.jis#gmail.com",
"Approval SPP Komersial",
"HTML Support",
{ htmlBody: htmlForEmail }
);
ws.getRange(3, 13,lr-2).setValue('EMAIL_SENT');
}
'''
and this is my sample file link :
https://docs.google.com/spreadsheets/d/13TKIhY7HmK3o-j98q45XXb2nwZzfYwyYn7EULhY_RJw/edit#gid=1216091331
it seems i have problem with the trigger and if condition code which i don't understand
Thank you!
Defining const trigger = ws.getRange(3, 1,lr-2).getValues(); returns you an array of type [[FALSE], [true], [FALSE], [true]]
To make your code work you need to define a loop that iterates through each row (and trigger) invidually
Also, remove the ; from if ((trigger != 'FALSE') && (statusEmail != 'EMAIL_SENT')); {
Sample:
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("MASTER");
const h3 = 'SPP Proyek JIS Tanggal xx dari xxx';
const headers = ws.getRange("A2:M2").getValues();
const item = headers[0][4];
const spec = headers[0][5];
const sat = headers[0][6];
const qty = headers[0][7];
const price = headers[0][8];
const total = headers[0][9];
const tujuan = headers[0][10];
const lr = ws.getLastRow();
const tableRangeValues = ws.getRange(3, 5,lr-2,7).getDisplayValues();
var data = ws.getRange(3, 1,lr-2,13).getDisplayValues();
for (var i = 0; i < data.length; i++){
const trigger = data[i][0];
const statusEmail = data[i][12];
const htmlTemplate = HtmlService.createTemplateFromFile("Email");
htmlTemplate.h3 = h3;
htmlTemplate.headers = headers;
htmlTemplate.item = item;
htmlTemplate.spec = spec;
htmlTemplate.sat = sat;
htmlTemplate.qty = qty;
htmlTemplate.price = price;
htmlTemplate.total = total;
htmlTemplate.tujuan = tujuan;
htmlTemplate.tableRangeValues = tableRangeValues;
htmlTemplate.trigger = trigger;
htmlTemplate.statusEmail = statusEmail;
const htmlForEmail = htmlTemplate.evaluate().getContent();
Logger.log(trigger);
if ((trigger != 'FALSE') && (statusEmail != 'EMAIL_SENT')) {
GmailApp.sendEmail(
"sistem.jis#gmail.com",
"Approval SPP Komersial",
"HTML Support",
{ htmlBody: htmlForEmail }
);
ws.getRange(3, 13,lr-2).setValue('EMAIL_SENT');
}
}
}
Note:
In this sample I did not modify tableRangeValues since those values are processed later on on your client-side. Depending on what you want them to be like, you might also want to iterate throguh tem.

Scraping IMDb episodes using Cheerio.js - only first page of TV episodes is returned

Working on scraping TV episodes from IMDb (Breaking Bad in the example below). The problem is when implementing the for loop, only the first iteration of j is returned.
My assumption is the return statement is exiting the loop but I'm unsure how to fix the problem.
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const searchUrl = 'https://www.imdb.com/find?s=tt&ttype=tv&ref_=fn_tv&q=';
const movieUrl = 'https://www.imdb.com/title/';
async function getEpisodes(searchTerm) {
//const imdbID = await getID(searchTerm);
//const numSeasons = await getSeasons(imdbID);
const imdbID = 'tt0903747';
const numSeasons = 5;
const episodes = [];
for (let j = 1; j <= numSeasons; j++) {
return fetch(`${movieUrl}${imdbID}/episodes?season=${j}`)
.then(response => response.text())
.then(body => {
const $ = cheerio.load(body);
$('div[itemProp="episodes"]').each(function (i, element) {
const airdate = $(element).find('.airdate').text().trim();
const episodeTitle = $(element).find('a[itemProp="name"]').text().trim();
const votes = $(element).find('.ipl-rating-star__total-votes').text().trim().match(/\(([^)]+)\)/)[1];
const rating = $(element).find('.ipl-rating-star ').find('.ipl-rating-star__rating').text().trim().slice(0, 3);
episode = {
season: j,
episodeTitle,
airdate,
votes,
rating
};
episodes.push(episode);
});
return episodes; //Only season 1 is returned.
});
}
}
Let's rewrite the function using async await style. This way we make sure we fire fetch numSeasons times, await all of them, and process them one by one.
async function processResponse(response, season) {
const body = await response.text();
const $ = cheerio.load(body);
let episodes = [];
$('div[itemProp="episodes"]').each(function (i, element) {
const airdate = $(element).find('.airdate').text().trim();
const episodeTitle = $(element).find('a[itemProp="name"]').text().trim();
const votes = $(element).find('.ipl-rating-star__total-votes').text().trim().match(/\(([^)]+)\)/)[1];
const rating = $(element).find('.ipl-rating-star ').find('.ipl-rating-star__rating').text().trim().slice(0, 3);
episode = {
season,
episodeTitle,
airdate,
votes,
rating
};
episodes.push(episode);
});
return episodes;
}
async function getEpisodes(searchTerm) {
//const imdbID = await getID(searchTerm);
//const numSeasons = await getSeasons(imdbID);
const imdbID = 'tt0903747';
const numSeasons = 5;
let promises = [];
for (let j = 1; j <= numSeasons; j++) {
promises.push(fetch(`${movieUrl}${imdbID}/episodes?season=${j}`));
}
const responses = await Promise.all(promises);
return responses.reduce((accumulator, response, index) => {
return accumulator.concat(await processResponse(response, index + 1));
}, []);
}

I would like to analyze the image with tensorflw.js - nodejs

mobilenet.js
var loadFrozenModel = require('#tensorflow/tfjs-converter');
var NamedTensorMap = require('#tensorflow/tfjs-converter');
var tfc = require('#tensorflow/tfjs-core');
var IMAGENET_CLASSES = require('./imagenet_classes');
const GOOGLE_CLOUD_STORAGE_DIR = 'https://storage.googleapis.com/tfjs-models/savedmodel/';
const MODEL_FILE_URL = 'mobilenet_v1_1.0_224/optimized_model.pb';
const WEIGHT_MANIFEST_FILE_URL = 'mobilenet_v1_1.0_224/weights_manifest.json';
const INPUT_NODE_NAME = 'input';
const OUTPUT_NODE_NAME = 'MobilenetV1/Predictions/Reshape_1';
const PREPROCESS_DIVISOR = tfc.scalar(255 / 2);
class MobileNet {
constructor() {}
async load() {
this.model = await loadFrozenModel(
GOOGLE_CLOUD_STORAGE_DIR + MODEL_FILE_URL,
GOOGLE_CLOUD_STORAGE_DIR + WEIGHT_MANIFEST_FILE_URL);
}
dispose() {
if (this.model) {
this.model.dispose();
}
}
predict(input) {
const preprocessedInput = tfc.div(
tfc.sub(input.asType('float32'), PREPROCESS_DIVISOR),
PREPROCESS_DIVISOR);
const reshapedInput =
preprocessedInput.reshape([1, ...preprocessedInput.shape]);
const dict = {};
dict[INPUT_NODE_NAME] = reshapedInput;
return this.model.execute(dict, OUTPUT_NODE_NAME);
}
getTopKClasses(predictions, topK) {
const values = predictions.dataSync();
predictions.dispose();
let predictionList = [];
for (let i = 0; i < values.length; i++) {
predictionList.push({value: values[i], index: i});
}
predictionList = predictionList
.sort((a, b) => {
return b.value - a.value;
})
.slice(0, topK);
return predictionList.map(x => {
return {label: IMAGENET_CLASSES[x.index], value: x.value};
});
}
}
module.exports = MobileNet;
test.js
var tfc = require('#tensorflow/tfjs-core');
var MobileNet = require('./mobilenet');
var fs = require('fs');
var image = require('get-image-data')
var i = 0;
var meta;
image('./cat.jpg', function(err, getImageData){
if(err) throw err;
console.log('start to image data ');
console.log(i++);
console.log("meta : " + getImageData.data.length);
console.log("getImageData :"+getImageData);
const mobileNet = new MobileNet();
console.time('Loading of model');
// await mobileNet.load();
console.timeEnd('Loading of model');
console.log("maybee this is error on the data type");
const pixels = tfc.fromPixels(image);
console.time('First prediction');
let result = mobileNet.predict(pixels);
const topK = mobileNet.getTopKClasses(result, 5);
console.timeEnd('First prediction');
resultElement.innerText = '';
topK.forEach(x => {
resultElement.innerText += `${x.value.toFixed(3)}: ${x.label}\n`;
});
console.time('Subsequent predictions');
result = mobileNet.predict(pixels);
mobileNet.getTopKClasses(result, 5);
console.timeEnd('Subsequent predictions');
mobileNet.dispose();
});
I want to analyze the image using the tensorflow.js.
But it doesn't work.
ReferenceError: ImageData is not defined
at MathBackendCPU.fromPixels (/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/#tensorflow/tfjs-core/dist/kernels/backend_cpu.js:75:31)
at Engine.fromPixels (/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/#tensorflow/tfjs-core/dist/engine.js:292:29)
at ArrayOps.fromPixels (/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/#tensorflow/tfjs-core/dist/ops/array_ops.js:195:41)
at /Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/#tensorflow/tfjs-core/dist/ops/operation.js:11:61
at Object.Tracking.tidy (/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/#tensorflow/tfjs-core/dist/tracking.js:36:22)
at Object.descriptor.value [as fromPixels] (/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/#tensorflow/tfjs-core/dist/ops/operation.js:11:26)
at /Users/leeyongmin/Documents/tfjs-converter-master-2/demo/test.js:26:22
at /Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/get-image-data/index.js:18:7
at load (/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/get-image/server.js:18:5)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

Categories

Resources