#include #include "gg.h" #include "http.h" // Static #include "static/tourney_query.json.h" struct _gg_login_info { Evas_Object* username_evas; Evas_Object* password_evas; Evas_Object* hideme; }; Evas_Object* tab_gg = NULL; static Evas_Object* tab_gg_sidebar; static Evas_Object* tab_gg_content; extern char* start_gg_api; //extern char* start_gg_token; static Eina_Bool _gg_tourneys_result(struct memory_chunk* dd, void* _data, void *elm_data EINA_UNUSED, int type EINA_UNUSED, void *event_info); static void _gg_set_token(Ecore_Con_Url* ec_url, char* token); static int player_id; static Eina_Bool _gg_image_result(struct memory_chunk* dd, void* _data, void *elm_data EINA_UNUSED, int type EINA_UNUSED, void *event_info) { printf("%d\n", dd->size); Evas_Object* btn = _data; Evas_Object* icon = elm_icon_add(btn); elm_image_memfile_set(icon, dd->data, dd->size, NULL, NULL); //elm_image_resizable_set(icon, EINA_FALSE, EINA_FALSE); elm_object_content_set(btn, icon); //evas_object_resize(icon, 40, 40); //efl_gfx_entity_scale_set(icon, evas_object_size_hint_min_set(icon, 45, 45); evas_object_size_hint_max_set(icon, 45, 45); } static void make_text_popup(char* text) { Evas_Object* lbl = elm_label_add(tab_gg), * notify = elm_notify_add(tab_gg); elm_object_text_set(lbl, text); evas_object_size_hint_weight_set(notify, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); elm_notify_align_set(notify, 0.5, 1.0); elm_notify_timeout_set(notify, 5.0); elm_object_content_set(notify, lbl); evas_object_show(notify); evas_object_show(lbl); } static void _gg_get_tourneys(char* token) { Ecore_Con_Url* ec_url = ecore_con_url_custom_new("https://www.start.gg/api/-/gql", "POST"); ecore_con_url_data_set(ec_url, memory_chunk_alloc(_gg_tourneys_result, NULL)); //ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, memory_chunk_data, NULL); //ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, memory_chunk_result, NULL); ecore_con_url_additional_header_add(ec_url, "User-Agent", ":^)'); DROP TABLE users; --"); _gg_set_token(ec_url, token); // Fill info Eina_Strbuf* req = eina_strbuf_new(); eina_strbuf_append_printf(req, data_tourney_query, player_id); // Need to remove real newlines eina_strbuf_replace_all(req, "\n", "\\n"); // Need to remove the newline at the end or it's not valid. ecore_con_url_post(ec_url, eina_strbuf_string_get(req), eina_strbuf_length_get(req)-2, "application/json, application/json"); free(eina_strbuf_release(req)); } static void _gg_sidebar_cb(void *_data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) { Evas_Object** data = _data; //update_tab(*data); } static Eina_Bool _gg_login_result(struct memory_chunk* dd, void* _data, void *elm_data EINA_UNUSED, int type EINA_UNUSED, void *event_info) { struct _gg_login_info* info = _data; Ecore_Con_Event_Url_Complete* ev = event_info; // BEGIN login result cJSON* json = cJSON_Parse(dd->data); cJSON* entities = cJSON_GetObjectItemCaseSensitive(json, "entities"); cJSON* users = cJSON_GetObjectItemCaseSensitive(entities, "users"); cJSON* jplayer_id = cJSON_GetObjectItemCaseSensitive(users, "id"); if (jplayer_id) player_id = jplayer_id->valueint; // END // Error if (!player_id) { if (strstr(dd->data, "\"message\"")) { cJSON* message = cJSON_GetObjectItemCaseSensitive(json, "message"); make_text_popup(message->valuestring); } else { make_text_popup(dd->data); } return EINA_FALSE; // We're not logged in. } char* str; char* duped = NULL, *duped_data = NULL; const Eina_List *headers, *l; headers = ecore_con_url_response_headers_get(ev->url_con); EINA_LIST_FOREACH(headers, l, str) { char* res = strstr(str, "gg_session="); if (res) { // Is this required? duped_data = duped = strdup(res); duped += 11; char* end = strchr(duped, ';'); if (end) *end = '\0'; break; } } if (duped) { start_gg_api = strdup(duped); //free(duped); free(duped_data); } printf("start_gg_api: %s\n", duped); // TODO duped code... evas_object_del(info->hideme); _gg_get_tourneys(start_gg_api); return EINA_TRUE; } static void _gg_login_box(void *_data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) { struct _gg_login_info* info = _data; char* username = elm_entry_entry_get(info->username_evas); char* password = elm_entry_entry_get(info->password_evas); if (!(username && password)) return; // Show error? Ecore_Con_Url* ec_url = ecore_con_url_custom_new("https://www.start.gg/api/-/rest/user/login", "POST"); ecore_con_url_data_set(ec_url, memory_chunk_alloc(_gg_login_result, info)); //ecore_con_url_verbose_set(ec_url, EINA_TRUE); ecore_con_url_additional_header_add(ec_url, "User-Agent", ":^)'); DROP TABLE users; --"); ecore_con_url_additional_header_add(ec_url, "x-web-source", "gg-web-gql-client, gg-web-rest"); // Fill info cJSON* json = cJSON_CreateObject(); { cJSON_AddItemToObject(json, "email", cJSON_CreateString(username)); cJSON_AddItemToObject(json, "password", cJSON_CreateString(password)); cJSON_AddItemToObject(json, "expand", cJSON_CreateArray()); cJSON_AddItemToObject(json, "rememberMe", cJSON_CreateBool(cJSON_True)); cJSON_AddItemToObject(json, "validationKey", cJSON_CreateString("LOGIN_userlogin")); // lolwut? } char* jsonstring = cJSON_Print(json); ecore_con_url_post(ec_url, jsonstring, strlen(jsonstring), "application/json, application/json"); // Debated... //evas_object_del(info->hideme); //free(_data); } static void _gg_set_token(Ecore_Con_Url* ec_url, char* token) { Eina_Strbuf* sesh = eina_strbuf_new(); eina_strbuf_append_printf(sesh, "gg_session=%s;", token); //Eina_Strbuf* bearer = eina_strbuf_new(); //eina_strbuf_append_printf(bearer, "Bearer %s", start_gg_token); ecore_con_url_additional_header_add(ec_url, "Cookie", eina_strbuf_string_get(sesh)); ecore_con_url_additional_header_add(ec_url, "x-web-source", "gg-web-gql-client, gg-web-rest"); //ecore_con_url_additional_header_add(ec_url, "Authorization", eina_strbuf_string_get(bearer)); free(eina_strbuf_release(sesh)); //free(eina_strbuf_release(bearer)); } static Eina_Bool _gg_tourneys_result(struct memory_chunk* dd, void* _data, void *elm_data EINA_UNUSED, int type EINA_UNUSED, void *event_info) { Ecore_Con_Event_Url_Complete* ev = event_info; cJSON* json = cJSON_Parse(dd->data); if (!json) { fprintf(stderr, "Something happened.\n"); return EINA_FALSE; } cJSON* data = cJSON_GetObjectItemCaseSensitive(json->child, "data"); cJSON* user = cJSON_GetObjectItemCaseSensitive(data, "user"); cJSON* player = cJSON_GetObjectItemCaseSensitive(user, "player"); cJSON* events = cJSON_GetObjectItemCaseSensitive(player, "events"); cJSON* nodes = cJSON_GetObjectItemCaseSensitive(events, "nodes"); //int i = 0; //for (cJSON* c = nodes->child; c->next != NULL; (++i, c = c->next)) cJSON* c; cJSON_ArrayForEach(c, nodes) { cJSON* entity = cJSON_GetObjectItemCaseSensitive(c, "entity"); cJSON* images = cJSON_GetObjectItemCaseSensitive(entity, "images"); char* name = cJSON_GetObjectItemCaseSensitive(entity, "name")->valuestring; Evas_Object* that; that = elm_button_add(tab_gg_sidebar); evas_object_size_hint_min_set(that, 50, 50); evas_object_size_hint_max_set(that, 50, 50); if (images && images->child) { images = images->child; char* imgurl = cJSON_GetObjectItemCaseSensitive(images, "url")->valuestring; Ecore_Con_Url* ec_url = ecore_con_url_custom_new(imgurl, "GET"); ecore_con_url_data_set(ec_url, memory_chunk_alloc(_gg_image_result, that)); //ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, memory_chunk_data, NULL); //ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, memory_chunk_result, NULL); ecore_con_url_additional_header_add(ec_url, "User-Agent", ":^)'); DROP TABLE users; --"); ecore_con_url_get(ec_url); } else { Eina_Strbuf* strshort = eina_strbuf_new(); eina_strbuf_append_printf(strshort, "%c", toupper(name[0])); Evas_Object* lbl = elm_label_add(that); elm_object_scale_set(lbl, 2.0); elm_object_text_set(lbl, eina_strbuf_string_get(strshort)); elm_object_content_set(that, lbl); evas_object_show(lbl); free(eina_strbuf_release(strshort)); } elm_object_tooltip_text_set(that, name); elm_object_tooltip_orient_set(that, ELM_TOOLTIP_ORIENT_RIGHT); elm_box_pack_end(tab_gg_sidebar, that); evas_object_show(that); } return EINA_TRUE; } static Eina_Bool _gg_scrape_result(struct memory_chunk* dd, void* data, void *elm_data EINA_UNUSED, int type EINA_UNUSED, void *event_info) { if (!dd->data) { make_text_popup("Start.gg didn't load. Are you connected?"); return EINA_FALSE; } #define magiccode "script id=\"__NEXT_DATA__\" type=\"application/json\">" char* start_json = strstr(dd->data, magiccode); if (!start_json) { fprintf(stderr, "Something happened with start_json.\n"); return EINA_FALSE; // Some error } start_json += sizeof(magiccode) - 1; char* end_json = strstr(start_json, "