Skip to content

Commit

Permalink
better similar track implementation, bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavbhatt committed Oct 8, 2019
1 parent 0112036 commit e5c5cd5
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Olivia.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.9.1, 2019-10-07T18:51:49. -->
<!-- Written by QtCreator 4.9.1, 2019-10-08T04:19:08. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
94 changes: 81 additions & 13 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ void MainWindow::init_similar_tracks(){
if(similarTracks==nullptr){
similarTracks = new SimilarTracks(this,5);
connect(similarTracks, &SimilarTracks::setSimilarTracks,[=](QStringList list){
similarTracks->isLoadingPLaylist = false;
currentSimilarTrackList.clear();
currentSimilarTrackList = list;
currentSimilarTrackProcessing = 0;
prepareSimilarTracks();
});
connect(similarTracks, &SimilarTracks::setPlaylist,[=](QStringList list){
similarTracks->isLoadingPLaylist = true;
currentSimilarTrackList.clear();
currentSimilarTrackList = list;
currentSimilarTrackProcessing = 0;
Expand All @@ -86,6 +94,10 @@ void MainWindow::init_similar_tracks(){
connect(similarTracks, &SimilarTracks::failedGetSimilarTracks,[=](){
ui->similarTrackLoader->stop();
});
connect(similarTracks, &SimilarTracks::clearList,[=](){
currentSimilarTrackList.clear();
ui->recommListWidget->clear();
});
}
}

Expand Down Expand Up @@ -1285,7 +1297,6 @@ void MainWindow::addToSimilarTracksQueue(const QVariant Base64andDominantColor){
currentSimilarTrackMeta.append(dominantColor);
currentSimilarTrackMeta.append(base64);


QWidget *track_widget = new QWidget(ui->recommListWidget);
track_widget->setToolTip(htmlToPlainText(title));
track_widget->setObjectName("track-widget-"+songId);
Expand Down Expand Up @@ -1370,29 +1381,75 @@ void MainWindow::addToSimilarTracksQueue(const QVariant Base64andDominantColor){
a->start(QPropertyAnimation::DeleteWhenStopped);
ui->recommListWidget->addItem(item);

ui->recommListWidget->setCurrentRow(ui->recommListWidget->count()-1);
// ui->recommListWidget->setCurrentRow(ui->recommListWidget->count()-1);
if(store_manager->isDownloaded(songId)){
ui->recommListWidget->itemWidget(item)->setEnabled(true);
track_ui.url->setText("file://"+setting_path+"/downloadedTracks/"+songId);
track_ui.offline->setPixmap(QPixmap(":/icons/offline.png").scaled(track_ui.offline->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
}else{
getAudioStream(ytIds,songId);
//reverse the process queue, so that recently added song can get first chance to process
if(ytdlQueue.count()>1){
ytdlQueue.insert(1, ytdlQueue.takeAt(ytdlQueue.count()-1));
}
}
// ui->recommListWidget->scrollToBottom();
similarTracksProcessHelper();
}else{
QListWidgetItem* item;
item = new QListWidgetItem(ui->recommListWidget);

QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);

item->setSizeHint(track_widget->minimumSizeHint());
ui->recommListWidget->setItemWidget(item, track_widget);
ui->recommListWidget->itemWidget(item)->setGraphicsEffect(eff);
ui->recommListWidget->itemWidget(item)->setEnabled(false); //enable when finds a url

QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(500);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InCirc);
a->start(QPropertyAnimation::DeleteWhenStopped);
ui->recommListWidget->addItem(item);

//ui->recommListWidget->setCurrentRow(ui->recommListWidget->count()-1);
if(store_manager->isDownloaded(songId)){
ui->recommListWidget->itemWidget(item)->setEnabled(true);
track_ui.url->setText("file://"+setting_path+"/downloadedTracks/"+songId);
track_ui.offline->setPixmap(QPixmap(":/icons/offline.png").scaled(track_ui.offline->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
}else{
QNetworkAccessManager *m_netwManager = new QNetworkAccessManager(this);
connect(m_netwManager,&QNetworkAccessManager::finished,[=](QNetworkReply* rep){
if(rep->error() == QNetworkReply::NoError){
QString id = rep->readAll();
getAudioStream(id,songId);
similarTracksProcessHelper();
rep->deleteLater();
m_netwManager->deleteLater();
}else{
qDebug()<<"error processing track"<<rep->request().url().toString();
}
});
QString query = title.replace("N/A","")+" - "+artist.replace("N/A","");
QUrl url("http://ktechpit.com/USS/Olivia/youtube.php?millis=0&query="+query);
QNetworkRequest request(url);
m_netwManager->get(request);
}
}
}

void MainWindow::similarTracksProcessHelper(){
//clear meta of currentSimilarTrack and free it for next track
currentSimilarTrackMeta.clear();
//process next track in currentSimilarTrackList list;
if(currentSimilarTrackProcessing < similarTracks->numberOfSimilarTracksToLoad /*currentSimilarTrackList.count()-1*/){
currentSimilarTrackProcessing++;
prepareSimilarTracks();
if(similarTracks->isLoadingPLaylist){
if(currentSimilarTrackProcessing < currentSimilarTrackList.count()-1){
currentSimilarTrackProcessing++;
prepareSimilarTracks();
}
}else{
if(currentSimilarTrackProcessing < similarTracks->numberOfSimilarTracksToLoad /*currentSimilarTrackList.count()-1*/){
currentSimilarTrackProcessing++;
prepareSimilarTracks();
}
}

}

void MainWindow::addToQueue(QString id,QString title,
Expand Down Expand Up @@ -1969,7 +2026,7 @@ void MainWindow::ytdlReadyRead(){
listWidgetItem->setEnabled(true);
QLineEdit *url = listWidgetItem->findChild<QLineEdit *>("url");
static_cast<QLineEdit*>(url)->setText(m48url);
qDebug()<<"NEW URL:"<<m48url;
//qDebug()<<"NEW URL:"<<m48url;
//TODO
store_manager->saveStreamUrl(songId,m48url,getExpireTime(m48url));
mfr->deleteLater();
Expand Down Expand Up @@ -2353,11 +2410,17 @@ void MainWindow::listItemDoubleClicked(QListWidget *list,QListWidgetItem *item){
shadow_list_.back()->setColor(QColor("#292929"));
lbl->setGraphicsEffect(shadow_list_.back());
}

if(store_manager->isDownloaded(songId)){
radio_manager->playRadio(false,QUrl(url));
}else{
saveTracksAfterBuffer = settingsObj.value("saveAfterBuffer","true").toBool();
radio_manager->playRadio(saveTracksAfterBuffer,QUrl(url));
//do not save tracks if playing from similar tracks list
if(list->objectName()=="recommListWidget"){
radio_manager->playRadio(false,QUrl(url));
}else{
radio_manager->playRadio(saveTracksAfterBuffer,QUrl(url));
}
}

//update metadata of MPRIS interface
Expand Down Expand Up @@ -3926,3 +3989,8 @@ void MainWindow::on_showSimilarList_clicked()
ui->recommWidget->isVisible() ? ui->showSimilarList->setIcon(QIcon(":/icons/sidebar/hideRecommend.png")):
ui->showSimilarList->setIcon(QIcon(":/icons/sidebar/showRecommend.png"));
}

//add playlist from web interface
void MainWindow::addPlaylistByData(QString data){
similarTracks->addPlaylist(data);
}
2 changes: 2 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class MainWindow : public QMainWindow
Q_INVOKABLE void delete_song_cache(QVariant track_id);
Q_INVOKABLE void remove_song(QVariant track_id);
Q_INVOKABLE void addToSimilarTracksQueue(const QVariant Base64andDominantColor);
Q_INVOKABLE void addPlaylistByData(QString data);

QString youtubeSearchTerm;
bool saveTracksAfterBuffer;
Expand Down Expand Up @@ -231,6 +232,7 @@ private slots:
void showRecommendedTrackOption();
void showPayPalDonationMessageBox();
void prepareSimilarTracks();
void similarTracksProcessHelper();
private:
QStringList currentSimilarTrackMeta ,currentSimilarTrackList;
int currentSimilarTrackProcessing = 0;
Expand Down
17 changes: 7 additions & 10 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ padding-left: 10px;</string>
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<property name="elideMode">
<enum>Qt::ElideNone</enum>
Expand Down Expand Up @@ -1477,6 +1477,9 @@ QScrollBar:vertical {
<property name="autoScrollMargin">
<number>5</number>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::NoDragDrop</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
Expand Down Expand Up @@ -1629,15 +1632,6 @@ QScrollBar:vertical {
<property name="autoScrollMargin">
<number>5</number>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
Expand Down Expand Up @@ -1958,6 +1952,9 @@ QScrollBar:vertical {
<property name="autoScrollMargin">
<number>5</number>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::NoDragDrop</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
Expand Down
23 changes: 22 additions & 1 deletion similartracks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

SimilarTracks::SimilarTracks(QObject *parent,int limit) : QObject(parent)
{

numberOfSimilarTracksToLoad = limit;
}

void SimilarTracks::addSimilarTracks(QString video_id){

isLoadingPLaylist = false;
QNetworkAccessManager *m_netwManager = new QNetworkAccessManager(this);
connect(m_netwManager,&QNetworkAccessManager::finished,[=](QNetworkReply* rep){
if(rep->error() == QNetworkReply::NoError){
Expand Down Expand Up @@ -46,3 +47,23 @@ void SimilarTracks::addSimilarTracks(QString video_id){
QNetworkRequest request(url);
m_netwManager->get(request);
}

void SimilarTracks::addPlaylist(QString data){
isLoadingPLaylist = true;

emit clearList();

QStringList list = data.split("gettrackinfo(");
list.removeFirst();

QStringList finalList;
foreach (QString str, list) {
finalList.append(QString(str.split(");\">").first()).remove("&quot;"));
}
if(finalList.count()>0){
emit setPlaylist(finalList);
}else{
emit failedGetSimilarTracks();
}
}

4 changes: 4 additions & 0 deletions similartracks.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ class SimilarTracks : public QObject
public:
explicit SimilarTracks(QObject *parent = nullptr, int limit = 5);
int numberOfSimilarTracksToLoad;
bool isLoadingPLaylist = false;
private:
QStringList playedTracksIds;

signals:
void setSimilarTracks(QStringList);
void setPlaylist(QStringList);
void failedGetSimilarTracks();
void clearList();

public slots:
void addSimilarTracks(QString video_id);
void addPlaylist(QString data);
};

#endif // SIMILARTRACKS_H

0 comments on commit e5c5cd5

Please sign in to comment.