forked from keshavbhatt/olivia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
similartracks.cpp
110 lines (93 loc) · 3.65 KB
/
similartracks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "similartracks.h"
#include "elidedlabel.h"
#include <QDebug>
SimilarTracks::SimilarTracks(QObject *parent,int limit) : QObject(parent)
{
numberOfSimilarTracksToLoad = limit;
store_manager = this->parent()->findChild<store*>("store_manager");
}
void SimilarTracks::addSimilarTracks(QString video_id,QString songId){
isLoadingPLaylist = false;
QNetworkAccessManager *m_netwManager = new QNetworkAccessManager(this);
connect(m_netwManager,&QNetworkAccessManager::finished,[=](QNetworkReply* rep){
if(rep->error() == QNetworkReply::NoError){
QString repStr = rep->readAll();
QStringList list = repStr.split("gettrackinfo(");
list.removeFirst();
QStringList finalList;
foreach (QString str, list) {
//prevent adding already played tracks or add tracks only which are not played in this session
if(!playedTracksIds.contains(str.split("!=-=!")[4])){
QString dataStr = QString(str.split(");\">").first()).remove(""");
if(!dataStr.isEmpty() && (QString(dataStr.at(0))=="'"||QString(dataStr.at(0))=="\"")){
dataStr = dataStr.remove("'");
dataStr = dataStr.remove("\"");
}
finalList.append(dataStr);
}
}
// qDebug()<<finalList;
if(finalList.count()>0){
emit setSimilarTracks(finalList);
int tracksLeft;
if(finalList.count()<numberOfSimilarTracksToLoad){ //set finalLeft tracks for the for loop below
tracksLeft = finalList.count();
}else{
tracksLeft = numberOfSimilarTracksToLoad;
}
for (int i = 0; i < tracksLeft; i++) {
playedTracksIds.append(QString(finalList.at(i)).split("!=-=!")[4]);
}
}else{
emit failedGetSimilarTracks();
}
}else{
emit failedGetSimilarTracks();
}
rep->deleteLater();
m_netwManager->deleteLater();
});
QUrl url;
if(store_manager->getAlbumId(songId).contains("undefined-") || isNumericStr(songId)){
url = "http://ktechpit.com/USS/Olivia/youtube_related_videos.php?video_id="+video_id;
}else{
url = "http://ktechpit.com/USS/Olivia/spotify/spotify_related_seed_song.php?s_id="+songId+"&url=null";
}
QNetworkRequest request(url);
m_netwManager->get(request);
}
bool SimilarTracks::isNumericStr(const QString str){
bool isNumeric;
str.toInt(&isNumeric, 16);
str.toInt(&isNumeric, 10);
return isNumeric;
}
void SimilarTracks::addPlaylist(QString data){
isLoadingPLaylist = true;
emit clearList();
QStringList list = data.split("gettrackinfo(");
list.removeFirst();
QStringList finalList;
foreach (QString str, list) {
QString dataStr = QString(str.split(");\">").first()).remove(""");
if(!dataStr.isEmpty() && (QString(dataStr.at(0))=="'"||QString(dataStr.at(0))=="\"")){
dataStr = dataStr.remove("'");
dataStr = dataStr.remove("\"");
}
finalList.append(dataStr);
}
if(finalList.count()>0){
emit setPlaylist(finalList);
}else{
emit failedGetSimilarTracks();
}
}
void SimilarTracks::getNextTracksInPlaylist(QStringList trackListFromMainWindow){
isLoadingPLaylist = true;
emit clearListKeepingPlayingTrack();
if(trackListFromMainWindow.count()>0){
emit setPlaylist(trackListFromMainWindow);
}else{
emit failedGetSimilarTracks();
}
}