Skip to content

Commit

Permalink
more small refactorring
Browse files Browse the repository at this point in the history
  • Loading branch information
aliakseis committed Oct 23, 2019
1 parent b02c4fc commit 6cb84ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
22 changes: 8 additions & 14 deletions Player/PlayerDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,16 @@ bool CPlayerDoc::openUrl(const std::string& originalUrl)
m_url = url;
m_subtitles.reset();
m_nightcore = false;
auto transcripts = getYoutubeTranscripts(originalUrl);
if (!transcripts.empty())
auto map(std::make_unique<SubtitlesMap>());
if (getYoutubeTranscripts(originalUrl,
[&map](double start, double duration, const std::string& text) {
map->add({
boost::icl::interval<double>::closed(start, start + duration),
boost::algorithm::trim_copy(text) + '\n' });
}))
{
// TODO refactor
m_unicodeSubtitles = true;
auto map(std::make_unique<SubtitlesMap>());
for (const auto& v : transcripts)
{
map->add({
boost::icl::interval<double>::closed(v.start, v.start + v.duration),
boost::algorithm::trim_copy(v.text) + '\n' });
}
if (!map->empty())
{
m_subtitles = std::move(map);
}
m_subtitles = std::move(map);
}
m_frameDecoder->pauseResume();
onPauseResume(false);
Expand Down
24 changes: 11 additions & 13 deletions Player/YouTuber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class YouTubeTranscriptDealer
~YouTubeTranscriptDealer();

bool isValid() const { return !!m_obj; }
std::vector<TranscriptRecord> getYoutubeTranscripts(const std::string& id);
bool getYoutubeTranscripts(const std::string& id, AddYoutubeTranscriptCallback cb);

private:
boost::python::object m_obj;
Expand Down Expand Up @@ -477,24 +477,22 @@ YouTubeTranscriptDealer::~YouTubeTranscriptDealer()
}


std::vector<TranscriptRecord> YouTubeTranscriptDealer::getYoutubeTranscripts(const std::string& id)
bool YouTubeTranscriptDealer::getYoutubeTranscripts(const std::string& id, AddYoutubeTranscriptCallback cb)
{
BOOST_LOG_TRIVIAL(trace) << "getYoutubeTranscripts() id = \"" << id << "\"";
using namespace boost::python;
try
{
const auto v = m_obj(id);
std::vector<TranscriptRecord> result;
const auto length = len(v);
for (int i = 0; i < length; ++i)
{
const auto& el = v[i];
result.push_back({
extract<std::string>(el["text"]),
extract<double>(el["start"]),
extract<double>(el["duration"]) });
cb(extract<double>(el["start"]),
extract<double>(el["duration"]),
extract<std::string>(el["text"]));
}
return result;
return true;
}
catch (const std::exception& ex)
{
Expand All @@ -505,7 +503,7 @@ std::vector<TranscriptRecord> YouTubeTranscriptDealer::getYoutubeTranscripts(con
BOOST_LOG_TRIVIAL(error) << "getYoutubeTranscripts() error \"" << parse_python_exception() << "\"";
}

return{};
return false;
}


Expand Down Expand Up @@ -634,14 +632,14 @@ std::string getYoutubeUrl(std::string url)
return url;
}

std::vector<TranscriptRecord> getYoutubeTranscripts(std::string url)
bool getYoutubeTranscripts(std::string url, AddYoutubeTranscriptCallback cb)
{
if (extractYoutubeId(url))
{
CWaitCursor wait;
static YouTubeTranscriptDealer buddy;
if (buddy.isValid())
return buddy.getYoutubeTranscripts(url);
return buddy.getYoutubeTranscripts(url, cb);
}

return{};
Expand All @@ -664,9 +662,9 @@ std::string getYoutubeUrl(std::string url)
return url;
}

std::vector<TranscriptRecord> getYoutubeTranscripts(std::string url)
bool getYoutubeTranscripts(std::string, AddYoutubeTranscriptCallback)
{
return{};
return false;
}

#endif // YOUTUBE_EXPERIMENT
10 changes: 4 additions & 6 deletions Player/YouTuber.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <functional>
#include <vector>

std::vector<std::string> ParsePlaylist(std::string url, bool force);
Expand All @@ -9,10 +10,7 @@ std::vector<std::string> ParsePlaylistFile(const TCHAR* fileName);

std::string getYoutubeUrl(std::string url);

struct TranscriptRecord
{
std::string text;
double start, duration;
};
// start, duration, text
typedef std::function<void(double, double, const std::string&)> AddYoutubeTranscriptCallback;

std::vector<TranscriptRecord> getYoutubeTranscripts(std::string url);
bool getYoutubeTranscripts(std::string url, AddYoutubeTranscriptCallback cb);

0 comments on commit 6cb84ef

Please sign in to comment.