ranking and analise endpoints

This commit is contained in:
Jędrzej 2022-01-25 00:41:49 +01:00
parent a77cf011d3
commit 3dd504a287
10 changed files with 129 additions and 35 deletions

View File

@ -11,6 +11,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace BitSearch.API.Controllers namespace BitSearch.API.Controllers
@ -32,11 +33,37 @@ namespace BitSearch.API.Controllers
} }
[HttpGet("ranking")] [HttpGet("ranking")]
public IActionResult GetRanking() public async Task<IActionResult> GetRanking()
{ {
//var request = new HttpRequestMessage(HttpMethod.Get, "https://api.twitter.com/2/tweets/counts/recent"); var cryptoHash = new Dictionary<string, string>()
{
{"Bitcoin", "#btc"},
{"Etherum", "#eth"},
{"Ripple", "xrp"},
{"Litecoin", "#ltc"},
{"Binance Coin", "#binance"}
};
return Ok(); var token = Configuration["Token"];
var twitterClient = _httpClientFactory.CreateClient();
twitterClient.BaseAddress = new Uri("https://api.twitter.com/2/tweets/counts/");
twitterClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
twitterClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var ranking = new List<CryptoRanking>();
foreach (KeyValuePair<string, string> crypto in cryptoHash)
{
var requestUri = QueryHelpers.AddQueryString("recent", "query", crypto.Value);
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
var response = await twitterClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var tweetsDto = JsonConvert.DeserializeObject<TweetCount>(await response.Content.ReadAsStringAsync());
ranking.Add(new CryptoRanking() { Name = crypto.Key, TweetAmount = tweetsDto.meta.total_tweet_count });
}
}
return Ok(ranking.OrderByDescending(r => r.TweetAmount));
} }
[HttpGet("analise/{hash}")] [HttpGet("analise/{hash}")]
@ -44,23 +71,31 @@ namespace BitSearch.API.Controllers
{ {
var queryString = new Dictionary<string, string>() var queryString = new Dictionary<string, string>()
{ {
{"query", hash}, {"query", $"#{hash}"},
{"max_results", "100"}, {"max_results", "100"},
{"tweet.fields", "lang,referenced_tweets"}, {"tweet.fields", "lang,referenced_tweets"},
}; };
var token = Configuration["Token"]; var token = Configuration["Token"];
var httpClient = _httpClientFactory.CreateClient(); var twitterClient = _httpClientFactory.CreateClient();
httpClient.BaseAddress = new Uri("https://api.twitter.com/2/tweets/search/"); twitterClient.BaseAddress = new Uri("https://api.twitter.com/2/tweets/search/");
var requestUri = QueryHelpers.AddQueryString("recent", queryString); var requestUri = QueryHelpers.AddQueryString("recent", queryString);
var request = new HttpRequestMessage(HttpMethod.Get, requestUri); var twiiterRequest = new HttpRequestMessage(HttpMethod.Get, requestUri);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); twitterClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); twitterClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await httpClient.SendAsync(request); var response = await twitterClient.SendAsync(twiiterRequest);
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
return Ok(await response.Content.ReadAsStringAsync()); var tweetsDto = JsonConvert.DeserializeObject<TweeterResponse>(await response.Content.ReadAsStringAsync());
tweetsDto.data = tweetsDto.data.Where(d => d.lang == "en");
var microServiceClient = _httpClientFactory.CreateClient();
var microServiceRequest = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1:5000/analysis");
microServiceRequest.Content = JsonContent.Create(tweetsDto);
var microServiceResponse = await microServiceClient.SendAsync(microServiceRequest);
return Ok(await microServiceResponse.Content.ReadAsStringAsync());
//return Ok(tweetsDto);
} }
return Ok(response); return Ok(response);

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BitSearch.API.Models
{
public class BaseTweetsDto<T>
{
public IEnumerable<T> data { get; set; }
}
}

View File

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BitSearch.API.Models
{
public class CryptoAnalysis
{
public string Name { get; set; }
public decimal Positive { get; set; }
public decimal Negative { get; set; }
}
}

View File

@ -8,7 +8,6 @@ namespace BitSearch.API.Models
public class CryptoRanking public class CryptoRanking
{ {
public string Name { get; set; } public string Name { get; set; }
public string Hasztag { get; set; }
public int TweetAmount { get; set; } public int TweetAmount { get; set; }
} }
} }

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BitSearch.API.Models
{
public class MetaData
{
public int total_tweet_count { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BitSearch.API.Models
{
public class ReferencedTweets
{
public string type { get; set; }
public string id { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BitSearch.API.Models
{
public class TweetCount
{
public MetaData meta { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BitSearch.API.Models
{
public class TweeterResponse : BaseTweetsDto<TweetsDto>
{
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BitSearch.API.Models
{
public class TweetsDto
{
public string id { get; set; }
public string text { get; set; }
public string lang { get; set; }
public IEnumerable<ReferencedTweets> referenced_tweets { get; set; }
}
}

View File

@ -1,13 +1,13 @@
{ {
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": { "iisSettings": {
"windowsAuthentication": false, "windowsAuthentication": false,
"anonymousAuthentication": true, "anonymousAuthentication": true,
"iisExpress": { "iisExpress": {
"applicationUrl": "http://localhost:62241", "applicationUrl": "http://localhost:62241",
"sslPort": 44393 "sslPort": 0
} }
}, },
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": { "profiles": {
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
@ -19,13 +19,13 @@
}, },
"BitSearch.API": { "BitSearch.API": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} },
"dotnetRunMessages": "true",
"applicationUrl": "https://localhost:5001;http://localhost:5000"
} }
} }
} }