Create a gist now

Instantly share code, notes, and snippets.

[C++] アニメワショーイから今期の作品一覧を取得 ref: http://sakuhindb.com/pj/6_B4C9CDFDBFCDA4B5A4F3/20100218.html
#include <fstream>
#include <istream>
#include <ostream>
#include <boost/regex.hpp>
#include <boost/asio.hpp>
#include "iconv_wrapper.hpp"
using namespace std;
using namespace boost;
using namespace boost::asio::ip;
#define HOST "www.tsundere.com"
#define PORT "http"
#define PATH "/newprog/2015-10-ts.html"
#define HTML_PATH "./2015Q4.html"
int main()
{
try {
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(HOST, PORT);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end) {
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error) {
throw boost::system::system_error(error);
}
boost::asio::streambuf request;
ostream request_stream(&request);
request_stream << "GET " << PATH << " HTTP/1.0\r\n";
request_stream << "Host: " << HOST << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
boost::asio::write(socket, request);
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
istream response_stream(&response);
string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
string status_message;
getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
cout << "Invalid response\n";
return -1;
}
if (status_code != 200) {
cout << "Response returned with status code " << status_code << "\n";
return -1;
}
boost::asio::read_until(socket, response, "\r\n\r\n");
string header;
while (getline(response_stream, header) && header != "\r") {
/*cout << header << "\n";*/
}
/*cout << "\n";*/
ofstream ofs(HTML_PATH);
if (response.size() > 0) {
ofs << &response;
}
while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) {
ofs << &response;
}
if (error != boost::asio::error::eof) {
throw boost::system::system_error(error);
}
ifstream ifs(HTML_PATH);
string getStr;
if (ifs.fail()) {
return -1;
}
string tmp_str;
IconvWrapper iconv;
while (getline(ifs, getStr)) {
getStr = iconv.from_to(getStr, "euc-jp", "utf-8");
boost::regex serch_reg("<a .*?target.*?>(.*?)</a>");
boost::regex replace_reg("<a .*?target.*?>|</a>");
boost::smatch m;
string match_str;
string replace_str;
if (boost::regex_search(getStr, m, serch_reg)) {
match_str = m.str();
replace_str = boost::regex_replace(match_str, replace_reg, "");
}
if ((replace_str == "") || (tmp_str == replace_str)) {
continue;
}
else {
cout << replace_str << "\n";
tmp_str = replace_str;
}
}
if (remove(HTML_PATH) != 0) {
cout << "Error deleting file\n";
}
}
catch (std::exception& e) {
cout << "Exception: " << e.what() << "\n";
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment