Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
Get user name from Twitch instead of login form
Browse files Browse the repository at this point in the history
Fixes #140
  • Loading branch information
Ippytraxx committed Aug 7, 2016
1 parent 77ca78a commit 3e38b7e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 23 deletions.
51 changes: 29 additions & 22 deletions src/gt-twitch-login-dlg.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ gt_twitch_login_dlg_class_init(GtTwitchLoginDlgClass* klass)
gtk_widget_class_bind_template_child_private(widget_class, GtTwitchLoginDlg, web_view);
}

static void
user_name_cb(GObject* source,
GAsyncResult* res,
gpointer udata)
{
GtTwitchLoginDlg* self = GT_TWITCH_LOGIN_DLG(udata);
GError* error = NULL;

gchar* user_name = g_task_propagate_pointer(G_TASK(res), &error);

if (error)
{
gtk_widget_destroy(GTK_WIDGET(self));

return; //TODO: Show error to user
}

g_message("{GtTwitchLoginDlg} Successfulyy got username=%s", user_name);

g_object_set(main_app, "user-name", user_name, NULL);

gt_win_show_info_message(GT_WIN(gtk_window_get_transient_for(GTK_WINDOW(self))),
_("Successfully logged in to Twitch!"));

g_free(user_name);
gtk_widget_destroy(GTK_WIDGET(self));
}

static void
uri_changed_cb(GObject* source,
GParamSpec* pspec,
Expand All @@ -110,38 +138,18 @@ uri_changed_cb(GObject* source,

g_message("{GtTwitchLoginDlg} Successfully got OAuth token '%s'", token);

gt_win_show_info_message(GT_WIN(gtk_window_get_transient_for(GTK_WINDOW(self))),
_("Successfully logged in to Twitch!"));
gt_twitch_user_name_async(main_app->twitch, user_name_cb, self);

gtk_widget_destroy(GTK_WIDGET(self));
g_free(token);

}
else if (g_str_has_prefix(url, "http://localhost/?error=access_denied"))
{
g_message("{GtTwitchLoginDlg} Error logging or login cancelled");

gtk_widget_destroy(GTK_WIDGET(self));
}

g_match_info_unref(match_info);
g_free(url);
}

static void
submit_form_cb(WebKitWebView* web_view,
WebKitFormSubmissionRequest* request,
gpointer udata)
{
GHashTable* text_fields = webkit_form_submission_request_get_text_fields(request);

gchar* user_name = g_hash_table_lookup(text_fields, "username");

g_message("{GtTwitchLoginDlg} Got username '%s' from form", user_name);

g_object_set(main_app, "user-name", user_name, NULL);
}

static void
gt_twitch_login_dlg_init(GtTwitchLoginDlg* self)
{
Expand All @@ -152,7 +160,6 @@ gt_twitch_login_dlg_init(GtTwitchLoginDlg* self)
gtk_widget_init_template(GTK_WIDGET(self));

g_signal_connect(priv->web_view, "notify::uri", G_CALLBACK(uri_changed_cb), self);
g_signal_connect(priv->web_view, "submit-form", G_CALLBACK(submit_form_cb), self);

const gchar* uri = g_strdup_printf("https://api.twitch.tv/kraken/oauth2/authorize?response_type=token&client_id=%s&redirect_uri=%s&scope=%s", "afjnp6n4ufzott4atb3xpb8l5a31aav", "http://localhost", "chat_login+user_follows_edit");

Expand Down
90 changes: 90 additions & 0 deletions src/gt-twitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define FOLLOWS_URI "https://api.twitch.tv/api/users/%s/follows/channels?limit=%d&offset=%d"
#define FOLLOW_CHANNEL_URI "https://api.twitch.tv/kraken/users/%s/follows/channels/%s?oauth_token=%s"
#define UNFOLLOW_CHANNEL_URI "https://api.twitch.tv/kraken/users/%s/follows/channels/%s?oauth_token=%s"
#define OAUTH_INFO_URI "https://api.twitch.tv/kraken?oauth_token=%s"

#define STREAM_INFO "#EXT-X-STREAM-INF"

Expand Down Expand Up @@ -1934,3 +1935,92 @@ gt_twitch_unfollow_channel_async(GtTwitch* self,

g_object_unref(task);
}

gchar*
gt_twitch_user_name(GtTwitch* self,
GError** error)
{
GtTwitchPrivate* priv = gt_twitch_get_instance_private(self);
SoupMessage* msg;
gchar* uri;
gchar* ret = NULL;
JsonParser* parser;
JsonNode* node;
JsonReader* reader;

uri = g_strdup_printf(OAUTH_INFO_URI,
gt_app_get_oauth_token(main_app));
msg = soup_message_new(SOUP_METHOD_GET, uri);

if (!send_message(self, msg))
{
//TODO: Use new logging function
g_warning("{GtTwitch} Unable to get username");

g_set_error(error, GT_TWITCH_ERROR, GT_TWITCH_ERROR_USER_NAME,
"Unable to get username");

goto finish;
}

parser = json_parser_new();
json_parser_load_from_data(parser, msg->response_body->data, msg->response_body->length, NULL);
node = json_parser_get_root(parser);
reader = json_reader_new(node);

json_reader_read_member(reader, "token");

json_reader_read_member(reader, "user_name");
ret = g_strdup(json_reader_get_string_value(reader));
json_reader_end_member(reader);

json_reader_end_member(reader);

g_object_unref(parser);
g_object_unref(reader);

finish:
g_free(uri);
g_object_unref(msg);

return ret;

}

static void
user_name_async_cb(GTask* task,
gpointer source,
gpointer task_data,
GCancellable* cancel)
{
GenericTaskData* data = task_data;
gchar* ret = NULL;
GError* error = NULL;

ret = gt_twitch_user_name(data->twitch, &error);

if (!ret)
g_task_return_error(task, error);
else
g_task_return_pointer(task, ret, g_free);
}

void
gt_twitch_user_name_async(GtTwitch* self,
GAsyncReadyCallback cb,
gpointer udata)
{
GTask* task = NULL;
GenericTaskData* data = NULL;

task = g_task_new(NULL, NULL, cb, udata);

data = generic_task_data_new();
data->twitch = self;

g_task_set_task_data(task, data, (GDestroyNotify) generic_task_data_free);

g_task_run_in_thread(task, user_name_async_cb);

g_object_unref(task);
}
4 changes: 3 additions & 1 deletion src/gt-twitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef enum _GtTwitchError
GT_TWITCH_ERROR_FOLLOWS_ALL,
GT_TWITCH_ERROR_FOLLOW_CHANNEL,
GT_TWITCH_ERROR_UNFOLLOW_CHANNEL,
GT_TWITCH_ERROR_USER_NAME,
} GtTwitchError;

typedef enum _GtTwitchStreamQuality
Expand Down Expand Up @@ -145,7 +146,8 @@ gboolean gt_twitch_follow_channel(GtTwitch* self, const gchar*
gboolean gt_twitch_unfollow_channel(GtTwitch* self, const gchar* chan_name);
void gt_twitch_follow_channel_async(GtTwitch* self, const gchar* chan_name, GAsyncReadyCallback cb, gpointer udata);
void gt_twitch_unfollow_channel_async(GtTwitch* self, const gchar* chan_name, GAsyncReadyCallback cb, gpointer udata);

gchar* gt_twitch_user_name(GtTwitch* self, GError** error);
void gt_twitch_user_name_async(GtTwitch* self, GAsyncReadyCallback cb, gpointer udata);
G_END_DECLS

#endif

0 comments on commit 3e38b7e

Please sign in to comment.