Terminal weather query based on Nodejs

Source: Internet
Author: User

international practice, first on

The day before yesterday, suddenly thought, how directly in the command line query the weather? Well, then write one. Then began to look for the city, the weather API interface, finally made such a thing.

Installation method: $ NPM Install Tianqi-g

Source: Https://github.com/liuhongqiang/tianqi

OneWeather Interface

Search the Internet, the first ready to use the Chinese Weather Network data interface, but need to register, also line, register it.

Registration is good, and the information is incomplete, OK, fill in the name, ID card ...

Finally, also upload ID information, see this I do not want to use it, and then found that the use of the interface to fill the server IP.

Suddenly, the heart of 100,000 that what whistling and past ...

Then think of Baidu's product has an API store, search the next, sure enough to find the right interface.

City interface

Http://apistore.baidu.com/microservice/cityinfo?cityname= City Name

{"Errnum": 0, "retmsg": "Success", "Retdata": {"CityName": "\u5317\u4eac", "Provincename": "\u5317\u4eac", "Citycode": " 101010100 "," ZipCode ":" 100000 "," Telareacode ":" 010 "}}

Weather interface

Http://apis.baidu.com/apistore/weatherservice/recentweathers?cityid= City Code

The request needs to add the Apikey field in the header, which can be obtained in the Baidu weather interface.

TwoCode Writing

1. Bin/tianqi Weather Entry Code

#!/usr/bin/env Noderequire ("colorful"). Toxic (); var package = require (".. /package ") if (!process.argv[2]) {  console.log (' Tianqi ~ ' + package.version.green);  Console.log (Package.description.grey);  Console.log ("  $". Cyan + "Tianqi Beijing");  Console.log ("  $". Cyan + "Tianqi Shanghai");  return;} Console.log (); var Tianqi = require ('.. '); Tianqi (Process.argv.slice (2). Join ('));

The first line, set the file will be node executed.

If there are no parameters, the output tianqi version information and the use example.

If there are arguments, the index.js function is called.

2.index.js main function

var request = require ("request"); var config = require ("./lib/config"); var    Print = require ("./lib/print"); module.exports = function (word) {word = encodeURIComponent (word); Request.get (config.city + Word, function (error, response, body) {if (!error && response.statuscode = 200            ) {var cityresult = Json.parse (body); if (Cityresult.errnum = = 0) {var options = {Url:config.weather + cityresult.retdata.c                Itycode, headers: {"Apikey": Config.apikey}                }; Request.get (Options, function (error, response, body) {if (!error && response.statuscode = = 20                    0) {Print.print (Json.parse (body));            }                });            } else {console.log (cityResult.retMsg.red); }        }    });};

First, the city interface is requested to obtain the code of the user input city.

Then request the weather interface to get the weather data.

Finally, call print.jsand output the weather information.

3.print.js Output Information

Exports.print = function (data) {    if (Data.errnum = = 0) {        var today = Data.retData.today;        var todaystr = "Today's weather" + Today.type.magenta;        Todaystr + = "  Current temperature" + Today.curTemp.green;        Todaystr + = "  minimum temperature" + Today.lowtemp.blue;        Todaystr + = "  maximum temperature" + today.hightemp.red;        Todaystr + = "  wind" + today.fengli.yellow;        Console.log (TODAYSTR);        Console.log ();        var forecast = Data.retData.forecast;        Console.log ("forecast for the next four days ~". Cyan);        Console.log ();        Forecast.foreach (function (data) {            var str = "" + data.date + "" + Data.type.magenta;            str + = "Minimum temperature" + Data.lowtemp.blue;            str + = "  maximum temperature" + data.hightemp.red;            str + = "  wind" + data.fengli.yellow;            Console.log (str);            Console.log ();        });    } else {        console.log (data.errMsg.red);    }};

This is relatively simple, it is not explained.

Iii. Summary

This tianqi query software is nodejs a simple practice of your own.

The knowledge points used include HTTP requests, parsing of JSON, and the release of NPM packages .

Overall speaking, it is relatively simple, share to everyone.

Source published in Github:https://github.com/liuhongqiang/tianqi

Always welcome everyone to mention issue , or initiate pull request to improve it, thank you.

Terminal weather query based on Nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.