remwharead/src/cli/main.cpp

258 lines
6.1 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/>.
*/
#include "remwharead_cli.hpp"
2019-07-26 07:12:50 +02:00
#include "export/adoc.hpp"
#include "export/bookmarks.hpp"
#include "export/csv.hpp"
2019-09-03 17:47:39 +02:00
#include "export/json.hpp"
#include "export/link.hpp"
2019-11-28 09:03:52 +01:00
#include "export/rofi.hpp"
2019-09-06 00:53:58 +02:00
#include "export/rss.hpp"
#include "export/simple.hpp"
2019-05-19 09:42:52 +02:00
#include "search.hpp"
#include "sqlite.hpp"
#include "types.hpp"
#include "uri.hpp"
#include <chrono>
#include <fstream>
#include <iostream>
#include <list>
#include <locale>
#include <string>
#include <thread>
2019-05-12 00:02:31 +02:00
2019-07-27 09:59:43 +02:00
using namespace remwharead;
using namespace remwharead_cli;
2019-05-12 00:02:31 +02:00
using std::cerr;
using std::endl;
using std::string;
using std::chrono::system_clock;
using std::ofstream;
using std::list;
2019-05-12 00:02:31 +02:00
int App::main(const std::vector<std::string> &args)
2019-05-11 02:52:33 +02:00
{
2019-05-22 12:25:06 +02:00
std::locale::global(std::locale("")); // Set locale globally.
if (_exit_requested)
2019-05-12 20:19:00 +02:00
{
return 0;
}
2019-10-28 03:04:25 +01:00
if (_argument_error)
{
2019-10-28 03:04:25 +01:00
return 1;
}
if (!args.empty())
{
_uri = args[0];
}
if (_uri.empty() && _format == export_format::undefined)
{
cerr << "Error: You have to specify either an URI or --export.\n";
return 1;
2019-05-12 20:19:00 +02:00
}
2019-05-12 00:02:31 +02:00
2019-05-14 20:45:48 +02:00
Database db;
if (!db)
{
cerr << "Error: Database could not be opened.\n";
2019-08-09 13:01:54 +02:00
return 2;
}
if (!_uri.empty())
{
URI uri(_uri);
archive_answer archive_data;
std::thread thread_archive;
if (_archive)
{
2020-10-31 13:01:53 +01:00
// clang-format off
thread_archive = std::thread([&archive_data, &uri]
{
archive_data = uri.archive();
});
2020-10-31 13:01:53 +01:00
// clang-format on
}
2019-05-16 08:36:35 +02:00
html_extract page = uri.get();
2019-09-23 19:28:19 +02:00
if (_archive)
2019-05-22 09:43:58 +02:00
{
thread_archive.join();
if (!archive_data)
{
cerr << "Error archiving URL: " << archive_data.error << endl;
}
2019-05-22 09:43:58 +02:00
}
if (!page)
{
cerr << "Error: Could not fetch page.\n";
cerr << page.error << endl;
return 3;
}
db.store({_uri, archive_data.uri, system_clock::now(), _tags,
page.title, page.description, page.fulltext});
2019-05-16 00:05:18 +02:00
}
ofstream file;
if (!_file.empty())
2019-05-16 08:37:50 +02:00
{
file.open(_file);
2019-05-16 08:37:50 +02:00
if (!file.good())
{
cerr << "Error: Could not open file: " << _file << endl;
2019-08-09 13:01:54 +02:00
return 2;
2019-05-16 08:37:50 +02:00
}
}
if (_format != export_format::undefined)
2019-05-16 00:05:18 +02:00
{
list<Database::entry> entries = db.retrieve(_timespan[0], _timespan[1]);
if (!_search_tags.empty())
2019-05-16 08:37:50 +02:00
{
2019-08-08 15:36:04 +02:00
Search search(entries);
entries = search.search_tags(_search_tags, _regex);
2019-05-16 08:37:50 +02:00
}
else if (!_search_all.empty())
{
2019-08-09 00:14:26 +02:00
Search search(entries);
entries = search.search_all_threaded(_search_all, _regex);
}
2019-05-19 09:42:52 +02:00
switch (_format)
2019-05-19 09:42:52 +02:00
{
case export_format::csv:
2019-05-16 08:37:50 +02:00
{
2019-05-19 09:42:52 +02:00
if (file.is_open())
{
2019-06-06 16:06:25 +02:00
Export::CSV(entries, file).print();
2019-05-19 09:42:52 +02:00
file.close();
}
else
{
2019-06-06 16:06:25 +02:00
Export::CSV(entries).print();
2019-05-19 09:42:52 +02:00
}
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::AsciiDoc(entries, file).print();
2019-05-19 09:42:52 +02:00
file.close();
}
else
{
Export::AsciiDoc(entries).print();
2019-05-19 09:42:52 +02:00
}
break;
2019-05-16 08:37:50 +02:00
}
2019-05-26 15:47:06 +02:00
case export_format::bookmarks:
{
if (file.is_open())
{
2019-06-06 17:11:56 +02:00
Export::Bookmarks(entries, file).print();
2019-05-26 15:47:06 +02:00
file.close();
}
else
{
2019-06-06 17:11:56 +02:00
Export::Bookmarks(entries).print();
2019-05-26 15:47:06 +02:00
}
break;
}
2019-07-21 16:37:36 +02:00
case export_format::simple:
{
if (file.is_open())
{
Export::Simple(entries, file).print();
file.close();
}
else
{
Export::Simple(entries).print();
}
break;
}
2019-09-03 17:47:39 +02:00
case export_format::json:
{
if (file.is_open())
{
Export::JSON(entries, file).print();
file.close();
}
else
{
Export::JSON(entries).print();
}
break;
}
2019-09-06 00:53:58 +02:00
case export_format::rss:
{
if (file.is_open())
{
Export::RSS(entries, file).print();
file.close();
}
else
{
Export::RSS(entries).print();
}
break;
}
case export_format::link:
{
if (file.is_open())
{
Export::Link(entries, file).print();
file.close();
}
else
{
Export::Link(entries).print();
}
break;
}
2019-11-28 09:03:52 +01:00
case export_format::rofi:
{
if (file.is_open())
{
Export::Rofi(entries, file).print();
file.close();
}
else
{
Export::Rofi(entries).print();
}
break;
}
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
return 0;
2019-05-11 02:52:33 +02:00
}
POCO_APP_MAIN(App)