Skip to content

Commit

Permalink
cddb and mb methods
Browse files Browse the repository at this point in the history
  • Loading branch information
squawkcpp committed Aug 13, 2018
1 parent 130e762 commit b7faa86
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 67 deletions.
21 changes: 21 additions & 0 deletions av/averrc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ namespace av {

const char* av_category_t::name() const noexcept { return "av"; }
std::error_condition av_category_t::default_error_condition ( int ev ) const noexcept {
if ( ev == static_cast< int > ( MB_DISCID_NO_MATCH ) )
{ return std::error_condition ( MB_DISCID_NO_MATCH ); }

if ( ev == static_cast< int > ( MB_DISCID_NOT_FOUND ) )
{ return std::error_condition ( MB_DISCID_NOT_FOUND ); }

if ( ev == static_cast< int > ( MB_INVALID_TOC ) )
{ return std::error_condition ( MB_INVALID_TOC ); }

if ( ev == static_cast< int > ( DISCID_RESULT_EMPTY ) )
{ return std::error_condition ( DISCID_RESULT_EMPTY ); }

Expand Down Expand Up @@ -114,6 +123,18 @@ bool av_category_t::equivalent ( const std::error_code& code, int condition ) co
static_cast< int > ( default_error_condition ( code.value() ).value() ) == condition;
}
std::string av_category_t::message ( int ev ) const {
if ( ev == CDDB_NO_MATCH )
{return "No match found"; }

if ( ev == MB_DISCID_NOT_FOUND )
{return "discid can not be found in musicbrainz database."; }

if ( ev == MB_DISCID_NO_MATCH )
{return "no match in musicbrainz discid results."; }

if ( ev == MB_INVALID_TOC )
{return "musicbrainz error: incvalid toc."; }

static char error_buffer[255];
av_strerror ( ev, error_buffer, sizeof ( error_buffer ) );
return error_buffer;
Expand Down
8 changes: 7 additions & 1 deletion av/averrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ namespace av {
enum av_errc {
UNKNOWN,
DISCID_RESULT_EMPTY,
NOT_FOUND,
MB_DISCID_NOT_FOUND,
MB_DISCID_NO_MATCH,
CDDB_NO_MATCH = 202,
MB_INVALID_TOC = 400,
NOT_FOUND,
AV_BSF_NOT_FOUND = AV_FFERRTAG ( 0xF8,'B','S','F' ), ///< Bitstream filter not found
AV_BUG = AV_FFERRTAG ( 'B','U','G','!' ), ///< Internal bug, also see AVERROR_BUG2
AV_BUFFER_TOO_SMALL = AV_FFERRTAG ( 'B','U','F','S' ), ///< Buffer too small
Expand Down Expand Up @@ -81,6 +84,9 @@ inline std::error_code make_error_code ( int error ) {
switch ( error ) {
case UNKNOWN:
case DISCID_RESULT_EMPTY:
case MB_DISCID_NOT_FOUND:
case MB_DISCID_NO_MATCH:
case MB_INVALID_TOC:
case NOT_FOUND:
case CDDB_NO_MATCH:
case AV_BSF_NOT_FOUND:
Expand Down
2 changes: 1 addition & 1 deletion av/discid/cddb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ std::error_code parse_discid ( const std::string& body, discid::release_t& targe
}
}

if ( _status != 200 && _status != 211 )
if ( _status != 200 && _status != 200 && _status != 211 )
{return av::make_error_code ( _status );}

return std::error_code();
Expand Down
85 changes: 75 additions & 10 deletions av/discid/discid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,28 @@ std::error_code get ( const std::string& uri, std::stringstream& ss ) {
return _errc;
}

std::error_code mb ( const toc_t& discinfo, release_t& target, const std::string& musicbrainz ) {
std::error_code _errc;

const std::string _discid_url = mb::url ( discinfo, musicbrainz );
std::stringstream _ss;
bool match_album ( const std::string& album, const release& release ) {

// # ifdef DEBUG
std::cout << _discid_url << std::endl;
// # endif
auto _release_title = release.title;
std::transform ( _release_title.begin(), _release_title.end(), _release_title.begin(), ::tolower );

if ( ! ( _errc = get ( _discid_url, _ss ) ) )
{ _errc = mb::parse_discid ( _ss.str(), target ); }
auto _album = album;
std::transform ( _album.begin(), _album.end(), _album.begin(), ::tolower );

return _errc;

if ( _album == _release_title )
{ return true; }

if ( _release_title.size() >= _album.size() ) {
const auto _temp_title = _release_title.substr ( _release_title.size() - _album.size() );

if ( _temp_title == _album )
{ return true; }

}

return false;
}

std::error_code mb ( const std::string& mbid, toc_t& target, const std::string& musicbrainz ) {
Expand All @@ -175,6 +183,43 @@ std::error_code mb ( const std::string& mbid, toc_t& target, const std::string&
return _errc;
}

std::error_code mb ( const toc_t& discinfo, toc_t& target, std::function< bool ( const release& ) > comperator, const std::string& musicbrainz ) {
std::error_code _errc;
release_t _release_toc;

if ( ! ( _errc = mb ( discinfo, _release_toc, musicbrainz ) ) ) {
if ( ! _release_toc.empty() ) {
for ( auto& __release : _release_toc ) {
if ( comperator ( __release ) ) {
_errc = discid::mb ( __release.mbid, target, musicbrainz );
return _errc;
}
}

_errc = av::make_error_code ( av::MB_DISCID_NO_MATCH );

} else { _errc = av::make_error_code ( av::MB_DISCID_NOT_FOUND ); }
}

return _errc;
}

std::error_code mb ( const toc_t& discinfo, release_t& target, const std::string& musicbrainz ) {
std::error_code _errc;

const std::string _discid_url = mb::url ( discinfo, musicbrainz );
std::stringstream _ss;

// # ifdef DEBUG
std::cout << _discid_url << std::endl;
// # endif

if ( ! ( _errc = get ( _discid_url, _ss ) ) )
{ _errc = mb::parse_discid ( _ss.str(), target ); }

return _errc;
}

std::error_code cddb ( const toc_t& discinfo, release_t& target, const std::string& freedb ) {

std::error_code _errc;
Expand Down Expand Up @@ -216,4 +261,24 @@ std::error_code cddb ( const std::string& category, const std::string& id, disci
return _errc;
}

std::error_code cddb ( const toc_t& discinfo, toc_t& target, std::function< bool ( const release& ) > comperator, const std::string& freedb ) {
std::error_code _errc;
release_t _release_toc;

if ( ! ( _errc = discid::cddb ( discinfo, _release_toc, freedb ) ) ) {
if ( ! _release_toc.empty() ) {
for ( auto& __release : _release_toc ) {
if ( comperator ( __release ) ) {
_errc = discid::cddb ( __release.category, __release.mbid, target, freedb );
return _errc;
}
}

_errc = av::make_error_code ( av::MB_DISCID_NO_MATCH );

} else { _errc = av::make_error_code ( av::CDDB_NO_MATCH ); }
}

return _errc;
}
}//namespace discid
32 changes: 26 additions & 6 deletions av/discid/discid.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef DISCID_H
#define DISCID_H

#include <functional>
#include <string>
#include <vector>
#include <system_error>
Expand Down Expand Up @@ -99,21 +100,25 @@ std::ostream& operator<< (
/** The target output stream. */ std::ostream& stream,
/** The release to print */ release_t& r );


bool match_album ( const std::string& album, const release& release );

/** @brief get the content from the webserver */
std::error_code get (
/** complete uri of the request */ const std::string& uri,
/** buffer to store the body */ std::stringstream& ss );


/** @brief busicbrainz url from cdripper logfile.
<a href="https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2#discid">
metabrainz lookup with discid.
</a>
* @return error_code.
*/
std::error_code mb (
/** discinfo album table of content */ const toc_t& discinfo,
/** target release info */ release_t& target,
/** musicbrainz url */ const std::string& musicbrainz = "http://musicbrainz.org/ws/2" );
/** discinfo album table of content */ const toc_t& discinfo,
/** target release info */ release_t& target,
/** musicbrainz url */ const std::string& musicbrainz = "http://musicbrainz.org/ws/2" );

/** @brief busicbrainz url from cdripper logfile.
<a href="https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2#discid">
Expand All @@ -122,9 +127,17 @@ std::error_code mb (
* @return error code.
*/
std::error_code mb (
/** musicbrainz release id */ const std::string& mbid,
/** result toc */ toc_t& target,
/** musicbrainz url */ const std::string& musicbrainz = "http://musicbrainz.org/ws/2" );
/** musicbrainz release id */ const std::string& mbid,
/** result toc */ toc_t& target,
/** musicbrainz url */ const std::string& musicbrainz = "http://musicbrainz.org/ws/2" );

/** @brief get the metadata from musicbrainz. */
std::error_code mb (
/** discinfo album table of content */ const toc_t& discinfo,
/** target toc info */ toc_t& target,
/** comperator for the lookup result selection */ std::function< bool ( const release& ) > comperator,
/** musicbrainz url */ const std::string& musicbrainz = "http://musicbrainz.org/ws/2" );


/** @brief freedb url from table of content.
@return error code. */
Expand All @@ -140,5 +153,12 @@ std::error_code cddb (
/** cddb id */ const std::string& id,
/** target release info */ discid::toc_t& discinfo,
/** freedb url */ const std::string& freedb = "http://freedb.freedb.org/~cddb/cddb.cgi" );

/** @brief get the metadata from musicbrainz. */
std::error_code cddb (
/** discinfo album table of content */ const toc_t& discinfo,
/** target toc info */ toc_t& target,
/** comperator for the lookup result selection */ std::function< bool ( const release& ) > comperator,
/** musicbrainz url */ const std::string& freedb = "http://freedb.freedb.org/~cddb/cddb.cgi" );
}//namespace discid
#endif //DISCID_H
8 changes: 4 additions & 4 deletions av/discid/musicbrainz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ inline std::error_code parse_release ( json& __release, discid::release_t& targe

target.push_back ( {_release_id, _release_title } );

} else { return av::make_error_code ( 404 ); }
} else { std::cout << "tag and/or title not set" << std::endl; }

return std::error_code();
}
Expand Down Expand Up @@ -101,15 +101,15 @@ std::error_code parse_release ( const std::string& body, discid::toc_t& target )
_toc.artists.push_back ( { _artist_id, _artist_name, _artist_sort } );
}

} else { return av::make_error_code ( 404 ); } //TODO
} else { std::cout << "no artists found" << std::endl; }

target.push_back ( _toc );
}

} else { return av::make_error_code ( 404 ); } //TODO
} else { std::cout << "no tracks found" << std::endl; }
}

} else { return av::make_error_code ( 404 ); } //TODO
} else { std::cout << "no media found" << std::endl; }

return _errc;
}
Expand Down
4 changes: 2 additions & 2 deletions av/discid/toc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ toc_t parse_cuesheet (
_toclist.back().end = time ( _toclist.back().end_sector ) - _toclist.back().start;
}

} else if ( !files.empty() && exists ( path + files.back() ) ) {
} else if ( !files.empty() && exists ( path + "/" + files.back() ) ) {

av::Format format ( path + files.back() );
av::Format format ( path + "/" + files.back() );

if ( !format ) {
_playtime = static_cast< toc_time_t > ( format.playtime() );
Expand Down
Loading

0 comments on commit b7faa86

Please sign in to comment.