diff options
| author | 2024-01-01 11:23:22 -0500 | |
|---|---|---|
| committer | 2024-01-01 11:23:22 -0500 | |
| commit | 130a0edc0405e53b45f8b2f8da0b94356480a644 (patch) | |
| tree | 76cda2f7e0098bdcb6c54dc152bf3e95467fd44e /src/fruits/cap | |
| parent | 099126fdca625c5f6c219c5a1a7ecbc0e145988f (diff) | |
| download | camu-130a0edc0405e53b45f8b2f8da0b94356480a644.tar.gz camu-130a0edc0405e53b45f8b2f8da0b94356480a644.tar.bz2 camu-130a0edc0405e53b45f8b2f8da0b94356480a644.zip | |
wip
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/fruits/cap')
| -rw-r--r-- | src/fruits/cap/cap.c | 59 | ||||
| -rw-r--r-- | src/fruits/cap/cap.h | 9 | ||||
| -rw-r--r-- | src/fruits/cap/meson.build | 6 |
3 files changed, 45 insertions, 29 deletions
diff --git a/src/fruits/cap/cap.c b/src/fruits/cap/cap.c index 317abf4..3f5f5e8 100644 --- a/src/fruits/cap/cap.c +++ b/src/fruits/cap/cap.c @@ -1,20 +1,20 @@ #include <al/random.h> -#include "cap.h" - -#if CAP_USE_PYTHON -#include "../../shoki/src/search.h" -#endif #include "../../cache/handlers/file.h" #ifdef AKIYO_HAS_CURL #include "../../cache/handlers/http.h" #endif +#include "cap.h" + void cap_init(struct cap_runner *cap, bool (*callback)(void *, u8, str *, str *, void *), void *userdata) { cap->callback = callback; cap->userdata = userdata; al_array_init(cap->lists); +#if CAP_USE_PYTHON + sho_client_init(&cap->search, NULL); +#endif } static struct cap_list *list_from_name(struct cap_runner *cap, str *name) @@ -28,7 +28,7 @@ static struct cap_list *list_from_name(struct cap_runner *cap, str *name) return NULL; } -static struct cch_entry *entry_for_unique_id(str *unique_id) +static struct cch_entry *entry_for_unique_id(struct cap_runner *cap, str *unique_id) { struct cch_entry *entry = NULL; #ifdef AKIYO_HAS_CURL @@ -36,16 +36,21 @@ static struct cch_entry *entry_for_unique_id(str *unique_id) if (is_search || al_str_cmp(unique_id, al_str_c("https://"), 0, 8) == 0 || al_str_cmp(unique_id, al_str_c("http://"), 0, 7) == 0) { #if CAP_USE_PYTHON - struct sho_search s; - sho_search_init(&s); + str provider; str query; + al_str_from(&provider, ""); al_str_from(&query, ""); if (al_str_cmp(unique_id, al_str_c("https://twitter.com"), 0, 19) == 0 || al_str_cmp(unique_id, al_str_c("https://x.com"), 0, 13) == 0) { al_str_cat(&query, al_str_c("tweet:")); al_str_cat(&query, unique_id); - if (!sho_search_more_results(&s, al_str_c("twitter"), &query)) { + al_str_cat(&provider, al_str_c("twitter")); + } else if (al_str_cmp(unique_id, al_str_c("https://instagram.com"), 0, 21) == 0) { + s32 slash = al_str_rfind(unique_id, '/'); + if (slash >= 0) { + al_str_cat(&query, al_str_substr(unique_id, slash + 1, unique_id->len)); } + al_str_cat(&provider, al_str_c("instagram")); } else { if (is_search) { al_str_cat(&query, al_str_substr(unique_id, 1, unique_id->len)); @@ -53,26 +58,27 @@ static struct cch_entry *entry_for_unique_id(str *unique_id) al_str_cat(&query, al_str_c("link:")); al_str_cat(&query, unique_id); } - if (!sho_search_more_results(&s, al_str_c("youtube"), &query)) { - } + al_str_cat(&provider, al_str_c("youtube")); } + s32 id = sho_client_create_search(&cap->search, &provider, &query); + al_str_free(&provider); al_str_free(&query); - entry = NULL; - struct sho_result_page *page; - al_array_foreach_ptr(s.result.pages, k, page) { - struct sho_post *post; - al_array_foreach_ptr(page->posts, i, post) { - struct sho_post_media *media; - al_array_foreach_ptr(post->media, j, media) { - if (media->url.len > 0) { - entry = cch_handler_http_create(&media->url); - break; - } + if (id < 0) return NULL; + struct sho_search *search = sho_client_get_search(&cap->search, id); + if (!search || !sho_search_from_page(search, 0)) return NULL; + struct sho_result_page *page = &al_array_at(search->pages, 0); + struct sho_post *post; + al_array_foreach_ptr(page->posts, i, post) { + struct sho_post_media *media; + al_array_foreach_ptr(post->media, j, media) { + if (media->url.len > 0) { + entry = cch_handler_http_create(&media->url); + break; } - if (entry) break; } + if (entry) break; } - sho_search_free(&s); + sho_client_discard_search(&cap->search, id); #else if (!is_search) entry = cch_handler_http_create(unique_id); #endif @@ -88,9 +94,8 @@ static bool buffer_entry(struct cap_runner *cap, struct cap_list *list, struct c { if (entry->errored) return false; if (entry->buffer_requested) return true; - entry->entry = entry_for_unique_id(&entry->unique_id); - if (!entry->entry) return false; - if (!cap->callback(cap->userdata, CAP_BUFFER, &list->name, &entry->unique_id, entry->entry)) { + entry->entry = entry_for_unique_id(cap, &entry->unique_id); + if (!entry->entry || !cap->callback(cap->userdata, CAP_BUFFER, &list->name, &entry->unique_id, entry->entry)) { entry->errored = true; return false; } diff --git a/src/fruits/cap/cap.h b/src/fruits/cap/cap.h index 57f6889..69d4d6d 100644 --- a/src/fruits/cap/cap.h +++ b/src/fruits/cap/cap.h @@ -1,11 +1,15 @@ #pragma once -#define CAP_USE_PYTHON 1 +#define CAP_USE_PYTHON 0 #include <al/array.h> #include <al/str.h> #include <aki/thread.h> +#if CAP_USE_PYTHON +#include "../../shoki/src/search.h" +#endif + struct cap_list_entry { str unique_id; struct cch_entry *entry; @@ -34,6 +38,9 @@ enum { struct cap_runner { array(struct cap_list) lists; +#if CAP_USE_PYTHON + struct sho_client search; +#endif bool (*callback)(void *, u8, str *, str *, void *); void *userdata; }; diff --git a/src/fruits/cap/meson.build b/src/fruits/cap/meson.build index b5930ac..f85ab00 100644 --- a/src/fruits/cap/meson.build +++ b/src/fruits/cap/meson.build @@ -1,4 +1,8 @@ cap_src = ['cap.c'] -cap_deps = [common_deps, shoki] +cap_deps = [common_deps] +use_python = true +if use_python + cap_deps += [shoki] +endif cap = declare_dependency(sources: cap_src, dependencies: cap_deps) |