-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKakaoAPI.cs
More file actions
133 lines (109 loc) · 4.65 KB
/
KakaoAPI.cs
File metadata and controls
133 lines (109 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using RestSharp;
using System;
using System.IO;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
namespace StoryDownloader3;
public class KakaoAPI(string loginCookie)
{
private readonly static RestClient s_client = new("https://story.kakao.com");
public string LoginID { get; set; }
public string Nickname { get; set; }
public async Task LoadInformation()
{
LoginID = await GetLoginID();
Nickname = await GetNickname();
}
private async Task<string> PostStoryReq(string endpoint, string postData)
{
// Initialize the RestSharp client
RestRequest request = new(endpoint, Method.Post);
// Set headers
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0");
request.AddHeader("Accept", "application/json");
request.AddHeader("Referer", "https://story.kakao.com/");
request.AddHeader("Accept-Language", "ko");
request.AddHeader("X-Kakao-ApiLevel", "49");
request.AddHeader("X-Kakao-DeviceInfo", "web:-;-;-");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
request.AddHeader("Cookie", loginCookie);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
// Set the request body
request.AddParameter("application/x-www-form-urlencoded", postData, ParameterType.RequestBody);
// Execute the request
RestResponse response = await s_client.ExecuteAsync(request);
// Return the content of the response
return response.Content;
}
private async Task<string> GetStoryReq(string endpoint, string referer = "")
{
// Initialize the RestSharp request
RestRequest request = new(endpoint, Method.Get);
// Set headers
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0");
request.AddHeader("Accept", "application/json");
request.AddHeader("Referer", $"https://story.kakao.com/{referer}");
request.AddHeader("Accept-Language", "ko");
request.AddHeader("X-Kakao-ApiLevel", "49");
request.AddHeader("X-Kakao-DeviceInfo", "web:-;-;-");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
request.AddHeader("Cookie", loginCookie);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
// Execute the request
RestResponse response = await s_client.ExecuteAsync(request);
// Return the content of the response
return response.Content;
}
public async Task DownloadMedia(string url, string savePath)
{
// Create a RestRequest for the download
RestRequest request = new(url, Method.Get);
request.AddHeader("Referer", $"https://story.kakao.com/{LoginID}");
// Execute the request and get the raw byte response
byte[] response = await s_client.DownloadDataAsync(request);
try
{
// Write the response to a file
await File.WriteAllBytesAsync(savePath, response);
}
catch (IOException) { } // Ignore
catch (Exception) { } // Ignore
}
private async Task<string> GetLoginID()
{
JsonNode jObj = JsonNode.Parse(await GetStoryReq("/a/settings/profile"));
return jObj["id"].ToString();
}
private async Task<string> GetNickname()
{
JsonNode jObj = JsonNode.Parse(await GetStoryReq("/a/settings/profile"));
return jObj["display_name"].ToString();
}
public async Task<JsonArray> GetArticles(string since)
{
JsonNode jObj = JsonNode.Parse(await GetStoryReq($"/a/profiles/{LoginID}?with=activities&since={since}"));
return jObj["activities"].AsArray();
}
public async Task<JsonArray> GetComments(string articleID, string since)
{
return JsonNode.Parse(await GetStoryReq($"/a/activities/{articleID}/comments?lpp=30&order=asc&since={since}", articleID.Replace('.', '/'))).AsArray();
}
public async Task<int> GetArticleCount()
{
JsonNode jObj = JsonNode.Parse(await GetStoryReq("/a/settings/profile"));
return (int)jObj["activity_count"];
}
public async Task<JsonArray> GetFriends()
{
JsonNode jObj = JsonNode.Parse(await GetStoryReq($"/a/friends"));
return jObj["profiles"].AsArray();
}
public async Task<JsonArray> GetInvitations()
{
return JsonNode.Parse(await GetStoryReq("/a/invitations")).AsArray();
}
public async Task<JsonArray> GetMessages(string since)
{
return JsonNode.Parse(await GetStoryReq($"/a/messages?since={since}")).AsArray();
}
}