Browse Source

(#146) Migrate emote_downloader to v3 BTTV API

rexim 5 years ago
parent
commit
90f4201c4c
1 changed files with 98 additions and 11 deletions
  1. 98 11
      src/emote_downloader.cpp

+ 98 - 11
src/emote_downloader.cpp

@@ -256,10 +256,10 @@ void append_global_ffz_mapping(CURL *curl,
     }
     }
 }
 }
 
 
-void append_bttv_mapping(CURL *curl,
-                         const char *emotes_url,
-                         FILE *mapping,
-                         Fixed_Array<Curl_Download, 1024> *downloads)
+void append_global_bttv_mapping(CURL *curl,
+                                const char *emotes_url,
+                                FILE *mapping,
+                                Fixed_Array<Curl_Download, 1024> *downloads)
 {
 {
     curl_buffer.clean();
     curl_buffer.clean();
 
 
@@ -285,11 +285,9 @@ void append_bttv_mapping(CURL *curl,
         abort();
         abort();
     }
     }
 
 
-    expect_json_type(result.value, JSON_OBJECT);
-    auto emotes = json_object_value_by_key(result.value.object, SLT("emotes"));
-    expect_json_type(emotes, JSON_ARRAY);
+    expect_json_type(result.value, JSON_ARRAY);
 
 
-    FOR_JSON (Json_Array, emote, emotes.array) {
+    FOR_JSON (Json_Array, emote, result.value.array) {
         expect_json_type(emote->value, JSON_OBJECT);
         expect_json_type(emote->value, JSON_OBJECT);
 
 
         auto emote_id = json_object_value_by_key(emote->value.object, SLT("id"));
         auto emote_id = json_object_value_by_key(emote->value.object, SLT("id"));
@@ -317,6 +315,96 @@ void append_bttv_mapping(CURL *curl,
     };
     };
 }
 }
 
 
+void append_channel_bttv_mapping(CURL *curl,
+                                 const char *emotes_url,
+                                 FILE *mapping,
+                                 Fixed_Array<Curl_Download, 1024> *downloads)
+{
+    curl_buffer.clean();
+
+    auto res = curl_perform_to_string_buffer(curl, emotes_url, &curl_buffer);
+    if (res != CURLE_OK) {
+        println(stderr, "curl_perform_to_string_buffer() failed: ",
+                curl_easy_strerror(res));
+        abort();
+    }
+
+    Memory memory = {};
+    memory.capacity = JSON_MEMORY_BUFFER_CAPACITY;
+    memory.buffer = json_memory_buffer;
+
+    String source = {
+        curl_buffer.size,
+        curl_buffer.data
+    };
+
+    Json_Result result = parse_json_value(&memory, source);
+    if (result.is_error) {
+        print_json_error(stdout, result, source, emotes_url);
+        abort();
+    }
+
+    expect_json_type(result.value, JSON_OBJECT);
+
+    auto channel_emotes = json_object_value_by_key(result.value.object, SLT("channelEmotes"));
+    expect_json_type(channel_emotes, JSON_ARRAY);
+    FOR_JSON (Json_Array, emote, channel_emotes.array) {
+        expect_json_type(emote->value, JSON_OBJECT);
+
+        auto emote_id = json_object_value_by_key(emote->value.object, SLT("id"));
+        expect_json_type(emote_id, JSON_STRING);
+
+        auto emote_code = json_object_value_by_key(emote->value.object, SLT("code"));
+        expect_json_type(emote_code, JSON_STRING);
+
+        auto emote_imageType = json_object_value_by_key(emote->value.object, SLT("imageType"));
+        expect_json_type(emote_imageType, JSON_STRING);
+
+        Curl_Download download = {};
+
+        download.file.write("emotes/emote-");
+        download.file.write(downloads->size);
+        download.file.write(".");
+        download.file.write(emote_imageType.string.data, emote_imageType.string.len);
+
+        download.url.write("https://cdn.betterttv.net/emote/");
+        download.url.write(emote_id.string.data, emote_id.string.len);
+        download.url.write("/3x");
+
+        println(mapping, emote_code.string, ",", download.file);
+        downloads->push(download);
+    }
+
+    auto shared_emotes = json_object_value_by_key(result.value.object, SLT("sharedEmotes"));
+    expect_json_type(shared_emotes, JSON_ARRAY);
+    FOR_JSON (Json_Array, emote, shared_emotes.array) {
+        expect_json_type(emote->value, JSON_OBJECT);
+
+        auto emote_id = json_object_value_by_key(emote->value.object, SLT("id"));
+        expect_json_type(emote_id, JSON_STRING);
+
+        auto emote_code = json_object_value_by_key(emote->value.object, SLT("code"));
+        expect_json_type(emote_code, JSON_STRING);
+
+        auto emote_imageType = json_object_value_by_key(emote->value.object, SLT("imageType"));
+        expect_json_type(emote_imageType, JSON_STRING);
+
+        Curl_Download download = {};
+
+        download.file.write("emotes/emote-");
+        download.file.write(downloads->size);
+        download.file.write(".");
+        download.file.write(emote_imageType.string.data, emote_imageType.string.len);
+
+        download.url.write("https://cdn.betterttv.net/emote/");
+        download.url.write(emote_id.string.data, emote_id.string.len);
+        download.url.write("/3x");
+
+        println(mapping, emote_code.string, ",", download.file);
+        downloads->push(download);
+    }
+}
+
 bool is_sep(char x)
 bool is_sep(char x)
 {
 {
 #ifdef _WIN32
 #ifdef _WIN32
@@ -419,9 +507,8 @@ int main(void)
 
 
     curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, MAX_PARALLEL);
     curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, MAX_PARALLEL);
 
 
-
-    append_bttv_mapping(curl, "https://api.betterttv.net/2/emotes", mapping, &downloads);
-    append_bttv_mapping(curl, "https://api.betterttv.net/2/channels/tsoding", mapping, &downloads);
+    append_global_bttv_mapping(curl, "https://api.betterttv.net/3/cached/emotes/global", mapping, &downloads);
+    append_channel_bttv_mapping(curl, "https://api.betterttv.net/3/cached/users/twitch/110240192", mapping, &downloads);
     append_global_ffz_mapping(curl, mapping, &downloads);
     append_global_ffz_mapping(curl, mapping, &downloads);
     append_room_ffz_mapping(curl, "https://api.frankerfacez.com/v1/room/tsoding", mapping, &downloads);
     append_room_ffz_mapping(curl, "https://api.frankerfacez.com/v1/room/tsoding", mapping, &downloads);