Jump to content

Applied Programming/Internet Data/JavaScript

From Wikiversity

internet.js

[edit | edit source]
/* This program demonstrates webpage, XML, and JSON processing.

Input:
    None

Output:
    XML and JSON sample data.

References:
    https://www.mediawiki.org/wiki/Manual:Parameters_to_Special:Export
    https://www.twilio.com/blog/2017/08/http-requests-in-node-js.html
    https://nodejs.dev/learn/make-an-http-post-request-using-nodejs
    https://www.npmjs.com/package/xml-js

*/

const https = require("https");
const xmljs = require("xml-js");

if (typeof module != "undefined" && !module.parent) {
    main();
}

/**
 * Runs main program logic.
 */
async function main() {
    try {
        let url = "https://www.w3schools.com/xml/note.xml";
        let text = await getWebpage(url);
        displayUrl(url, text);
        displayXmltree(text)

        url = "https://en.wikiversity.org/wiki/Special:Export";
        parameters = {"pages":"Applied Programming", "limit":"1"};
        text = await postWebpage(url, parameters);
        displayUrl(url, text);

        url = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikiversity/all-access/user/" +
            "Applied_Programming%2fInternet_Data" +
            "/daily/2018030100/2018033100";
        text = await getWebpage(url);
        displayUrl(url, text);
        displayWikistats(text);
    } catch (error) {
        console.error(error)
    }
}

/**
 * Gets a webpage.
 * 
 * @param {string} url to retrieve
 * @returns {string} webpage text
 */
async function getWebpage(url) {
    return new Promise(function(resolve, reject) {
        let request = https.get(url, (response) => {
            let data = "";

            response.on("data", (chunk) => {
                data += chunk;
            });

            response.on("end", () => {
                resolve(data);
            });

        }).on("error", (error) => {
            reject(error);
        });

        request.end();
    });
}

/**
 * Posts to a webpage.
 * 
 * @param {string} url to retrieve
 * @param {object} parameters to post
 * @returns {string} webpage response
 */
async function postWebpage(url, parameters) {
    return new Promise(function(resolve, reject) {
        parameters["method"] = "POST";
        let request = https.request(url, parameters, (response) => {
            let data = "";

            response.on("data", (chunk) => {
                data += chunk;
            });

            response.on("end", () => {
                resolve(data);
            });

        }).on("error", (error) => {
            reject(error);
        });

        request.end();
    });
}

/**
 * Displays a url and text.
 * 
 * @param {string} url to display
 * @param {string} text to display
 */
function displayUrl(url, text) {
    console.log(url);
    console.log(text);
    console.log("");
}

/**
 * Displays an XML tree.
 * 
 * @param {string} text to display
 */
function displayXmltree(text) {
    text = text.replace(/<\?xml.+?>\s?/, "");
    const json = xmljs.xml2json(text, {compact: true, spaces: 4});
    console.log(json);
}

/**
 * Displays timestamp and page views for Wikimedia REST API statistics in JSON format.
 * 
 * @param {string} text retrieved from /wikimedia.org/api/rest_v1/metrics
 */
function displayWikistats(text) {
    let object = JSON.parse(text);
    for (item of object.items) {
        console.log(`${item["timestamp"]}: ${item["views"]}`);
    }
    console.log("");
}

Try It

[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own JavaScript compiler / interpreter / IDE.

See Also

[edit | edit source]