Skip to content

Commit 2a631e5

Browse files
committed
Update api
Bug-fixes New api methods Rating calculation util
1 parent d768008 commit 2a631e5

40 files changed

Lines changed: 54774 additions & 36 deletions

APIDebug/APIDebug.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Reference Include="System.Xml" />
4545
</ItemGroup>
4646
<ItemGroup>
47+
<Compile Include="ApiTests.cs" />
4748
<Compile Include="Program.cs" />
4849
<Compile Include="Properties\AssemblyInfo.cs" />
4950
</ItemGroup>

APIDebug/ApiTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using CodeforcesAPI;
7+
using CodeforcesAPI.Utils;
8+
9+
namespace APIDebug
10+
{
11+
delegate void TestHandler(Codeforces api);
12+
static class ApiTests
13+
{
14+
public static void RecentActionsTest1(Codeforces api)
15+
{
16+
foreach(var x in api.RecentActions(10).Result)
17+
{
18+
if (x.Comment != null)
19+
{
20+
Console.WriteLine("[comment][{0}] {1}: {2}", x.Time, x.Comment.CommentatorHandle, x.Comment.Text);
21+
}
22+
if (x.BlogEntry != null)
23+
{
24+
Console.WriteLine("[blog][{0}] {1}", x.Time, x.BlogEntry.Title);
25+
}
26+
}
27+
}
28+
public static void BlogEntryView1(Codeforces api)
29+
{
30+
var view = api.BlogEntry.View(44362).Result;
31+
Console.WriteLine(view.Title);
32+
Console.WriteLine("Rating: " + view.Rating);
33+
Console.WriteLine("Author: " + view.AuthorHandle);
34+
}
35+
public static void BlogEntryComments1(Codeforces api)
36+
{
37+
var view = api.BlogEntry.Comments(44362).Result;
38+
Console.WriteLine("Comments: " + view.Length);
39+
foreach(var x in view)
40+
{
41+
Console.WriteLine("{0}: {1}", x.CommentatorHandle, x.Text);
42+
}
43+
}
44+
}
45+
}

APIDebug/Program.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,37 @@
44
using System.Text;
55
using System.Data;
66
using System.IO;
7+
using CodeforcesAPI.Utils;
78
using CodeforcesAPI;
9+
using System.Diagnostics;
810
using System.Threading.Tasks;
911

1012
namespace APIDebug
1113
{
1214
class Program
1315
{
14-
static void Main()
16+
static Dictionary<string, TestHandler> Tests = new Dictionary<string, TestHandler>
1517
{
16-
var api = new Codeforces(Langs.EN);
17-
var users = api.User.RatedList(true).Result;
18+
{ "recentActions(10)", ApiTests.RecentActionsTest1 },
19+
{ "blogView1", ApiTests.BlogEntryView1 },
20+
{ "blogComments1", ApiTests.BlogEntryComments1 }
21+
};
22+
static void Main(string[] args)
23+
{
24+
var api = new Codeforces();
25+
var sw = new Stopwatch();
26+
foreach(var test in Tests)
27+
{
28+
Console.WriteLine(test.Key + ":");
29+
30+
sw.Restart();
31+
test.Value(api);
32+
sw.Stop();
1833

19-
Console.WriteLine(api.User.RatedList(true).Result.Count());
34+
Console.WriteLine("{0} complete after {1} ms", test.Key, sw.ElapsedMilliseconds);
35+
Console.WriteLine("Press any key to continue");
36+
Console.ReadKey(true);
37+
}
2038
}
2139
}
2240
}

