Browse Source

Merge pull request #147 from tsoding/146

(#146) Migrate emote_downloader to v3 BTTV API
Alexey Kutepov 5 years ago
parent
commit
0e8de76086
3 changed files with 78 additions and 30 deletions
  1. 5 1
      Makefile
  2. 72 29
      src/emote_downloader.cpp
  3. 1 0
      src/vodus.cpp

+ 5 - 1
Makefile

@@ -7,12 +7,16 @@ VODUS_EXTRA_CXXFLAGS += -DVODUS_SSE -msse4
 endif
 
 # TODO(#87): we need an option to build with system libraries
+ifeq ($(UNAME), Darwin)
+VODUS_PKGS=freetype2 libpcre2-8 glfw3
+else
 VODUS_PKGS=freetype2 libpcre2-8 glfw3 gl
+endif
 VODUS_CXXFLAGS=-Wall -fno-exceptions -std=c++17 $(VODUS_EXTRA_CXXFLAGS) -ggdb `pkg-config --cflags $(VODUS_PKGS)` -I./third_party/ffmpeg-4.3-dist/usr/local/include/ -I./third_party/giflib-5.2.1-dist/usr/local/include/
 VODUS_LIBS=`pkg-config --libs $(VODUS_PKGS)` -L./third_party/giflib-5.2.1-dist/usr/local/lib/ ./third_party/giflib-5.2.1-dist/usr/local/lib/libgif.a -L./third_party/ffmpeg-4.3-dist/usr/local/lib/ -L./third_party/glfw-3.3.2-dist/usr/local/lib/ -lavcodec -lavutil -lswresample -pthread -lm -llzma -lz -ldl
 
 ifeq ($(UNAME), Darwin)
-VODUS_LIBS += -framework AVFoundation -framework VideoToolbox -framework CoreVideo -framework AudioToolbox -framework CoreMedia -framework CoreFoundation -liconv
+VODUS_LIBS += -framework AVFoundation -framework VideoToolbox -framework CoreVideo -framework AudioToolbox -framework CoreMedia -framework CoreFoundation -framework OpenGL -liconv
 endif
 
 EMOTE_DOWNLOADER_PKGS=libcurl

+ 72 - 29
src/emote_downloader.cpp

@@ -256,10 +256,42 @@ 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_bttv_emotes_array(Json_Array emotes_array,
+                              FILE *mapping,
+                              Fixed_Array<Curl_Download, 1024> *downloads)
+{
+    FOR_JSON (Json_Array, emote, 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);
+    }
+}
+
+void append_global_bttv_mapping(CURL *curl,
+                                const char *emotes_url,
+                                FILE *mapping,
+                                Fixed_Array<Curl_Download, 1024> *downloads)
 {
     curl_buffer.clean();
 
@@ -285,36 +317,48 @@ void append_bttv_mapping(CURL *curl,
         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);
+    append_bttv_emotes_array(result.value.array, mapping, downloads);
+}
 
-    FOR_JSON (Json_Array, emote, emotes.array) {
-        expect_json_type(emote->value, JSON_OBJECT);
+void append_channel_bttv_mapping(CURL *curl,
+                                 const char *emotes_url,
+                                 FILE *mapping,
+                                 Fixed_Array<Curl_Download, 1024> *downloads)
+{
+    curl_buffer.clean();
 
-        auto emote_id = json_object_value_by_key(emote->value.object, SLT("id"));
-        expect_json_type(emote_id, JSON_STRING);
+    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();
+    }
 
-        auto emote_code = json_object_value_by_key(emote->value.object, SLT("code"));
-        expect_json_type(emote_code, JSON_STRING);
+    Memory memory = {};
+    memory.capacity = JSON_MEMORY_BUFFER_CAPACITY;
+    memory.buffer = json_memory_buffer;
 
-        auto emote_imageType = json_object_value_by_key(emote->value.object, SLT("imageType"));
-        expect_json_type(emote_imageType, JSON_STRING);
+    String source = {
+        curl_buffer.size,
+        curl_buffer.data
+    };
 
-        Curl_Download download = {};
+    Json_Result result = parse_json_value(&memory, source);
+    if (result.is_error) {
+        print_json_error(stdout, result, source, emotes_url);
+        abort();
+    }
 
-        download.file.write("emotes/emote-");
-        download.file.write(downloads->size);
-        download.file.write(".");
-        download.file.write(emote_imageType.string.data, emote_imageType.string.len);
+    expect_json_type(result.value, JSON_OBJECT);
 
-        download.url.write("https://cdn.betterttv.net/emote/");
-        download.url.write(emote_id.string.data, emote_id.string.len);
-        download.url.write("/3x");
+    auto channel_emotes = json_object_value_by_key(result.value.object, SLT("channelEmotes"));
+    expect_json_type(channel_emotes, JSON_ARRAY);
+    append_bttv_emotes_array(channel_emotes.array, mapping, downloads);
 
-        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);
+    append_bttv_emotes_array(shared_emotes.array, mapping, downloads);
 }
 
 bool is_sep(char x)
@@ -419,9 +463,8 @@ int main(void)
 
     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_room_ffz_mapping(curl, "https://api.frankerfacez.com/v1/room/tsoding", mapping, &downloads);
 

+ 1 - 0
src/vodus.cpp

@@ -38,6 +38,7 @@ using namespace aids;
 #include <smmintrin.h>
 #endif // VODUS_SSE
 
+#define GLFW_INCLUDE_GLCOREARB
 #define GL_GLEXT_PROTOTYPES
 #include <GLFW/glfw3.h>