Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ Youtube chat example #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions examples/YoutubeChat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------

*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash

# qtcreator generated files
*.pro.user*

# xemacs temporary files
*.flc

# Vim temporary files
.*.swp

# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*

# MinGW generated files
*.Debug
*.Release

# Python byte code
*.pyc

# Binaries
# --------
*.dll
*.exe

34 changes: 34 additions & 0 deletions examples/YoutubeChat/YoutubeChat.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
QT += core gui


include(../../src/src.pri)
greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets
}


CONFIG += c++11


TARGET = YoutubeChat
CONFIG += console

TEMPLATE = app

SOURCES += main.cpp \
commentmodel.cpp \
livebroadcastmodel.cpp \
o2youtube.cpp \
youtubeapi.cpp \
youtubecontroller.cpp \
helper.cpp \
messagereceiver.cpp

HEADERS += \
commentmodel.h \
livebroadcastmodel.h \
o2youtube.h \
youtubeapi.h \
youtubecontroller.h \
helper.h \
messagereceiver.h
57 changes: 57 additions & 0 deletions examples/YoutubeChat/commentmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "commentmodel.h"

namespace youtube
{
CommentModel::CommentModel(QObject *parent): QAbstractListModel(parent)
{

}

QHash<int, QByteArray> CommentModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[RoleCommentMessage] = "Message properties";
roles[RoleCommentAuthor] = "Message author properties";
return roles;
}

void CommentModel::clearComments()
{
beginRemoveRows(QModelIndex(), 0, std::max(0,rowCount() - 1));
comments.clear();
endRemoveRows();
}

void CommentModel::addComment(QVariantMap comment)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
comments.append(comment);
endInsertRows();
}

int CommentModel::rowCount(const QModelIndex &parent) const
{
return comments.count();
}

QVariant CommentModel::data(const QModelIndex &index, int role) const
{
QVariant result;
QVariantMap comment = comments[index.row()];
switch (role) {
case Roles::RoleCommentMessage:
result = comment["snippet"];

/*foreach (const QVariant &v, broadcast.keys()) {
qWarning() << v.toString() << " : " << broadcast[v.toString()];
}*/
break;
case Roles::RoleCommentAuthor:
result = comment["authorDetails"];
break;
default:
result = comment;
}
return result;
}

}
111 changes: 111 additions & 0 deletions examples/YoutubeChat/commentmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#ifndef COMMENTMODEL_H
#define COMMENTMODEL_H

#include <QAbstractListModel>
#include <QList>
#include <QVariantMap>

#include <QObject>

namespace youtube
{
//docs can be found here: https://developers.google.com/youtube/v3/live/docs/liveChatMessages
enum class CommentType
{
TextMessage,
FanFunding,
MessageDeleted,
NewSponsor,
UserBanned,
//you can extend this class and define more types
//as youtube defines much more event types.
NotImplemented
};

//you can rewrite this class as it contain only fields author need.
//data example
/*
* "authorDetails": {
"channelId": "UCgCuI4JV0Imukb3ZcmC24kA",
"channelUrl": "http://www.youtube.com/channel/UCgCuI4JV0Imukb3ZcmC24kA",
"displayName": "Stas Artemjev",
"profileImageUrl": "https://yt3.ggpht.com/--esiXXT-93U/AAAAAAAAAAI/AAAAAAAAAAA/MReYWijclIg/s88-c-k-no-mo-rj-c0xffffff/photo.jpg",
"isVerified": false,
"isChatOwner": true,
"isChatSponsor": false,
"isChatModerator": false
}
* */
struct CommentAuthor
{
QString displayName;
QString profileImageUrl;
bool isChatOwner;
bool isChatSponsor;
bool isChatModerator;
};

//data example
/*
* "snippet": {
"type": "textMessageEvent",
"liveChatId": "EiEKGFVDZ0N1STRKVjBJbXVrYjNaY21DMjRrQRIFL2xpdmU",
"authorChannelId": "UCgCuI4JV0Imukb3ZcmC24kA",
"publishedAt": "2016-11-16T20:50:29.047Z",
"hasDisplayContent": true,
"displayMessage": "тест 3",
"textMessageDetails": {
"messageText": "тест 3"
}
* */
struct CommentMessage
{
QString text;
CommentType messageType;
bool canBeDisplayed;
};

struct CommentFundraisingDetails
{
unsigned long amountMicros;
QString currency;
QString amountDisplayString;
QString userComment;
};


class CommentModel:public QAbstractListModel
{
public:
enum Roles {
//message text
RoleCommentMessage = Qt::UserRole + 1,
//Type of message: standart message, fanfunding event, new sponsor event etc
RoleCommentAuthor = Qt::UserRole +2,
};

CommentModel(QObject *parent = 0);

/// Clear all comments
void clearComments();

/// Add a comment
void addComment(QVariantMap comment);

/// Get number of comments
int rowCount(const QModelIndex &parent = QModelIndex()) const;

/// Access a comment
QVariant data(const QModelIndex & index, int role) const;

/// Get role names
QHash<int, QByteArray>roleNames() const;


protected:
QList<QVariantMap> comments;


};
}
#endif // COMMENTMODEL_H
20 changes: 20 additions & 0 deletions examples/YoutubeChat/helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "helper.h"

