Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Camilo Hernández committed Mar 9, 2021
0 parents commit 5a53b1c
Show file tree
Hide file tree
Showing 27 changed files with 107,467 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Project exclude paths
/cmake-build-debug/CMakeFiles/
/cmake-build-release/CMakeFiles/
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.18)
project(hyitt)

set(CMAKE_CXX_STANDARD 14)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

include(FetchContent)
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/whoshuu/cpr.git GIT_TAG c34ddb9b3de2a22fdbd5d318d8b7d1997e6ca0bf)
FetchContent_MakeAvailable(cpr)

include_directories(include)
link_directories(lib)

add_executable(hyitt hyitt/main.cpp hyitt/video/video.h hyitt/video/video.cpp hyitt/video/quality.h hyitt/player/player.h hyitt/player/player.cpp hyitt/stream/audio_stream.cpp hyitt/stream/audio_stream.h hyitt/player/interface.cpp hyitt/player/interface.h hyitt/decoder/decoder.cpp hyitt/decoder/decoder.h include/Duktape/duktape.c hyitt/exceptions.h)

set(LINK_LIBS ${CONAN_LIBS} cpr::cpr basswebm bass)

target_link_libraries(${PROJECT_NAME} ${LINK_LIBS})
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2020 Camilo Hernández <me@camiloh.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Hear Youtube in the Terminal - HYITT
HYITT is a minimalistic application that allows you to stream Youtube audio directly to the console. Since just the audio and not the video gets streamed, it requires less network bandwidth. Perfect for low-connectivity or online gaming (less lag!).

The usage is pretty straightforward. Just call the executable passing a Youtube URL as the argument:
```bash
hyitt https://www.youtube.com/watch?v=LDU_Txk06tM
```
Optionally one of `--low`, `--medium`, or `--high` can be used to specify the preferred stream quality.
7 changes: 7 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[requires]
boost/1.75.0
libcurl/7.69.1
jsoncpp/1.9.4

[generators]
cmake
Binary file added dll/bass.dll
Binary file not shown.
Binary file added dll/basswebm.dll
Binary file not shown.
158 changes: 158 additions & 0 deletions hyitt/decoder/decoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//
// HYITT - Hear Youtube in the Terminal
// Created by Camilo Hernández and released under the MIT Licence
//

#include <iostream>
#include <regex>

#include "boost/algorithm/string.hpp"
#include "boost/algorithm/string_regex.hpp"
#include "boost/regex.h"
#include "Duktape/duktape.h"

#include "decoder.h"
#include "../exceptions.h"
#include "../video/video.h"

static const boost::regex FUNC_NAME_REGEX[] {
boost::regex(R"(\b[cs]\s*&&\s*[adf]\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?<sig>[a-zA-Z0-9$]+)\()"),
boost::regex(R"(\b[a-zA-Z0-9]+\s*&&\s*[a-zA-Z0-9]+\.set\([^,]+\s*,\s*encodeURIComponent\s*\(\s*(?<sig>[a-zA-Z0-9$]+)\()"),
boost::regex(R"(\bm=(?<sig>[a-zA-Z0-9$]{2})\(decodeURIComponent\(h\.s\)\))"),
boost::regex(R"(\bc&&\(c=(?<sig>[a-zA-Z0-9$]{2})\(decodeURIComponent\(c\)\))"),
boost::regex(R"((?:\b|[^a-zA-Z0-9$])(?<sig>[a-zA-Z0-9$]{2})\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\);[a-zA-Z0-9$]{2}\.[a-zA-Z0-9$]{2}\(a,\d+\))"),
boost::regex(R"((?:\b|[^a-zA-Z0-9$])(?<sig>[a-zA-Z0-9$]{2})\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\))"),
boost::regex(R"((?<sig>[a-zA-Z0-9$]+)\s*=\s*function\(\s*a\s*\)\s*{\s*a\s*=\s*a\.split\(\s*""\s*\))"),
};

Decoder::Decoder(const std::string& vidId) {
videoId = vidId;

playerUrl = getPlayerUrl();
playerJs = getPlayer();
sigFuncName = getSigFuncName();
sigFuncDef = findSigFunc();
helperFuncsDef = findHelperFuncs();
}

std::string Decoder::decode(const std::string &signatureCipher) {
duk_context *duk_ctx = duk_create_heap_default();

auto sigData = parseQueryData(signatureCipher);
std::string s = sigData["s"];
std::string sp = sigData["sp"];
std::string url = sigData["url"];

duk_eval_string(duk_ctx, jsCallString(s).c_str());
std::string res = duk_get_string(duk_ctx, -1);

duk_destroy_heap(duk_ctx);

url = curl_easy_unescape(curl_easy_init(), url.c_str(), 0, nullptr);
return url + "&" + sp + "=" + res;
}

