用到的函数、API等
1、中央气象台API返回的JSON数据(http://m.weather.com.cn/data/101010100.html)
2、外网获取IP(http://ip.dnsexit.com/index.php)
3、Sqlite(1中的城市代号利用sqlite3实现查询)
4、C++中GB2312字符串和UTF-8之间的转换(见这篇文章http://blog.csdn.net/lgh1992314/article/details/8579206)
5、Jsoncpp(主要是处理1中的数据==见这篇文章http://blog.csdn.net/lgh1992314/article/details/8582179)
...
程序依赖于数据库中的城市以及城市代码的丰富度,所以有的城市可能会查询不到哦。
半成品:
- #include <iostream>
- #include <ctime>
- #include <string>
- #include <cstring>
- #include <cstdlib>
- #include <sstream>
- #include <fstream>
- #include <sqlite3.h>
- #include <afxinet.h>
- #include <windows.h>
- #include<json.h>
- #pragma comment(lib,"sqlite3.lib")
- using namespace Json;
- using namespace std;
- std::string City_code;
- int sqlite3_exec_callback(void *data, int n_columns, char **col_values, char **col_names)
- {
- //int i;
- /*for(i=0; i < n_columns; i++)
- {
- cout << col_values[i] << "\t";
- }
- cout << endl;*/
- City_code = col_values[0];
- return 0;
- }
- std::string UtfToGb(std::string utf8)//UTF-8 to GB2312
- {
- int len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
- wchar_t* wstr = new wchar_t[len+1];
- memset(wstr, 0, len+1);
- MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, wstr, len);
- len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
- char* str = new char[len+1];
- memset(str, 0, len+1);
- WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
- if(wstr) delete[] wstr;
- std::string strs = (str);
- delete []str;
- return strs;
- }
- void jsonDate(string strValue) //Jsoncpp
- {
- //Json::StyledWriter style_write;
- Json::Value value;
- Json::Reader reader;
- reader.parse(strValue, value);
- //std::cout << style_write.write(value) << std::endl;
- std::cout << value["weatherinfo"]["date_y"].asString() << " ";
- std::cout << value["weatherinfo"]["week"].asString() << std::endl;
- std::cout << "================================================" << std::endl;
- std::cout << "= " << value["weatherinfo"]["temp1"].asString() << " " << " "
- << value["weatherinfo"]["weather1"].asString() << value["weatherinfo"]["wind1"].asString() << std::endl;
- std::cout << "= " << value["weatherinfo"]["temp2"].asString() << " "
- << value["weatherinfo"]["weather2"].asString() << " " << value["weatherinfo"]["wind2"].asString() << std::endl;
- std::cout << "= " << value["weatherinfo"]["temp3"].asString() << " "
- << value["weatherinfo"]["weather3"].asString() << " " << value["weatherinfo"]["wind3"].asString() << std::endl;
- std::cout << "= " << value["weatherinfo"]["temp4"].asString() << " "
- << value["weatherinfo"]["weather4"].asString() << " " << value["weatherinfo"]["wind4"].asString() << std::endl;
- std::cout << "= " << value["weatherinfo"]["temp5"].asString() << " "
- << value["weatherinfo"]["weather5"].asString() << " " << value["weatherinfo"]["wind5"].asString() << std::endl;
- std::cout << "= " << value["weatherinfo"]["temp6"].asString() << " "
- << value["weatherinfo"]["weather6"].asString() << " " << value["weatherinfo"]["wind6"].asString() << std::endl;
- std::cout << "================================================" << std::endl;
- }
- std::string GetIp()
- {
- CString url = "http://ip.dnsexit.com/";
- CString content;
- CString data;
- DWORD dwStatusCode;
- CInternetSession session("HttpClient");
- CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
- pfile -> QueryInfoStatusCode(dwStatusCode);
- if(dwStatusCode == HTTP_STATUS_OK)
- {
- while (pfile -> ReadString(data))
- {
- content += data;
- }
- }
- pfile -> Close();
- delete pfile;
- session.Close();
- return std::string(content);
- }
- std::string GetProvince(std::string ip)
- {
- std::string url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=";
- url.append(ip);
- CString content;
- CString data;
- DWORD dwStatusCode;
- CInternetSession session("HttpClient");
- CHttpFile* pfile = (CHttpFile *)session.OpenURL(url.c_str());
- pfile -> QueryInfoStatusCode(dwStatusCode);
- if(dwStatusCode == HTTP_STATUS_OK)
- {
- while (pfile -> ReadString(data))
- {
- content += data;
- }
- }
- pfile -> Close();
- delete pfile;
- session.Close();
- std::string result = std::string(content);
- int left = result.find("<location>");
- int right = result.find("</location>");
- //cout << left << "\t" << right << endl;
- std::string province = result.substr(left + 10,right - left - 10 - 5);
- return province;
- }
- std::string GetWeather(std::string city_code)
- {
- std::string url = "http://m.weather.com.cn/data/";
- url.append(city_code);
- url.append(".html");
- CString content;
- CString data;
- DWORD dwStatusCode;
- CInternetSession session("HttpClient");
- CHttpFile* pfile = (CHttpFile *)session.OpenURL(url.c_str());
- pfile -> QueryInfoStatusCode(dwStatusCode);
- if(dwStatusCode == HTTP_STATUS_OK)
- {
- while (pfile -> ReadString(data))
- {
- content += data;
- }
- }
- pfile -> Close();
- delete pfile;
- session.Close();
- return UtfToGb(std::string(content));
- }
- void ShowWeather()
- {
- sqlite3 *db = NULL;
- char *err_msg = NULL;
- if(SQLITE_OK != sqlite3_open("weather.db",&db))
- {
- std::cout << "can't open the database." << std::endl;
- exit(-1);
- }
- std::string province;
- std::string ip = GetIp();
- std::cout << "IP:" << ip << std::endl;
- province = GetProvince(ip);
- std::cout << "您所在的地方:" << province << std::endl;
- std::cout << "您所在地天气:" << std::endl;
- std::string city = province.substr(province.find("省") + 2,4);
- std::string sql = "select city_code from weather where city = ";
- sql.append("\"");
- sql.append(city);
- sql.append("\"");
- sqlite3_exec(db, sql.c_str(), &sqlite3_exec_callback, 0, &err_msg);
- string strValue = GetWeather(City_code);
- jsonDate(strValue);
- City_code.clear();
- sqlite3_close(db);
- }
- int main()
- {
- ShowWeather();
- sqlite3 *db = NULL;
- char *err_msg = NULL;
- if(SQLITE_OK != sqlite3_open("weather.db",&db))
- {
- std::cout << "can't open the database." << std::endl;
- exit(-1);
- }
- std::string province;
- std::string sql = "select city_code from weather where city = ";
- std::cout << "please input the province" << std::endl;
- std::cin >> province;
- sql.append("\"");
- sql.append(province);
- sql.append("\"");
- std::cout << "天气:" << std::endl;
- sqlite3_exec(db, sql.c_str(), &sqlite3_exec_callback, 0, &err_msg);
- string strValue = GetWeather(City_code);
- jsonDate(strValue);
- sqlite3_close(db);
- return 0;
- }
输出:
- IP:124.135.247.252
- 您所在的地方:山东省聊城市
- 您所在地天气:
- 2013年2月16日 星期六
- ================================================
- = 7℃~0℃ 多云转雨夹雪南风3-4级转小于3级
- = 4℃~-3℃ 阴转小雪 南风小于3级转北风4-5级
- = 1℃~-7℃ 阴转多云 北风3-4级转小于3级
- = 3℃~-4℃ 晴 南风小于3级
- = 5℃~-4℃ 晴 北风小于3级
- = 7℃~-2℃ 晴 南风小于3级
- ================================================
- please input the province
- 北京
- 天气:
- 2013年2月16日 星期六
- ================================================
- = 2℃~-3℃ 阴转多云微风
- = 6℃~-4℃ 多云转晴 北风3-4级
- = 3℃~-8℃ 晴 北风3-4级转微风
- = 3℃~-6℃ 晴 微风
- = 4℃~-6℃ 晴转多云 微风
- = 4℃~-5℃ 多云转晴 微风
- ================================================
- 请按任意键继续. . .