CodeforcesAPI.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.24720.0
4+
VisualStudioVersion = 14.0.25123.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeforcesAPI", "CodeforcesAPI\CodeforcesAPI.csproj", "{B6D8757A-009A-4BAE-8845-AEB820B640B6}"
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APIDebug", "APIDebug\APIDebug.csproj", "{DFC826BB-91E0-464D-8ACF-7FD19C8FAD58}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RatingCalculation", "RatingCalculation\RatingCalculation.csproj", "{A442E510-D698-45AB-A41D-8B954955FF5F}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{DFC826BB-91E0-464D-8ACF-7FD19C8FAD58}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{DFC826BB-91E0-464D-8ACF-7FD19C8FAD58}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{DFC826BB-91E0-464D-8ACF-7FD19C8FAD58}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{A442E510-D698-45AB-A41D-8B954955FF5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{A442E510-D698-45AB-A41D-8B954955FF5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{A442E510-D698-45AB-A41D-8B954955FF5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{A442E510-D698-45AB-A41D-8B954955FF5F}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

CodeforcesAPI/Codeforces.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
using CodeforcesAPI.Helpers;
1111
using Newtonsoft.Json;
1212
using Newtonsoft.Json.Serialization;
13+
using System.IO;
1314

1415
namespace CodeforcesAPI
1516
{
1617
public class Codeforces
1718
{
1819
public const string ApiUrl = "http://codeforces.com/api/";
1920

21+
public BlogEntryMethods BlogEntry { get; private set; }
2022
public ContestMethods Contest { get; private set; }
2123
public ProblemSetMethods ProblemSet { get; private set; }
2224
public UserMethods User { get; private set; }
@@ -26,6 +28,7 @@ public class Codeforces
2628

2729
public Codeforces(Langs lang = Langs.EN, ApiKey apiKey = null)
2830
{
31+
BlogEntry = MethodsGroup.Init<BlogEntryMethods>(this);
2932
Contest = MethodsGroup.Init<ContestMethods>(this);
3033
ProblemSet = MethodsGroup.Init<ProblemSetMethods>(this);
3134
User = MethodsGroup.Init<UserMethods>(this);
@@ -42,6 +45,19 @@ protected virtual void AddParameters(string method, Dictionary<string, object> p
4245
}
4346
}
4447

48+
/// <summary>
49+
/// Returns recent actions.
50+
/// </summary>
51+
/// <param name="maxCount">Number of recent actions to return. Can be up to 100.</param>
52+
/// <returns>Returns a list of RecentAction objects.</returns>
53+
public async Task<RecentAction[]> RecentActions(int maxCount)
54+
{
55+
return await SendWebRequest<RecentAction[]>("recentActions", new Dictionary<string, object>
56+
{
57+
{ "maxCount", maxCount }
58+
});
59+
}
60+
4561
/// <summary>
4662
/// Call codeforces API.
4763
/// </summary>
@@ -54,19 +70,23 @@ public async Task<T> SendWebRequest<T>(string method, Dictionary<string, object>
5470
if (parameters == null) parameters = new Dictionary<string, object>();
5571

5672
AddParameters(method, parameters);
57-
58-
var req = method + "?" + UrlHelper.ToGetRequest(parameters);
59-
var uri = new Uri(ApiUrl + req);
60-
73+
74+
var req = UrlHelper.ToGetRequest(parameters);
75+
//var uri = ApiUrl + method; // POST
76+
var uri = ApiUrl + method + "?" + req;
6177

6278
using (var client = new HttpClient())
6379
{
6480
ApiResponse<T> responseObject = null;
6581
try
6682
{
67-
HttpResponseMessage response = await client.GetAsync(uri).ConfigureAwait(false);
68-
responseObject = await response.Content.ReadAsAsync<ApiResponse<T>>().ConfigureAwait(false);
69-
response.EnsureSuccessStatusCode();
83+
/*
84+
var jsonResp = await (await client.PostAsync(uri, new FormUrlEncodedContent(
85+
parameters.Select(x => new KeyValuePair<string, string>(x.Key, x.Value.ToString())))
86+
)).Content.ReadAsStringAsync(); // POST
87+
*/
88+
var jsonResp = await client.GetStringAsync(uri);
89+
responseObject = JsonConvert.DeserializeObject<ApiResponse<T>>(jsonResp);
7090
}
7191
catch (HttpRequestException)
7292
{

CodeforcesAPI/CodeforcesAPI.csproj

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,14 @@
3333
<Prefer32Bit>false</Prefer32Bit>
3434
</PropertyGroup>
3535
<ItemGroup>
36-
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
37-
<HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
36+
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
3838
<Private>True</Private>
3939
</Reference>
4040
<Reference Include="System" />
4141
<Reference Include="System.Core" />
4242
<Reference Include="System.Net" />
4343
<Reference Include="System.Net.Http" />
44-
<Reference Include="System.Net.Http.Extensions, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
45-
<HintPath>..\packages\System.Net.Http.Formatting.Extension.5.2.3.0\lib\System.Net.Http.Extensions.dll</HintPath>
46-
<Private>True</Private>
47-
</Reference>
48-
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
49-
<HintPath>..\packages\System.Net.Http.Formatting.Extension.5.2.3.0\lib\System.Net.Http.Formatting.dll</HintPath>
50-
<Private>True</Private>
51-
</Reference>
52-
<Reference Include="System.Net.Http.Primitives, Version=4.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
53-
<HintPath>..\packages\System.Net.Http.Formatting.Extension.5.2.3.0\lib\System.Net.Http.Primitives.dll</HintPath>
54-
<Private>True</Private>
55-
</Reference>
5644
<Reference Include="System.Web" />
5745
<Reference Include="System.Xml.Linq" />
5846
<Reference Include="System.Data.DataSetExtensions" />
@@ -77,11 +65,15 @@
7765
<Compile Include="Helpers\ExtUnixTimeConverter.cs" />
7866
<Compile Include="Helpers\UnixTime.cs" />
7967
<Compile Include="Helpers\UrlHelper.cs" />
68+
<Compile Include="Methods\BlogEntryMethods.cs" />
8069
<Compile Include="Methods\ContestMethods.cs" />
8170
<Compile Include="Methods\MethodsGroup.cs" />
8271
<Compile Include="Methods\ProblemSetMethods.cs" />
8372
<Compile Include="Methods\UserMethods.cs" />
73+
<Compile Include="Types\BlogEntry.cs" />
74+
<Compile Include="Types\Comment.cs" />
8475
<Compile Include="Types\Contest.cs" />
76+
<Compile Include="Types\ContestRegistrant.cs" />
8577
<Compile Include="Types\ContestStandings.cs" />
8678
<Compile Include="Types\Hack.cs" />
8779
<Compile Include="Types\HackJudgeProtocol.cs" />
@@ -95,8 +87,13 @@
9587
<Compile Include="Codeforces.cs" />
9688
<Compile Include="Types\RanklistRow.cs" />
9789
<Compile Include="Types\RatingChange.cs" />
90+
<Compile Include="Types\RecentAction.cs" />
9891
<Compile Include="Types\Submission.cs" />
9992
<Compile Include="Types\User.cs" />
93+
<Compile Include="Utils\ApiMethodsExt.cs" />
94+
<Compile Include="Utils\CodeforcesRatingCalculator2.cs" />
95+
<Compile Include="Utils\Contestant.cs" />
96+
<Compile Include="Utils\IRatingCalculator.cs" />
10097
</ItemGroup>
10198
<ItemGroup>
10299
<None Include="packages.config" />

CodeforcesAPI/Enums/PartyParticipantType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace CodeforcesAPI
77
{
88
public enum PartyParticipantType
99
{
10+
Absent,
1011
Contestant, Practice, Virtual, Manager, OutOfCompetition
1112
}
1213
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CodeforcesAPI.Methods
8+
{
9+
public class BlogEntryMethods : MethodsGroup
10+
{
11+
public override string GroupName
12+
{
13+
get
14+
{
15+
return "blogEntry";
16+
}
17+
}
18+
/// <summary>
19+
/// Returns a list of comments to the specified blog entry.
20+
/// </summary>
21+
/// <param name="blogEntryId">Id of the blog entry. It can be seen in blog entry URL. For example: /blog/entry/79</param>
22+
/// <returns>A list of Comment objects.</returns>
23+
public async Task<Comment[]> Comments(int blogEntryId)
24+
{
25+
return await SendWebRequest<Comment[]>("comments", new Dictionary<string, object>
26+
{
27+
{ "blogEntryId", blogEntryId }
28+
});
29+
}
30+
/// <summary>
31+
/// Returns blog entry.
32+
/// </summary>
33+
/// <param name="blogEntryId">Id of the blog entry. It can be seen in blog entry URL. For example: /blog/entry/79</param>
34+
/// <returns>Returns a BlogEntry object in full version.</returns>
35+
public async Task<BlogEntry> View(int blogEntryId)
36+
{
37+
return await SendWebRequest<BlogEntry>("view", new Dictionary<string, object>
38+
{
39+
{ "blogEntryId", blogEntryId }
40+
});
41+
}
42+
}
43+
}

CodeforcesAPI/Methods/ContestMethods.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ public async Task<RatingChange[]> RatingChanges(int contestId)
5252
{ "contestId", contestId }
5353
});
5454
}
55+
/*
56+
* Don't work now :(
57+
*
58+
/// <summary>
59+
/// Returns list of contest-registrants.
60+
/// </summary>
61+
/// <param name="contestId">Id of the contest. It is not the round number. It can be seen in contest URL. For example: /contest/566/status</param>
62+
/// <returns>Returns list of contest-registrants.</returns>
63+
public async Task<ContestRegistrant[]> Registrants(int contestId)
64+
{
65+
return await SendWebRequest<ContestRegistrant[]>("registrants", new Dictionary<string, object>
66+
{
67+
{ "contestId", contestId }
68+
});
69+
}
70+
*/
5571
/// <summary>
5672
/// Returns the description of the contest and the requested part of the standings.
5773
/// </summary>

CodeforcesAPI/Methods/MethodsGroup.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ namespace CodeforcesAPI.Methods
99
public abstract class MethodsGroup
1010
{
1111
public Codeforces Api { get; private set; }
12-
public virtual string GroupName
13-
{
14-
get
15-
{
16-
throw new NotImplementedException("Name not declarated");
17-
}
18-
}
12+
public abstract string GroupName { get; }
1913
protected async Task<T> SendWebRequest<T>(string method, Dictionary<string, object> parameters = null)
2014
{
2115
return await Api.SendWebRequest<T>(GroupName + "." + method, parameters);

0 commit comments

Comments
 (0)