pornsieve  0.0.0
Search porn sites and filter the results using regular expressions
Namespaces | Classes | Typedefs | Enumerations | Functions
pornsieve Namespace Reference

The namespace containing the pornsieve library. More...

Namespaces

 redtube
 Namespace for the RedTube API.
 

Classes

class  CURLException
 Exception for libcurl errors. More...
 
class  HTTPException
 Exception for HTTP errors. More...
 
struct  search_result
 Search results. More...
 

Typedefs

using time_point = std::chrono::system_clock::time_point
 In this lib we use the system clock for our time points. More...
 

Enumerations

enum  result_order {
  none, newest, mostviewed, rating,
  longest, shortest, popular, popular_week,
  popular_month
}
 Order of the search results. More...
 
enum  media_type { video }
 Which type of media to search for. More...
 

Functions

void configure_curl (CURL *curl_easy_handle)
 Set some options for curl connections. More...
 
vector< string > json_to_vector (string_view json, string_view key)
 Converts a JSON array into a vector. More...
 
std::chrono::seconds string_to_duration (string_view duration_string)
 Converts a duration from string to chrono seconds. More...
 
time_point string_to_timepoint (string_view time_string)
 Converts a time from string to time_point. More...
 
string timepoint_to_string (const system_clock::time_point &timepoint, string_view format="%FT%T")
 Return date and time as string in UTC. More...
 
void init ()
 Global pornsieve initialization. More...
 
void cleanup ()
 Global pornsieve cleanup. More...
 

Detailed Description

The namespace containing the pornsieve library.

Since
0.1.0

Typedef Documentation

◆ time_point

typedef std::chrono::system_clock::time_point pornsieve::time_point

In this lib we use the system clock for our time points.

Enumeration Type Documentation

◆ media_type

enum pornsieve::media_type
strong

Which type of media to search for.

Since
0.1.0
65 {
66  video
67 };

◆ result_order

Order of the search results.

Not every source accepts every value.

Since
0.1.0
47 {
48  none,
49  newest,
50  mostviewed,
51  rating,
52  longest,
53  shortest,
54  popular,
55  popular_week,
56  popular_month
57 };

Function Documentation

◆ cleanup()

void pornsieve::cleanup ( )

Global pornsieve cleanup.

This function must be called, only once, after you are done using pornsieve. Calls curl_global_cleanup(3). Not thread-safe.

Since
0.1.0
37 {
38  curl_global_cleanup();
39 }

◆ configure_curl()

void pornsieve::configure_curl ( CURL *  curl_easy_handle)

Set some options for curl connections.

This function is called every time a curl handle is created. It sets the user-agent and the proxy (if set in config::proxy).

Exceptions
pornsieve::CURLException
Since
0.1.0
32 {
33  CURLcode code{};
34 
35  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
36  code = curl_easy_setopt(curl_easy_handle, CURLOPT_USERAGENT,
37  format("pornsieve/{:s}", version).data());
38  if (code != CURLE_OK)
39  {
40  throw CURLException{code, "Could not set up user-agent"};
41  }
42 
43  if (!config::proxy.empty())
44  {
45  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
46  code = curl_easy_setopt(curl_easy_handle, CURLOPT_PROXY,
47  config::proxy.data());
48  if (code != CURLE_OK)
49  {
50  throw CURLException{code, "Could not set up proxy"};
51  }
52  }
53 }

◆ init()

void pornsieve::init ( )

Global pornsieve initialization.

This function must be called, only once, before anything else from the library is used. Calls curl_global_init(3). Not thread-safe.

Exceptions
pornsieve::CURLExceptionThrown if curl_global_init() fails.
Since
0.1.0
27 {
28  // NOLINTNEXTLINE(hicpp-signed-bitwise)
29  const CURLcode code{curl_global_init(CURL_GLOBAL_ALL)};
30  if (code != CURLE_OK)
31  {
32  throw CURLException{code, "Global libcurl init failed"};
33  }
34 }

◆ json_to_vector()

vector< string > pornsieve::json_to_vector ( string_view  json,
string_view  key 
)

Converts a JSON array into a vector.

Parameters
jsonThe JSON array as string.
keyThe key which value to put into the vector.
Exceptions
nlohmann::detail::type_errorThrown if the format of the JSON is different than expected.
Since
0.1.0
38 {
39  vector<string> result;
40  for (const auto &item : nlohmann::json::parse(json))
41  {
42  result.emplace_back(item[key.data()]);
43  }
44  return result;
45 }

◆ string_to_duration()

std::chrono::seconds pornsieve::string_to_duration ( string_view  duration_string)

Converts a duration from string to chrono seconds.

Parameters
duration_stringFormat: SS or MM:SS or HH:MM:SS.
Exceptions
-See std::stoul
Since
0.1.0
48 {
49  std::uint32_t seconds{0};
50 
51  size_t startpos{0};
52  size_t endpos{0};
53 
54  while (true)
55  {
56  endpos = duration_string.find(':', startpos);
57  seconds *= 60;
58  seconds += static_cast<uint32_t>(std::stoul(
59  duration_string.substr(startpos, endpos - startpos).data()));
60  startpos = endpos + 1;
61 
62  // Not pretty, but cleanest way to break AFTER the seconds are added.
63  if (endpos == string_view::npos)
64  {
65  break;
66  }
67  }
68 
69  return std::chrono::seconds{seconds};
70 }

◆ string_to_timepoint()

time_point pornsieve::string_to_timepoint ( string_view  time_string)

Converts a time from string to time_point.

Note
We are assuming that times are in UTC.
Since
0.1.0
73 {
74  std::stringstream sstime(time_string.data());
75  struct std::tm tm = {};
76  tm.tm_isdst = -1; // Don't convert to/from daylight saving time.
77  sstime >> std::get_time(&tm, "%Y-%m-%d %T"); // TODO: Check other APIs.
78  std::time_t time = mktime(&tm);
79 
80  return system_clock::from_time_t(time);
81 }

◆ timepoint_to_string()

string pornsieve::timepoint_to_string ( const system_clock::time_point &  timepoint,
string_view  format = "%FT%T" 
)

Return date and time as string in UTC.

Parameters
timepointThe point in time to convert.
formatFormat of the string (optional, defaults to %FT%T).

See strftime(3) for information on how to construct a valid format string.

Since
0.1.0
85 {
86  constexpr uint16_t bufsize = 1024;
87  time_t time = system_clock::to_time_t(timepoint);
88  std::tm *tm{nullptr};
89  tm = std::gmtime(&time);
90  char buffer[bufsize];
91  strftime(buffer, bufsize, format.data(), tm);
92 
93  return buffer;
94 }