-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
131350c
commit a288edd
Showing
13 changed files
with
237 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ public enum CommandLineOptionType | |
{ | ||
Unknown, | ||
Lookup, | ||
Import, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/MusicCatalogue.Entities/Exceptions/MultipleOperationsException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.Serialization; | ||
|
||
namespace MusicCatalogue.Entities.Exceptions | ||
{ | ||
[Serializable] | ||
[ExcludeFromCodeCoverage] | ||
public class MultipleOperationsException : Exception | ||
{ | ||
public MultipleOperationsException() | ||
{ | ||
} | ||
|
||
public MultipleOperationsException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public MultipleOperationsException(string message, Exception inner) : base(message, inner) | ||
{ | ||
} | ||
|
||
protected MultipleOperationsException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext) | ||
{ | ||
} | ||
|
||
public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
base.GetObjectData(info, context); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
namespace MusicCatalogue.Entities.Interfaces | ||
using MusicCatalogue.Entities.DataExchange; | ||
|
||
namespace MusicCatalogue.Entities.Interfaces | ||
{ | ||
public interface ICsvImporter | ||
{ | ||
event EventHandler<TrackDataExchangeEventArgs>? TrackImport; | ||
Task Import(string file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using MusicCatalogue.Entities.Config; | ||
using MusicCatalogue.Entities.Interfaces; | ||
using MusicCatalogue.Logic.Api; | ||
using MusicCatalogue.Logic.Api.TheAudioDB; | ||
using MusicCatalogue.Logic.Collection; | ||
using MusicCatalogue.Logic.Database; | ||
|
||
namespace MusicCatalogue.LookupTool.Logic | ||
{ | ||
internal class AlbumLookup | ||
{ | ||
private readonly IMusicLogger _logger; | ||
private readonly MusicApplicationSettings _settings; | ||
private readonly IMusicCatalogueFactory _factory; | ||
|
||
public AlbumLookup(IMusicLogger logger, IMusicCatalogueFactory factory, MusicApplicationSettings settings) | ||
{ | ||
_logger = logger; | ||
_settings = settings; | ||
_factory = factory; | ||
} | ||
|
||
/// <summary> | ||
/// Lookup an album given the artist name and album title | ||
/// </summary> | ||
/// <param name="artistName"></param> | ||
/// <param name="albumTitle"></param> | ||
public async Task LookupAlbum(string artistName, string albumTitle) | ||
{ | ||
// Get the API key and the URLs for the album and track lookup endpoints | ||
var key = _settings!.ApiServiceKeys.Find(x => x.Service == ApiServiceType.TheAudioDB)!.Key; | ||
var albumsEndpoint = _settings.ApiEndpoints.Find(x => x.EndpointType == ApiEndpointType.Albums)!.Url; | ||
var tracksEndpoint = _settings.ApiEndpoints.Find(x => x.EndpointType == ApiEndpointType.Tracks)!.Url; | ||
|
||
// Convert the URL into a URI instance that will expose the host name - this is needed | ||
// to set up the client headers | ||
var uri = new Uri(albumsEndpoint); | ||
|
||
// Configure an HTTP client | ||
var client = MusicHttpClient.Instance; | ||
client.AddHeader("X-RapidAPI-Key", key); | ||
client.AddHeader("X-RapidAPI-Host", uri.Host); | ||
|
||
// Configure the APIs | ||
var albumsApi = new TheAudioDBAlbumsApi(_logger, client, albumsEndpoint); | ||
var tracksApi = new TheAudioDBTracksApi(_logger, client, tracksEndpoint); | ||
var lookupManager = new AlbumLookupManager(_logger, albumsApi, tracksApi, _factory); | ||
|
||
// Lookup the album and its tracks | ||
var album = await lookupManager.LookupAlbum(artistName, albumTitle); | ||
if (album != null) | ||
{ | ||
// Dump the album details | ||
Console.WriteLine($"Title: {album.Title}"); | ||
Console.WriteLine($"Artist: {StringCleaner.Clean(artistName)}"); | ||
Console.WriteLine($"Released: {album.Released}"); | ||
Console.WriteLine($"Genre: {album.Genre}"); | ||
Console.WriteLine($"Cover: {album.CoverUrl}"); | ||
Console.WriteLine(); | ||
|
||
// Dump the track list | ||
if ((album.Tracks != null) && (album.Tracks.Count > 0)) | ||
{ | ||
foreach (var track in album.Tracks) | ||
{ | ||
Console.WriteLine($"{track.Number} : {track.Title}, {track.FormattedDuration()}"); | ||
} | ||
Console.WriteLine(); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("No tracks found"); | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Album details not found"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using MusicCatalogue.Entities.DataExchange; | ||
using MusicCatalogue.Entities.Interfaces; | ||
using MusicCatalogue.Entities.Logging; | ||
|
||
namespace MusicCatalogue.LookupTool.Logic | ||
{ | ||
internal class DataImport | ||
{ | ||
private readonly IMusicLogger _logger; | ||
private readonly IMusicCatalogueFactory _factory; | ||
|
||
public DataImport(IMusicLogger logger, IMusicCatalogueFactory factory) | ||
{ | ||
_logger = logger; | ||
_factory = factory; | ||
} | ||
|
||
/// <summary> | ||
/// Import the data held in the specified CSV file | ||
/// </summary> | ||
/// <param name="albumName"></param> | ||
public void Import(string file) | ||
{ | ||
_logger.LogMessage(Severity.Info, $"Importing {file} ..."); | ||
|
||
try | ||
{ | ||
// Register a handler for the "track imported" event and import the file | ||
_factory.Importer.TrackImport += OnTrackImported; | ||
_factory.Importer.Import(file); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Import error: {ex.Message}"); | ||
_logger.LogMessage(Severity.Info, $"Import error: {ex.Message}"); | ||
_logger.LogException(ex); | ||
} | ||
finally | ||
{ | ||
// Un-register the event handler | ||
_factory.Importer.TrackImport -= OnTrackImported; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Handler called when a track is imported | ||
/// </summary> | ||
/// <param name="sender"></param> | ||
/// <param name="e"></param> | ||
public void OnTrackImported(object? sender, TrackDataExchangeEventArgs e) | ||
{ | ||
if (e.Track != null) | ||
{ | ||
Console.WriteLine($"Imported {e.Track.ArtistName} {e.Track.AlbumTitle} {e.Track.TrackNumber} {e.Track.Title}"); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.