1
0
mirror of synced 2024-11-24 15:40:22 +01:00

List song titles from CDN

This commit is contained in:
0aubsq 2022-05-05 09:51:22 +02:00
parent ace6df9dfe
commit 2aa0829bfc
3 changed files with 128 additions and 28 deletions

View File

@ -5,28 +5,84 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net.Http; using System.Net.Http;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace TJAPlayer3 namespace TJAPlayer3
{ {
internal class API internal class API
{ {
public API(int selectedCDN)
public class APISongData
{ {
public string SongTitle;
}
#region [ContractResolver override for properties]
public class SongContractResolver : DefaultContractResolver
{
private Dictionary<string, string> PropertyMappings { get; set; }
public SongContractResolver(DBCDN.CDNData cdnData)
{
this.PropertyMappings = new Dictionary<string, string>
{
{"SongTitle", cdnData.Hooks.title["default"]},
};
}
protected override string ResolvePropertyName(string propertyName)
{
string resolvedName = null;
var resolved = this.PropertyMappings.TryGetValue(propertyName, out resolvedName);
return (resolved) ? resolvedName : base.ResolvePropertyName(propertyName);
}
}
#endregion
public API(DBCDN.CDNData selectedCDN)
{
cdnData = selectedCDN;
FetchedSongsList = new APISongData[0];
}
public APISongData[] FetchedSongsList;
public void tLoadSongsFromInternalCDN()
{
string url = cdnData.BaseUrl + cdnData.SongList;
var _fetched = GetCallAPI(url);
_fetched.Wait();
if (_fetched.Result != null)
FetchedSongsList = _fetched.Result;
} }
public async Task<object> PostCallAPI(string url, object jsonObject) #region [private]
private DBCDN.CDNData cdnData;
private async Task<APISongData[]> GetCallAPI(string url)
{ {
try try
{ {
using (HttpClient client = new HttpClient()) using (HttpClient client = new HttpClient())
{ {
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json"); var response = await client.GetAsync(url).ConfigureAwait(false);
var response = await client.PostAsync(url, content);
if (response != null) if (response != null)
{ {
var jsonString = await response.Content.ReadAsStringAsync(); var jsonString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<object>(jsonString);
var settings = new JsonSerializerSettings();
settings.ContractResolver = new SongContractResolver(cdnData);
return JsonConvert.DeserializeObject<APISongData[]>(jsonString, settings);
} }
} }
} }
@ -37,26 +93,8 @@ namespace TJAPlayer3
return null; return null;
} }
public async Task<object> GetCallAPI(string url)
{
try
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(url);
if (response != null)
{
var jsonString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<object>(jsonString);
}
}
}
catch (Exception e)
{
} #endregion
return null;
}
} }
} }

View File

@ -174,6 +174,39 @@ namespace TJAPlayer3
#endregion #endregion
#region [Song list menu]
if (currentMenu == ECurrentMenu.CDN_SONGS)
{
_ref = this.ttkCDNSongList;
_selector = cdnSongListIndex;
int baseY = 360;
for (int i = -4; i < 4; i++)
{
int pos = (_ref.Length * 5 + _selector + i) % _ref.Length;
CTexture tmpTex = TJAPlayer3.stage選曲.act曲リスト.ResolveTitleTexture(_ref[pos]);
if (_selector != pos)
{
tmpTex.color4 = C変換.ColorToColor4(Color.DarkGray);
TJAPlayer3.Tx.OnlineLounge_Song_Box?.tUpdateColor4(C変換.ColorToColor4(Color.DarkGray));
}
else
{
tmpTex.color4 = C変換.ColorToColor4(Color.White);
TJAPlayer3.Tx.OnlineLounge_Song_Box?.tUpdateColor4(C変換.ColorToColor4(Color.White));
}
TJAPlayer3.Tx.OnlineLounge_Song_Box?.t2D拡大率考慮上中央基準描画(TJAPlayer3.app.Device, 350, baseY + 100 * i);
tmpTex.t2D拡大率考慮上中央基準描画(TJAPlayer3.app.Device, 350, baseY + 18 + 100 * i);
}
}
#endregion
#endregion #endregion
@ -284,6 +317,26 @@ namespace TJAPlayer3
TJAPlayer3.Skin.sound取消音.t再生する(); TJAPlayer3.Skin.sound取消音.t再生する();
else else
{ {
if (currentMenu == ECurrentMenu.CDN_SONGS)
{
apiMethods = new API(dbCDNData);
apiMethods.tLoadSongsFromInternalCDN();
#region [Generate song list values]
this.ttkCDNSongList = new TitleTextureKey[apiMethods.FetchedSongsList.Length + 1];
this.ttkCDNSongList[0] = new TitleTextureKey(CLangManager.LangInstance.GetString(401), this.pfOLFont, Color.White, Color.DarkRed, 1000);
for (int i = 0; i < apiMethods.FetchedSongsList.Length; i++)
{
this.ttkCDNSongList[i + 1] = new TitleTextureKey(apiMethods.FetchedSongsList[i].SongTitle, this.pfOLFont, Color.White, Color.DarkRed, 1000);
}
this.cdnSongListIndex = 0;
#endregion
}
TJAPlayer3.Skin.sound決定音.t再生する(); TJAPlayer3.Skin.sound決定音.t再生する();
} }
@ -338,6 +391,10 @@ namespace TJAPlayer3
cdnOptMenuIndex += val; cdnOptMenuIndex += val;
} }
else if (currentMenu == ECurrentMenu.CDN_SONGS)
{
cdnSongListIndex = (ttkCDNSongList.Length + cdnSongListIndex + val) % ttkCDNSongList.Length;
}
return true; return true;
} }
@ -376,6 +433,7 @@ namespace TJAPlayer3
private DBCDN dbCDN; private DBCDN dbCDN;
private DBCDN.CDNData dbCDNData; private DBCDN.CDNData dbCDNData;
private API apiMethods;
// Main Menu // Main Menu
private TitleTextureKey[] ttkMainMenuOpt; private TitleTextureKey[] ttkMainMenuOpt;
@ -391,6 +449,10 @@ namespace TJAPlayer3
private ECurrentMenu[] cdnOptMenu; private ECurrentMenu[] cdnOptMenu;
private int cdnOptMenuIndex; private int cdnOptMenuIndex;
// CDN List songs option
private TitleTextureKey[] ttkCDNSongList;
private int cdnSongListIndex;
private class CMenuInfo private class CMenuInfo
{ {
public CMenuInfo(string ttl) public CMenuInfo(string ttl)

View File

@ -1,6 +1,6 @@
{ {
"Pidgey's TJADB CDN" : { "Pidgey's TJADB CDN" : {
"baseUrl": "https://dev.pidgey.net:8443/", "baseUrl": "https://dev.pidgey.net/",
"download": { "download": {
"en": "download/en/", "en": "download/en/",
"default": "download/orig/", "default": "download/orig/",