IF.Lastfm/IF.Lastfm.Console/Program.cs

85 lines
2.6 KiB
C#
Raw Normal View History

2013-06-08 18:49:21 +01:00
using System;
using System.Collections.Generic;
using System.IO;
2013-06-08 18:49:21 +01:00
using System.Linq;
using System.Net;
2013-06-08 18:49:21 +01:00
using System.Text;
using System.Threading.Tasks;
2013-06-08 18:49:21 +01:00
using IF.Lastfm.Core;
using IF.Lastfm.Core.Api;
using IF.Lastfm.Core.Api.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2013-06-08 18:49:21 +01:00
namespace IF.Lastfm.Console
{
internal class Program
2013-06-08 18:49:21 +01:00
{
private static string _apiKey;
private static string _apiSecret;
private static string _username;
private static string _pass;
2013-06-08 18:49:21 +01:00
private static void Main(string[] args)
2013-06-08 18:49:21 +01:00
{
try
{
Task.Run(async () => await Run()).Wait();
}
catch (AggregateException agg)
{
foreach (var ex in agg.InnerExceptions)
{
System.Console.WriteLine("\n====================\n");
if (ex is LastFmApiException)
{
var lex = ex as LastFmApiException;
System.Console.WriteLine("LastFmApiException thrown:\n {0}\n {1}",
lex.Error.GetApiName(),
lex.StackTrace);
}
else
{
System.Console.WriteLine("Exception thrown:\n {0}\n {1}",
ex.Message,
ex.StackTrace);
}
System.Console.WriteLine("\n====================\n");
}
}
System.Console.ReadLine();
}
public static async Task Run()
{
await LoadSession();
var auth = new Auth(_apiKey, _apiSecret);
await auth.GetSessionTokenAsync(_username, _pass);
var albumApi = new AlbumApi(auth);
var album = await albumApi.GetAlbumInfoAsync("Grimes", "Visions", false);
}
private async static Task LoadSession()
{
const string path = @"C:\lastfm-wp-config.json";
2013-06-08 18:49:21 +01:00
string json;
using (var reader = new StreamReader(path))
{
json = await reader.ReadToEndAsync();
}
var jo = JsonConvert.DeserializeObject<JToken>(json);
_apiKey = jo.Value<string>("apikey");
_apiSecret = jo.Value<string>("apisecret");
_username = jo.Value<string>("username");
_pass = jo.Value<string>("pass");
2013-06-08 18:49:21 +01:00
}
}
}