diff src/core/anime_db.cc @ 317:b1f4d1867ab1

services: VERY initial Kitsu support it only supports user authentication for now, but it's definitely a start.
author Paper <paper@paper.us.eu.org>
date Wed, 12 Jun 2024 04:07:10 -0400
parents 53e3c015a973
children d928ec7b6a0d
line wrap: on
line diff
--- a/src/core/anime_db.cc	Tue Jun 11 15:11:09 2024 -0400
+++ b/src/core/anime_db.cc	Wed Jun 12 04:07:10 2024 -0400
@@ -12,7 +12,9 @@
 #include <fstream>
 
 #include <exception>
+#include <cstdlib>
 #include <iostream>
+#include <random>
 
 namespace Anime {
 
@@ -300,6 +302,42 @@
 	return true;
 }
 
+int Database::GetUnusedId() {
+	/* TODO: move these out of here */
+
+	std::random_device rd;
+	std::mt19937 gen(rd());
+	std::uniform_int_distribution<int> distrib(1, INT_MAX);
+	int res;
+
+	do {
+		res = distrib(gen);
+	} while (items.count(res));
+
+	return res;
+}
+
+int Database::LookupServiceId(Service service, const std::string& id_to_find) {
+	for (const auto& [id, anime] : items) {
+		std::optional<std::string> service_id = anime.GetServiceId(service);
+		if (!service_id)
+			continue;
+
+		if (service_id == id_to_find)
+			return id;
+	}
+
+	return 0;
+}
+
+int Database::LookupServiceIdOrUnused(Service service, const std::string& id_to_find) {
+	int id = LookupServiceId(service, id_to_find);
+	if (id)
+		return id;
+
+	return GetUnusedId();
+}
+
 Database db;
 
 } // namespace Anime