using namespace youtube;
Helper::Helper(YoutubeController& controller,MessageReceiver& receiver,QObject * parent ):
youtube_(controller),
receiver_(receiver),
QObject(parent)
{
connect(&youtube_,SIGNAL(error(const QString )),&receiver_,SLOT(onError(const QString)));

connect(&youtube_,SIGNAL(message(const QString ,const QString ,const QString,bool , bool ,bool)),
&receiver_,SLOT(onNewMessage(const QString ,const QString ,const QString,bool , bool ,bool)));



connect(&youtube_,SIGNAL(fundraising(const QString ,const QString,const QString,ulong ,QString ,QString )),
&receiver_,SLOT(onFundraising(const QString ,const QString,const QString,ulong ,QString ,QString )));

connect(&youtube_,SIGNAL(newSponsor(const QString )),&receiver_,SLOT(onNewSponsor(const QString)));
}
16 changes: 16 additions & 0 deletions examples/YoutubeChat/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef HELPER_H
#define HELPER_H

#include "youtubecontroller.h"
#include "messagereceiver.h"
class Helper:public QObject
{
Q_OBJECT
public:
Helper(youtube::YoutubeController& controller,MessageReceiver& receiver,QObject * parent );
private:
youtube::YoutubeController& youtube_;
MessageReceiver& receiver_;
};

#endif // HELPER_H
49 changes: 49 additions & 0 deletions examples/YoutubeChat/livebroadcastmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <QDebug>
#include "livebroadcastmodel.h"

namespace youtube
{
LiveBroadcastModel::LiveBroadcastModel(QObject *parent) : QAbstractListModel(parent)
{
}

QHash<int, QByteArray> LiveBroadcastModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[RoleLiveChatId] = "RoleLiveChatId";
return roles;
}

void LiveBroadcastModel::addBroadcast(QVariantMap broadcast) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
broadcasts.append(broadcast);
endInsertRows();
}

void LiveBroadcastModel::clearBroadcasts() {
beginRemoveRows(QModelIndex(), 0, std::max(0,rowCount() - 1));
broadcasts.clear();
endRemoveRows();
}

int LiveBroadcastModel::rowCount(const QModelIndex &) const {
return broadcasts.count();
}

QVariant LiveBroadcastModel::data(const QModelIndex &index, int role) const {
QVariant result;
QVariantMap broadcast = broadcasts[index.row()];
switch (role) {
case LiveBroadcastModel::RoleLiveChatId:
result = broadcast["snippet"].toMap()["liveChatId"];

/*foreach (const QVariant &v, broadcast.keys()) {
qWarning() << v.toString() << " : " << broadcast[v.toString()];
}*/
break;
default:
result = broadcast;
}
return result;
}
}

Loading