Skip to content

Commit b1bd6ac

Browse files
committed
ニコ生で未ログインの時の処理を追加
1 parent cb8e396 commit b1bd6ac

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

NicoSitePlugin2/Api.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public ApiGetCommunityLivesException()
1515

1616
}
1717
}
18+
class NotLoggedInException : Exception { }
1819
class UserInfo
1920
{
2021
public string Nickname { get; set; }
@@ -53,12 +54,12 @@ public static async Task<CommunityLiveInfo[]> GetCommunityLives(IDataSource serv
5354
var url = $"https://com.nicovideo.jp/api/v1/communities/{communityIdWithoutCo}/lives.json";
5455
var res = await server.GetAsync(url, cc);
5556
dynamic d = JsonConvert.DeserializeObject(res);
56-
if(d.meta.status != 200)
57+
if (d.meta.status != 200)
5758
{
5859
throw new ApiGetCommunityLivesException();
5960
}
6061
var lives = new List<CommunityLiveInfo>();
61-
foreach(var liveDyn in d.data.lives)
62+
foreach (var liveDyn in d.data.lives)
6263
{
6364
var live = new CommunityLiveInfo
6465
{
@@ -82,14 +83,31 @@ public class CommunityLiveInfo
8283
public long UserId { get; set; }
8384
public string WatchUrl { get; set; }
8485
}
86+
/// <summary>
87+
///
88+
/// </summary>
89+
/// <param name="server"></param>
90+
/// <param name="cc"></param>
91+
/// <returns></returns>
92+
/// <exception cref="NotLoggedInException">未ログインの場合</exception>
93+
/// <exception cref="SpecChangedException"></exception>
8594
public static async Task<MyInfo> GetMyInfo(IDataSource server, CookieContainer cc)
8695
{
8796
var url = "https://www.nicovideo.jp/my";
8897
var html = await server.GetAsync(url, cc);
8998
var match = Regex.Match(html, "data-common-header=\"(.+)\"></div>");
9099
if (!match.Success)
91100
{
92-
throw new SpecChangedException(html);
101+
var matchHtmlTitle = Regex.Match(html, "<title>([^<]*)</title>");
102+
var title = matchHtmlTitle.Groups[1].Value;
103+
if (matchHtmlTitle.Success && title.Contains("ログイン"))
104+
{
105+
throw new NotLoggedInException();
106+
}
107+
else
108+
{
109+
throw new SpecChangedException(html);
110+
}
93111
}
94112
var json = match.Groups[1].Value.Replace("&quot;", "\"");
95113
dynamic d = JsonConvert.DeserializeObject(json);

NicoSitePlugin2/TestCommentProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,15 @@ public override void Disconnect()
571571
public override async Task<ICurrentUserInfo> GetCurrentUserInfo(IBrowserProfile browserProfile)
572572
{
573573
var cc = GetCookieContainer(browserProfile);
574-
var myInfo = await Api.GetMyInfo(_server, cc);
575-
return await Task.FromResult(new CurrentUserInfo { Username = myInfo.Nickname, IsLoggedIn = myInfo.IsLogin });
574+
try
575+
{
576+
var myInfo = await Api.GetMyInfo(_server, cc);
577+
return await Task.FromResult(new CurrentUserInfo { Username = myInfo.Nickname, IsLoggedIn = myInfo.IsLogin });
578+
}
579+
catch (NotLoggedInException)
580+
{
581+
return await Task.FromResult(new CurrentUserInfo { Username = "(未ログイン)", IsLoggedIn = false });
582+
}
576583
}
577584
class CurrentUserInfo : ICurrentUserInfo
578585
{

0 commit comments

Comments
 (0)