From ac6ca2535cba88e4212b99280b4506a6f92787a9 Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Thu, 24 Nov 2022 20:25:07 -0500 Subject: [PATCH] implemented profile page from public api --- INSTALLATION.md | 9 --- docker-compose.yml | 30 +--------- .../TwitterUserService.cs | 58 ++++++------------- src/BirdsiteLive/appsettings.json | 10 +--- 4 files changed, 24 insertions(+), 83 deletions(-) diff --git a/INSTALLATION.md b/INSTALLATION.md index 7268344..285a175 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -1,12 +1,5 @@ # Installation -## Prerequisites - -You will need a Twitter API key to make BirdsiteLIVE working. First create an **Standalone App** in the [Twitter developer portal](https://developer.twitter.com/en/portal/projects-and-apps) and retrieve the API Key and API Secret Key. - -Please make sure you are using a **Standalone App** API Key and not a **Project App** API Key (that will NOT work with BirdsiteLIVE), if you don't see the **Standalone App** section, you might need to [apply for Elevated Access](https://developer.twitter.com/en/portal/products/elevated) as described in the [API documentation](https://developer.twitter.com/en/support/twitter-api/developer-account). - - ## Server prerequisites Your instance will need [docker](https://docs.docker.com/engine/install/) and [docker-compose](https://docs.docker.com/compose/install/) installed and working. @@ -31,8 +24,6 @@ sudo nano docker-compose.yml * `Instance:Domain` the domain name you'll be using, for example use `birdsite.live` for the URL `https://birdsite.live` * `Instance:AdminEmail` the admin's email, will be displayed in the instance /.well-known/nodeinfo endpoint -* `Twitter:ConsumerKey` the Twitter API key -* `Twitter:ConsumerSecret` the Twitter API secret key #### Database credentials diff --git a/docker-compose.yml b/docker-compose.yml index 77dee53..27ba800 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,30 +1,6 @@ version: "3" -networks: - birdsitelivenetwork: - external: false - services: - server: - image: nicolasconstant/birdsitelive:latest - restart: always - container_name: birdsitelive - environment: - - Instance:Domain=domain.name - - Instance:AdminEmail=name@domain.ext - - Db:Type=postgres - - Db:Host=db - - Db:Name=birdsitelive - - Db:User=birdsitelive - - Db:Password=birdsitelive - - Twitter:ConsumerKey=twitter.api.key - - Twitter:ConsumerSecret=twitter.api.key - networks: - - birdsitelivenetwork - ports: - - "5000:80" - depends_on: - - db db: image: postgres:9.6 @@ -33,7 +9,7 @@ services: - POSTGRES_USER=birdsitelive - POSTGRES_PASSWORD=birdsitelive - POSTGRES_DB=birdsitelive - networks: - - birdsitelivenetwork volumes: - - ./postgres:/var/lib/postgresql/data \ No newline at end of file + - ./postgres:/var/lib/postgresql/data + ports: + - "5432:5432" diff --git a/src/BirdsiteLive.Twitter/TwitterUserService.cs b/src/BirdsiteLive.Twitter/TwitterUserService.cs index 4f7e98a..3ea3a25 100644 --- a/src/BirdsiteLive.Twitter/TwitterUserService.cs +++ b/src/BirdsiteLive.Twitter/TwitterUserService.cs @@ -23,6 +23,8 @@ namespace BirdsiteLive.Twitter private readonly ITwitterStatisticsHandler _statisticsHandler; private readonly ILogger _logger; private HttpClient _httpClient = new HttpClient(); + + private readonly string endpoint = "https://twitter.com/i/api/graphql/4LB4fkCe3RDLDmOEEYtueg/UserByScreenName?variables=%7B%22screen_name%22%3A%22elonmusk%22%2C%22withSafetyModeUserFields%22%3Atrue%2C%22withSuperFollowsUserFields%22%3Atrue%7D&features=%7B%22responsive_web_twitter_blue_verified_badge_is_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22responsive_web_twitter_blue_new_verification_copy_is_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%7D"; #region Ctor public TwitterUserService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ILogger logger) @@ -39,19 +41,17 @@ namespace BirdsiteLive.Twitter } public async Task GetUserAsync(string username) { - //Check if API is saturated - if (IsUserApiRateLimited()) throw new RateLimitExceededException(); - - //Proceed to account retrieval - await _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); JsonDocument res; try { - using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://api.twitter.com/2/users/by/username/"+ username + "?user.fields=name,username,protected,profile_image_url,url,description")) - { - request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + _twitterAuthenticationInitializer.Token); + using (var request = new HttpRequestMessage(new HttpMethod("GET"), endpoint.Replace("elonmusk", username))) + { + request.Headers.TryAddWithoutValidation("Authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"); + request.Headers.TryAddWithoutValidation("x-guest-token", "1595938298886758401"); + request.Headers.TryAddWithoutValidation("Referer", "https://twitter.com/"); + request.Headers.TryAddWithoutValidation("x-twitter-active-user", "yes"); var httpResponse = await _httpClient.SendAsync(request); httpResponse.EnsureSuccessStatusCode(); @@ -94,46 +94,24 @@ namespace BirdsiteLive.Twitter //foreach (var descriptionUrl in user.Entities?.Description?.Urls?.OrderByDescending(x => x.URL.Length)) // description = description.Replace(descriptionUrl.URL, descriptionUrl.ExpandedURL); + var result = res.RootElement.GetProperty("data").GetProperty("user").GetProperty("result"); return new TwitterUser { - Id = long.Parse(res.RootElement.GetProperty("data").GetProperty("id").GetString()), - Acct = res.RootElement.GetProperty("data").GetProperty("username").GetString(), - Name = res.RootElement.GetProperty("data").GetProperty("name").GetString(), - Description = res.RootElement.GetProperty("data").GetProperty("description").GetString(), - Url = res.RootElement.GetProperty("data").GetProperty("url").GetString(), - ProfileImageUrl = res.RootElement.GetProperty("data").GetProperty("profile_image_url").GetString(), - ProfileBackgroundImageUrl = res.RootElement.GetProperty("data").GetProperty("profile_image_url").GetString(), //for now - ProfileBannerURL = res.RootElement.GetProperty("data").GetProperty("profile_image_url").GetString(), //for now - Protected = res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(), + Id = long.Parse(result.GetProperty("rest_id").GetString()), + Acct = username, + Name = result.GetProperty("legacy").GetProperty("name").GetString(), //res.RootElement.GetProperty("data").GetProperty("name").GetString(), + Description = "", //res.RootElement.GetProperty("data").GetProperty("description").GetString(), + Url = "", //res.RootElement.GetProperty("data").GetProperty("url").GetString(), + ProfileImageUrl = result.GetProperty("legacy").GetProperty("profile_image_url_https").GetString(), + ProfileBackgroundImageUrl = result.GetProperty("legacy").GetProperty("profile_banner_url").GetString(), + ProfileBannerURL = result.GetProperty("legacy").GetProperty("profile_banner_url").GetString(), + Protected = false, //res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(), }; } public bool IsUserApiRateLimited() { - // Retrieve limit from tooling - //_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); - //ExceptionHandler.SwallowWebExceptions = false; - //RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly; - - //try - //{ - // var queryRateLimits = RateLimit.GetQueryRateLimit("https://api.twitter.com/1.1/users/show.json?screen_name=mastodon"); - - // if (queryRateLimits != null) - // { - // return queryRateLimits.Remaining <= 0; - // } - //} - //catch (Exception e) - //{ - // _logger.LogError(e, "Error retrieving rate limits"); - //} - - //// Fallback - //var currentCalls = _statisticsHandler.GetCurrentUserCalls(); - //var maxCalls = _statisticsHandler.GetStatistics().UserCallsMax; - //return currentCalls >= maxCalls; return false; } } diff --git a/src/BirdsiteLive/appsettings.json b/src/BirdsiteLive/appsettings.json index 4cee8ba..f3aa5f7 100644 --- a/src/BirdsiteLive/appsettings.json +++ b/src/BirdsiteLive/appsettings.json @@ -30,13 +30,9 @@ "Db": { "Type": "postgres", "Host": "127.0.0.1", - "Name": "mydb", - "User": "username", - "Password": "password" - }, - "Twitter": { - "ConsumerKey": "twitter.api.key", - "ConsumerSecret": "twitter.api.key" + "Name": "birdsitelive", + "User": "birdsitelive", + "Password": "birdsitelive" }, "Moderation": { "FollowersWhiteListing": null,