remwharead/src/main.cpp

128 lines
3.0 KiB
C++
Raw Normal View History

2019-05-11 02:52:33 +02:00
/* This file is part of remwharead.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2019-05-12 00:02:31 +02:00
#include <iostream>
#include <string>
#include <chrono>
2019-05-16 08:37:50 +02:00
#include <fstream>
#include <memory>
2019-05-12 20:19:00 +02:00
#include "sqlite.hpp"
2019-05-12 00:02:31 +02:00
#include "parse_options.hpp"
2019-05-16 08:36:35 +02:00
#include "uri.hpp"
2019-05-16 08:37:50 +02:00
#include "export.hpp"
2019-05-19 09:42:52 +02:00
#include "search.hpp"
2019-05-12 00:02:31 +02:00
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::chrono::system_clock;
2019-05-12 00:02:31 +02:00
2019-05-12 20:19:00 +02:00
int main(const int argc, const char *argv[])
2019-05-11 02:52:33 +02:00
{
2019-05-12 00:02:31 +02:00
options opts = parse_options(argc, argv);
2019-05-12 20:19:00 +02:00
if (opts.status_code != 0)
{
return opts.status_code;
}
2019-05-12 00:02:31 +02:00
2019-05-14 20:45:48 +02:00
Database db;
if (!db)
{
cerr << "Error: Database connection failed.\n";
return 2;
}
2019-05-16 08:36:35 +02:00
if (!opts.uri.empty())
{
2019-05-16 08:36:35 +02:00
URI uri(opts.uri);
html_extract page = uri.get();
2019-05-18 13:51:41 +02:00
if (page.fulltext.empty())
{
2019-05-21 11:59:58 +02:00
cerr << "Error: Could not fetch page.\n";
2019-05-18 13:51:41 +02:00
return 4;
}
2019-05-22 09:43:58 +02:00
string archive_uri;
if (opts.archive)
{
archive_uri = uri.archive();
}
db.store({opts.uri, archive_uri, system_clock::now(), opts.tags,
2019-05-16 00:05:18 +02:00
page.title, page.description, page.fulltext});
}
2019-05-16 08:37:50 +02:00
std::ofstream file;
if (!opts.file.empty())
{
file.open(opts.file);
if (!file.good())
{
cerr << "Error: Could not open file: " << opts.file << endl;
return 3;
}
}
2019-05-19 09:42:52 +02:00
if (opts.format != export_format::undefined)
2019-05-16 00:05:18 +02:00
{
2019-05-19 09:42:52 +02:00
vector<Database::entry> entries =
db.retrieve(opts.span[0], opts.span[1]);
if (!opts.search_tags.empty())
2019-05-16 08:37:50 +02:00
{
2019-05-19 09:42:52 +02:00
entries = search_tags(entries, opts.search_tags);
2019-05-16 08:37:50 +02:00
}
else if (!opts.search_all.empty())
{
entries = search_all(entries, opts.search_all);
}
2019-05-19 09:42:52 +02:00
switch (opts.format)
{
case export_format::csv:
2019-05-16 08:37:50 +02:00
{
2019-05-19 09:42:52 +02:00
if (file.is_open())
{
export_csv(entries, file);
file.close();
}
else
{
export_csv(entries);
}
break;
2019-05-16 08:37:50 +02:00
}
2019-05-19 09:42:52 +02:00
case export_format::asciidoc:
2019-05-16 08:37:50 +02:00
{
2019-05-19 09:42:52 +02:00
if (file.is_open())
{
export_adoc(entries, file);
file.close();
}
else
{
export_adoc(entries);
}
break;
2019-05-16 08:37:50 +02:00
}
2019-05-19 09:42:52 +02:00
default:
2019-05-16 08:37:50 +02:00
{
2019-05-19 09:42:52 +02:00
break;
}
2019-05-16 08:37:50 +02:00
}
}
2019-05-14 20:45:48 +02:00
2019-05-11 02:52:33 +02:00
return 0;
}