111 lines
4.4 KiB
C#
111 lines
4.4 KiB
C#
using BitSearch.API.Models;
|
|
using BitSearch.API.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Net.Http.Headers;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BitSearch.API.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class SearchController : ControllerBase
|
|
{
|
|
|
|
private readonly ILogger<SearchController> _logger;
|
|
private readonly IConfiguration Configuration;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly IAnaliseService _analiseService;
|
|
|
|
public SearchController(ILogger<SearchController> logger, IConfiguration configuration, IHttpClientFactory httpClientFactory,
|
|
IAnaliseService analiseService)
|
|
{
|
|
_logger = logger;
|
|
_httpClientFactory = httpClientFactory;
|
|
Configuration = configuration;
|
|
_analiseService = analiseService;
|
|
}
|
|
|
|
[HttpGet("ranking")]
|
|
public async Task<IActionResult> GetRanking()
|
|
{
|
|
var cryptoHash = new Dictionary<string, string>()
|
|
{
|
|
{"Bitcoin", "#btc"},
|
|
{"Etherum", "#eth"},
|
|
{"Tron", "#trx"},
|
|
{"Tether", "#usdt"},
|
|
{"Binance Coin", "#bnb"},
|
|
{"Cardano", "#ada"},
|
|
{"Dash", "#dash"},
|
|
{"Neo", "#neo"},
|
|
{"Steem", "#steem"},
|
|
{"Zcash", "#zec"}
|
|
};
|
|
|
|
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}")]
|
|
public async Task<IActionResult> GetAnalysis(string hash)
|
|
{
|
|
var queryString = new Dictionary<string, string>()
|
|
{
|
|
{"query", $"#{hash}"},
|
|
{"max_results", "100"},
|
|
{"tweet.fields", "lang,referenced_tweets"},
|
|
};
|
|
|
|
var token = Configuration["Token"];
|
|
var twitterClient = _httpClientFactory.CreateClient();
|
|
twitterClient.BaseAddress = new Uri("https://api.twitter.com/2/tweets/search/");
|
|
var requestUri = QueryHelpers.AddQueryString("recent", queryString);
|
|
var twiiterRequest = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
|
twitterClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
twitterClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
var response = await twitterClient.SendAsync(twiiterRequest);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var tweetsDto = JsonConvert.DeserializeObject<TweeterResponse>(await response.Content.ReadAsStringAsync());
|
|
tweetsDto.data = tweetsDto.data.Where(d => d.lang == "en");
|
|
var analise = await _analiseService.AnaliseTweets(tweetsDto);
|
|
|
|
return Ok(analise);
|
|
//return Ok(tweetsDto);
|
|
}
|
|
|
|
return Ok(response);
|
|
}
|
|
}
|
|
}
|