-
Notifications
You must be signed in to change notification settings - Fork 20
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
8e738e8
commit ccecc42
Showing
8 changed files
with
234 additions
and
145 deletions.
There are no files selected for viewing
106 changes: 57 additions & 49 deletions
106
Theresa3rd-Bot/TheresaBot.Main/Controller/CookieController.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 |
---|---|---|
@@ -1,79 +1,87 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using TheresaBot.Main.Common; | ||
using TheresaBot.Main.Model.Result; | ||
using TheresaBot.Main.Datas; | ||
using TheresaBot.Main.Helper; | ||
using TheresaBot.Main.Model.PO; | ||
using TheresaBot.Main.Services; | ||
using TheresaBot.Main.Type; | ||
using Microsoft.AspNetCore.Authorization; | ||
using TheresaBot.Main.Exceptions; | ||
using TheresaBot.Main.Model.DTO; | ||
|
||
using TheresaBot.Main.Model.Result; | ||
using TheresaBot.Main.Services; | ||
|
||
namespace TheresaBot.Main.Controller | ||
{ | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class CookieController : ControllerBase | ||
public class CookieController : BaseController | ||
{ | ||
private WebsiteService websiteService; | ||
|
||
public CookieController() | ||
{ | ||
websiteService = new WebsiteService(); | ||
} | ||
[HttpPost] | ||
|
||
[HttpGet] | ||
[Authorize] | ||
[Route("update/pck")] | ||
public async Task<ApiResult> UpdatePixivCookie([FromBody] CookieDto pck) | ||
[Route("get/pixiv")] | ||
public ApiResult GetPixiv() | ||
{ | ||
ApiResult result = await UpdatePixivCookieAsync(pck.pcookie); | ||
return result; | ||
var data = new { cookie = WebsiteDatas.Pixiv?.Cookie ?? string.Empty }; | ||
return ApiResult.Success(data); | ||
} | ||
private async Task<ApiResult> UpdatePixivCookieAsync(string pcko) | ||
|
||
[HttpPost] | ||
[Authorize] | ||
[Route("set/pixiv")] | ||
public ApiResult SetPixiv([FromBody] CookieDto cookie) | ||
{ | ||
string cookie = pcko; | ||
if (string.IsNullOrWhiteSpace(cookie)) | ||
try | ||
{ | ||
Console.WriteLine($"未检测到cookie"); | ||
return ApiResult.Fail($"未检测到cookie"); | ||
var cookieStr = cookie.Cookie; | ||
if (string.IsNullOrWhiteSpace(cookieStr)) return ApiResult.ParamError; | ||
var website = websiteService.UpdatePixivCookie(cookieStr); | ||
WebsiteDatas.LoadWebsite(); | ||
return ApiResult.Success(); | ||
} | ||
|
||
cookie = cookie.Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Replace("\"", "").Trim(); | ||
Dictionary<string, string> cookieDic = cookie.SplitCookie(); | ||
string PHPSESSID = cookieDic.ContainsKey("PHPSESSID") ? cookieDic["PHPSESSID"] : string.Empty; | ||
if (string.IsNullOrWhiteSpace(PHPSESSID)) | ||
catch (HandleException ex) | ||
{ | ||
Console.WriteLine($"cookie中没有检测到PHPSESSID,请重新获取cookie"); | ||
return ApiResult.Fail($"cookie中没有检测到PHPSESSID,请重新获取cookie"); | ||
return ApiResult.Fail(ex); | ||
} | ||
|
||
string[] sessionArr = PHPSESSID.Split('_', StringSplitOptions.RemoveEmptyEntries); | ||
if (sessionArr.Length < 2 || string.IsNullOrWhiteSpace(sessionArr[0])) | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"cookie中的PHPSESSID格式不正确,请重新获取cookie"); | ||
return ApiResult.Fail($"cookie中的PHPSESSID格式不正确,请重新获取cookie"); | ||
return ApiResult.Fail(ex); | ||
} | ||
} | ||
|
||
long userId = 0; | ||
if (long.TryParse(sessionArr[0].Trim(), out userId) == false) | ||
[HttpGet] | ||
[Authorize] | ||
[Route("get/saucenao")] | ||
public ApiResult GetSaucenao() | ||
{ | ||
var data = new { cookie = WebsiteDatas.Saucenao?.Cookie ?? string.Empty }; | ||
return ApiResult.Success(data); | ||
} | ||
|
||
[HttpPost] | ||
[Authorize] | ||
[Route("set/saucenao")] | ||
public ApiResult SetSaucenao([FromBody] CookieDto cookie) | ||
{ | ||
try | ||
{ | ||
Console.WriteLine($"cookie中的PHPSESSID格式不正确,请重新获取cookie"); | ||
return ApiResult.Fail($"cookie中的PHPSESSID格式不正确,请重新获取cookie"); | ||
var cookieStr = cookie.Cookie; | ||
if (string.IsNullOrWhiteSpace(cookieStr)) return ApiResult.ParamError; | ||
var website = websiteService.UpdateSaucenaoCookie(cookieStr); | ||
WebsiteDatas.LoadWebsite(); | ||
return ApiResult.Success(); | ||
} | ||
catch (HandleException ex) | ||
{ | ||
return ApiResult.Fail(ex); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return ApiResult.Fail(ex); | ||
} | ||
|
||
if (cookieDic.ContainsKey("__cf_bm")) cookieDic.Remove("__cf_bm"); | ||
if (cookieDic.ContainsKey("cto_bundle")) cookieDic.Remove("cto_bundle"); | ||
if (cookieDic.ContainsKey("categorized_tags")) cookieDic.Remove("cookieDic"); | ||
if (cookieDic.ContainsKey("tag_view_ranking")) cookieDic.Remove("tag_view_ranking"); | ||
cookie = cookieDic.JoinCookie(); | ||
|
||
string websiteCode = Enum.GetName(typeof(WebsiteType), WebsiteType.Pixiv) ?? string.Empty; | ||
WebsitePO website = websiteService.UpdateWebsite(websiteCode, cookie, userId, BotConfig.PixivConfig.CookieExpire); | ||
WebsiteDatas.LoadWebsite(); | ||
string expireDate = website.CookieExpireDate.ToString("yyyy-MM-dd HH:mm:ss"); | ||
Console.WriteLine($"cookie更新完毕,过期时间为{expireDate}"); | ||
|
||
return ApiResult.Success(); | ||
} | ||
|
||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
Theresa3rd-Bot/TheresaBot.Main/Exceptions/HandleException.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,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace TheresaBot.Main.Exceptions | ||
{ | ||
public class HandleException : Exception | ||
{ | ||
public string RemindMessage { get; init; } | ||
|
||
public HandleException(string message) : base(message) | ||
{ | ||
this.RemindMessage = message; | ||
} | ||
} | ||
} |
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,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace TheresaBot.Main.Model.DTO | ||
{ | ||
public record CookieDto | ||
{ | ||
public string Cookie { get; set; } | ||
} | ||
} |
Oops, something went wrong.