std::string Decoder::getPlayerUrl() {
const std::string URL = "https://www.youtube.com/embed/" + videoId;

cpr::Session session;
session.SetVerifySsl(false);
session.SetUrl(URL);

cpr::Response r = session.Get();
if (r.status_code != 200) {
throw NetworkException(r.status_code);
}

std::string html = r.text;
std::string jsUrl = Decoder::getJsUrl(html);

return "https://www.youtube.com/" + jsUrl;
}

std::string Decoder::getJsUrl(const std::string& html) {
std::vector<std::string> urlStart;
boost::algorithm::split_regex(urlStart, html, boost::regex(R"("jsUrl":")")) ;
if (urlStart.size() < 2) {
throw SignatureException();
}

std::vector<std::string> url;
boost::split(url, urlStart[1].substr(1, urlStart[0].size()), boost::is_any_of("\""));
if (url.size() < 2){
throw SignatureException();
}

return url[0];
}

std::string Decoder::getPlayer() {
cpr::Session session;
session.SetVerifySsl(false);
session.SetUrl(playerUrl);

cpr::Response r = session.Get();
if (r.status_code != 200) {
throw NetworkException(r.status_code);
}

return r.text;
}

std::string Decoder::getSigFuncName() {
boost::smatch m;
for (auto &exp: FUNC_NAME_REGEX) {
if (boost::regex_search(playerJs, m, exp)) {
return m["sig"];
}
}

throw SignatureException();
}

std::string Decoder::findSigFunc() {
auto term = sigFuncName + R"(=function\(a\)\{)";

std::vector<std::string> sects;
boost::split_regex(sects, playerJs, boost::regex(term));
if (sects.size() < 2) {
throw SignatureException();
}

std::vector<std::string> forwardSects;
boost::split_regex(forwardSects, sects[1], boost::regex("\\};"));
if (forwardSects.size() < 2) {
throw SignatureException();
}

return sigFuncName + "=function(a){" + forwardSects[0] + "};";
}

std::string Decoder::findHelperFuncs() {
std::vector<std::string> sects;
boost::split_regex(sects, sigFuncDef, boost::regex(R"(a=a.split\(""\);)"));
if (sects.size() < 2) {
throw SignatureException();
}

auto varName = sects[1].substr(0, 2);
auto term = "var " + varName + "=\\{";

std::vector<std::string> declSects;
boost::split_regex(declSects, playerJs, boost::regex(term));
if (sects.size() < 2) {
throw SignatureException();
}

std::vector<std::string> forwardSects;
boost::split_regex(forwardSects, declSects[1], boost::regex("\\};"));
if (forwardSects.size() < 2) {
throw SignatureException();
}

return "var " + varName + "={" + forwardSects[0] + "};";
}

std::string Decoder::jsCallString(const std::string& s) {
return helperFuncsDef+sigFuncDef+sigFuncName+"(decodeURIComponent(\"" + s + "\"))";
}
40 changes: 40 additions & 0 deletions hyitt/decoder/decoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// HYITT - Hear Youtube in the Terminal
// Created by Camilo Hernández and released under the MIT Licence
//

#ifndef RUBEN_DECODER_H
#define RUBEN_DECODER_H

#include <string>

#include "cpr/cpr.h"
#include "boost/format.hpp"

class Decoder {
public:
explicit Decoder() = default;
explicit Decoder(const std::string& vidId);
std::string decode(const std::string& signatureCipher);

private:
std::string videoId;
std::string playerUrl;
std::string playerJs;
std::string sigFuncName;
std::string sigFuncDef;
std::string helperFuncsDef;

std::string getPlayerUrl();
static std::string getJsUrl(const std::string& html);
std::string getPlayer();

std::string getSigFuncName();
std::string findSigFunc();
std::string findHelperFuncs();

std::string jsCallString(const std::string& s);
};


#endif //RUBEN_DECODER_H
48 changes: 48 additions & 0 deletions hyitt/exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// HYITT - Hear Youtube in the Terminal
// Created by Camilo Hernández and released under the MIT Licence
//

#ifndef RUBEN_EXCEPTIONS_H
#define RUBEN_EXCEPTIONS_H

#include "boost/lexical_cast.hpp"

struct BassException : public std::exception
{
int err_code;

explicit BassException(int code) { err_code = code; }

std::string what() {
return "Unable to start audio playback (BASS ERR " + boost::lexical_cast<std::string>(err_code) + ")";
}
};

struct NoStreamsException : public std::exception
{
std::string what() {
return "No suitable streams found or invalid video";
}
};

struct SignatureException : public std::exception
{
std::string what() {
return "Unable to retrieve the video's signature data";
}
};

struct NetworkException : public std::exception
{
int err_code;

explicit NetworkException(int code) { err_code = code; }

std::string what() {
return "Unable to establish connection (HTTP ERR " + boost::lexical_cast<std::string>(err_code) + ")";
}
};


#endif //RUBEN_EXCEPTIONS_H
Loading

0 comments on commit 5a53b1c

Please sign in to comment.