This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPostData.cs
101 lines (92 loc) · 3.51 KB
/
PostData.cs
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
using Grabacr07.KanColleWrapper;
using Livet;
using Nekoxy;
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Linq;
namespace DBPost
{
public class SvDataRaw
{
public string RequestBody;
public string ResponseBody;
public string EndPoint;
public SvDataRaw(string reqBody, string resBody, string endPoint)
{
RequestBody = reqBody;
ResponseBody = resBody;
EndPoint = endPoint;
}
public static SvDataRaw Parse(Session session)
{
var reqBody = session.Response.BodyAsString.Replace("svdata=", "");
var result = new SvDataRaw(session.Request.BodyAsString, reqBody, session.Request.PathAndQuery);
return result;
}
public static bool TryParse(Session session, out SvDataRaw result)
{
try
{
result = Parse(session);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
result = null;
return false;
}
return true;
}
public void SendDb()
{
if( !Properties.Settings.Default.IsSendDb || string.IsNullOrEmpty(Properties.Settings.Default.DbAccessKey) )
{
return;
}
using (System.Net.WebClient wc = new System.Net.WebClient())
{
NameValueCollection post = new NameValueCollection();
post.Add("token", Properties.Settings.Default.DbAccessKey);
post.Add("agent", "byCEC6DSrXrTrKcADwT3"); // このクライアントのエージェントキー
post.Add("url", EndPoint);
string requestBody = System.Text.RegularExpressions.Regex.Replace(RequestBody, @"&api(_|%5F)token=[0-9a-f]+|api(_|%5F)token=[0-9a-f]+&?", ""); // api_tokenを送信しないように削除
post.Add("requestbody", requestBody);
post.Add("responsebody", ResponseBody);
wc.UploadValuesAsync(new Uri("http://api.kancolle-db.net/2/"), post);
#if DEBUG
Debug.WriteLine("==================================================");
Debug.WriteLine("Send to KanColle statistics database");
Debug.WriteLine("url: " + EndPoint);
Debug.WriteLine("reqBody: " + requestBody);
Debug.WriteLine("resBody: " + ResponseBody);
Debug.WriteLine("==================================================");
#endif
}
}
}
public static class KanColleProxyExtensions
{
/// <summary>
/// Nekoxy でフックした <see cref="Session" /> オブジェクトの <see cref="Session.Response" /> データを
/// <see cref="SvDataRaw" /> 型にパースします。
/// </summary>
public static IObservable<SvDataRaw> Parse(this IObservable<Session> source)
{
Func<Session, SvDataRaw> converter = session =>
{
SvDataRaw result;
return SvDataRaw.TryParse(session, out result) ? result : null;
};
return source.Select(converter).Do(s => s.SendDb());
}
}
public class PostData : NotificationObject
{
public PostData(KanColleProxy proxy, string endPoint)
{
proxy.ApiSessionSource.Where(x => x.Request.PathAndQuery == endPoint).Parse().Subscribe();
}
}
}