Skip to content

Commit

Permalink
added captcha getter and solver method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Marcelli committed Feb 18, 2017
1 parent 9fdae31 commit 48e4412
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 8 deletions.
31 changes: 30 additions & 1 deletion JodelAPI/JodelAPI/Internal/ApiCall.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using JodelAPI.Json.Request;
using JodelAPI.Json.Response;
using JodelAPI.Shared;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JodelAPI.Internal
{
Expand All @@ -29,7 +34,7 @@ internal class ApiCall
/// <param name="version">API version to be used</param>
/// <param name="postAction">Action to be applied to post</param>
/// <param name="authorize"><c>true</c> if authorization bearer needs to be added to request header, otherwise <c>false</c></param>
internal ApiCall(System.Net.Http.HttpMethod method, string url, string version = "v3", string postAction = "", bool authorize = true)
internal ApiCall(HttpMethod method, string url, string version = "v3", string postAction = "", bool authorize = true)
{
Method = method;
Url = url;
Expand All @@ -38,6 +43,30 @@ internal ApiCall(System.Net.Http.HttpMethod method, string url, string version =
Authorize = authorize;
}

internal JsonCaptcha.RootObject GetCaptcha(User user)
{
string url = "https://" + Links.ApiBaseUrl + "/api/" + Version + Url + user.Token.Token;
var json = JsonConvert.DeserializeObject<JsonCaptcha.RootObject>(new WebClient().DownloadString(url));
return json;
}

internal bool PostCaptcha(User user, Captcha captcha, int[] answer)
{
string url = "https://" + Links.ApiBaseUrl + "/api/" + Version + Url + user.Token.Token;
string an = "[";
for (var index = 0; index < answer.Length; index++)
{
var a = answer[index];
an += a;
if (an.Length != index + 1)
an += ",";
else
an += "]";
}
var json = JObject.Parse(new WebClient().UploadString(url, "{\"key\":\"" + captcha.Key + "\",\"answer\":" + an + "}"));
return json.Value<bool>("verified");
}

internal string ExecuteRequest(User user, Dictionary<string, string> parameters = null, JsonRequest payload = null, string postId = null)
{
string plainJson = null;
Expand Down
4 changes: 2 additions & 2 deletions JodelAPI/JodelAPI/Internal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace JodelAPI.Internal
internal class Constants
{
// Key Values
public const string Key = "plerFToqEdWlzShdZlTywaCHRuzlKIMsNmOJVDGE";
public const string Key = "LDWWpuUigOnKCbCLpoNMDHCqHCWbLKPzHbnIUKIf";
public const string ClientId = "81e8a76e-1e02-4d17-9ba0-8a7020261b26";
public const string AppVersion = "4.31.1";
public const string AppVersion = "4.33.2";


// Headers
Expand Down
2 changes: 2 additions & 0 deletions JodelAPI/JodelAPI/Internal/Links.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal static class Links
public static readonly ApiCall GetMyPopularPosts = new ApiCall(HttpMethod.Get, "/posts/mine/popular/", "v2");
public static readonly ApiCall GetMyMostDiscussedPosts = new ApiCall(HttpMethod.Get, "/posts/mine/discussed/", "v2");
public static readonly ApiCall GetMyPinnedPosts = new ApiCall(HttpMethod.Get, "/posts/mine/pinned/", "v2");
public static readonly ApiCall GetCaptcha = new ApiCall(HttpMethod.Get, "/user/verification/imageCaptcha?access_token=");
public static readonly ApiCall VerifyCaptcha = new ApiCall(HttpMethod.Post, "/user/verification/imageCaptcha?access_token=");

#endregion

Expand Down
11 changes: 11 additions & 0 deletions JodelAPI/JodelAPI/Jodel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ public void SetLocation()
Links.SendUserLocation.ExecuteRequest(Account, payload: payload);
}

public Captcha GetCaptcha(bool advanced = false)
{
var captchaRoot = Links.GetCaptcha.GetCaptcha(Account);
return advanced ? new Captcha(new JodelWebClient().DownloadData(captchaRoot.image_url), captchaRoot.key, captchaRoot.image_url, captchaRoot.image_size) : new Captcha(new JodelWebClient().DownloadData(captchaRoot.image_url), captchaRoot.key, captchaRoot.image_url);
}

public bool SolveCaptcha(Captcha captcha, int[] answer)
{
return Links.VerifyCaptcha.PostCaptcha(Account, captcha, answer);
}

#endregion

#region Channels
Expand Down
2 changes: 2 additions & 0 deletions JodelAPI/JodelAPI/JodelAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="Json\Request\JsonRequestRecommendedChannels.cs" />
<Compile Include="Json\Request\JsonRequestRefreshAccessToken.cs" />
<Compile Include="Json\Request\JsonRequestUpDownVote.cs" />
<Compile Include="Json\Response\JsonCaptcha.cs" />
<Compile Include="Json\Response\JsonComments.cs" />
<Compile Include="Json\Response\JsonConfig.cs" />
<Compile Include="Json\Response\JsonFollowedChannelsMeta.cs" />
Expand All @@ -86,6 +87,7 @@
<Compile Include="Json\Request\JsonRequest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Shared\AccessToken.cs" />
<Compile Include="Shared\Captcha.cs" />
<Compile Include="Shared\Channel.cs" />
<Compile Include="Shared\JodelMainData.cs" />
<Compile Include="Shared\JodelPost.cs" />
Expand Down
18 changes: 18 additions & 0 deletions JodelAPI/JodelAPI/Json/Response/JsonCaptcha.cs
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 JodelAPI.Json.Response
{
internal class JsonCaptcha
{
public class RootObject
{
public string key { get; set; }
public string image_url { get; set; }
public int image_size { get; set; }
}
}
}
31 changes: 31 additions & 0 deletions JodelAPI/JodelAPI/Shared/Captcha.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JodelAPI.Shared
{
public class Captcha
{
public Captcha(byte[] image, string key, string imageUrl)
{
Image = image;
Key = key;
ImageUrl = imageUrl;
}

public Captcha(byte[] image, string key, string imageUrl, int imageSize)
{
Image = image;
Key = key;
ImageUrl = imageUrl;
ImageSize = imageSize;
}

public byte[] Image { get; }
public string Key { get; }
public string ImageUrl { get; }
public int ImageSize { get; }
}
}
19 changes: 14 additions & 5 deletions JodelAPI/JodelAPITests/JodelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,26 @@ public void GetRecommendedChannelsTest()
}

[TestMethod()]
public void GetFollowedChannelsMetaTest()
public void GetPostLocationComboTest()
{
Assert.IsTrue(jodel.GenerateAccessToken());
Assert.IsNotNull(jodel.GetFollowedChannelsMeta());
Assert.IsNotNull(jodel.GetPostLocationCombo());
}

[TestMethod()]
public void GetPostLocationComboTest()
public void GetCaptcha()
{
Assert.IsTrue(jodel.GenerateAccessToken());
Assert.IsNotNull(jodel.GetPostLocationCombo());
Assert.IsNotNull(jodel.GetCaptcha());
Console.WriteLine(jodel.GetCaptcha().ImageUrl + ":" + jodel.GetCaptcha().Key);
}

[TestMethod()]
public void SolveCaptcha()
{
var captcha = jodel.GetCaptcha();
Assert.IsNotNull(captcha);
Console.WriteLine(captcha.ImageUrl + ":" + captcha.Key);
Assert.IsInstanceOfType(jodel.SolveCaptcha(captcha, new[] {1}), typeof(bool));
}
}
}

0 comments on commit 48e4412

Please sign in to comment.