SocialCounter.X 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package SocialCounter.X --version 1.0.5                
NuGet\Install-Package SocialCounter.X -Version 1.0.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SocialCounter.X" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SocialCounter.X --version 1.0.5                
#r "nuget: SocialCounter.X, 1.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SocialCounter.X as a Cake Addin
#addin nuget:?package=SocialCounter.X&version=1.0.5

// Install SocialCounter.X as a Cake Tool
#tool nuget:?package=SocialCounter.X&version=1.0.5                

SocialCounter Icon

SocialCounter 📊

NuGet version

Welcome to SocialCounter, a high-performance .NET library designed to fetch and aggregate social media metrics across multiple platforms. Built with reliability and extensibility in mind, it provides a unified way to retrieve follower counts and other social metrics while handling rate limiting, retries, and failures gracefully.

Overview

The SocialCounter library offers:

  • Built-in Platform Support: Ready-to-use implementations for Instagram, X (Twitter), TikTok, Facebook, LinkedIn, and YouTube
  • Unified Interface: A single, consistent API to fetch social metrics across different platforms
  • Built-in Resilience: Automatic retry policies with exponential backoff and jitter
  • Parallel Processing: Concurrent requests to multiple social media platforms
  • Extensible Design: Easy to add new social media platform implementations
  • High Performance: Optimized for efficient HTTP operations with modern .NET features
  • Configurable Timeouts: Customizable timeout settings per platform or globally

Getting Started

Installation

Install the core SocialCounter package and any platform-specific packages you need:

dotnet add package SocialCounter
dotnet add package SocialCounter.Instagram
dotnet add package SocialCounter.X
dotnet add package SocialCounter.TikTok
dotnet add package SocialCounter.Facebook
dotnet add package SocialCounter.LinkedIn
dotnet add package SocialCounter.Youtube

Basic Setup

Configure SocialCounter in your ASP.NET Core application:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSocialCounters(options =>
    {
        options.Timeout = TimeSpan.FromSeconds(10);
        options.MaxRetryAttempts = 3;
        options.InitialRetryDelay = TimeSpan.FromSeconds(1);
    })
    .AddInstagramCounter()
    .AddXCounter()
    .AddTikTokCounter()
    .AddFacebookCounter()
    .AddLinkedInCounter()
    .AddYoutubeCounter();
}

Basic Usage

// Using individual platform clients
public class SocialMetricsService
{
    private readonly InstagramCounterClient _instagramClient;
    private readonly XCounterClient _xClient;
    private readonly SocialCounters _allCounters;

    public SocialMetricsService(
        InstagramCounterClient instagramClient,
        XCounterClient xClient,
        SocialCounters allCounters)
    {
        _instagramClient = instagramClient;
        _xClient = xClient;
        _allCounters = allCounters;
    }

    // Get metrics from a single platform
    public async Task<SocialCountResult> GetInstagramMetricsAsync(string handle)
    {
        return await _instagramClient.GetCount(handle, CancellationToken.None);
    }

    // Get metrics from all configured platforms concurrently
    public async Task<List<SocialCountResult>> GetAllMetricsAsync(string handle)
    {
        var results = await _allCounters.GetCountAsync(handle, CancellationToken.None);
        return results;
    }
}

Supported Platforms

Instagram

services.AddInstagramCounter();
await instagramClient.GetCount("username", CancellationToken.None);

X (Twitter)

services.AddXCounter();
await xClient.GetCount("username", CancellationToken.None);

TikTok

services.AddTikTokCounter();
await tiktokClient.GetCount("username", CancellationToken.None);

Facebook

services.AddFacebookCounter();
await facebookClient.GetCount("pagename", CancellationToken.None);

LinkedIn

services.AddLinkedInCounter();
await linkedInClient.GetCount("username", CancellationToken.None);

YouTube

services.AddYoutubeCounter();
await youtubeClient.GetCount("channelname", CancellationToken.None);

Features

Parallel Processing with Error Handling

The SocialCounters class provides a unified way to fetch metrics from all configured platforms concurrently:

public sealed class SocialCounters
{
    private readonly IEnumerable<SocialMediaClient> counters;
    private readonly ILogger<SocialCounters> logger;

    public SocialCounters(IEnumerable<SocialMediaClient> counters, ILogger<SocialCounters> logger)
    {
        this.counters = counters;
        this.logger = logger;
    }

    public async Task<List<SocialCountResult>> GetCountAsync(
        string handle,
        CancellationToken cancellationToken
    )
    {
        var results = new List<SocialCountResult>();

        await Task.WhenAll(
            this.counters.Select(async counter =>
            {
                try
                {
                    var result = await counter.GetCount(handle, cancellationToken);
                    results.Add(result);
                }
                catch (Exception ex)
                {
                    // Log and continue
                    this.logger.LogError(
                        ex,
                        "Failed to get count for {Platform} handle {Handle}",
                        counter.Platform,
                        handle
                    );
                }
            })
        );

        return results;
    }
}

This implementation ensures:

  • Concurrent processing of all platform requests
  • Graceful error handling per platform
  • Failed requests don't block other platforms
  • Comprehensive logging of failures

Resilient HTTP Operations

SocialCounter includes built-in resilience features:

  • Automatic retries for transient failures
  • Exponential backoff with jitter
  • Configurable timeout policies
  • Comprehensive error handling and logging

Supported HTTP Status Code Retries

  • 408 Request Timeout
  • 502 Bad Gateway
  • 503 Service Unavailable
  • 504 Gateway Timeout
  • 429 Too Many Requests

Configuration Options

The SocialCounterOptions class provides various configuration settings:

Option Description Default
Timeout Maximum time for any single request 10 seconds
MaxRetryAttempts Maximum number of retry attempts 3
InitialRetryDelay Initial delay between retries 1 second
BaseAddress Base URL for HTTP requests Empty
DefaultHeaders Default headers for all requests Empty Dictionary

Advanced Usage

Custom Configuration

services.AddSocialCounters(options =>
{
    options.Timeout = TimeSpan.FromSeconds(15);
    options.DefaultHeaders.Add("User-Agent", "MyApp/1.0");
    options.BaseAddress = "https://api.example.com";
})
.ConfigureHttpClient((provider, client) =>
{
    client.DefaultRequestHeaders.Add("Authorization", "Bearer token");
});

Implementing a Custom Platform Client

public class TwitterClient : SocialMediaClient
{
    public TwitterClient(HttpClient httpClient, ILogger<TwitterClient> logger) 
        : base(httpClient, logger)
    {
    }

    public override string Platform => "Twitter";

    public override async Task<SocialCountResult> GetCount(
        string handle, 
        CancellationToken cancellationToken)
    {
        var response = await GetAsync($"/users/{handle}/metrics", cancellationToken);
        // Process response and return count
        return new SocialCountResult(Platform, handle, followersCount);
    }
}

Error Handling

SocialCounter provides comprehensive error handling:

  • Failed requests are logged with detailed information
  • Exceptions don't break the entire operation
  • Each platform is processed independently
  • Empty results are returned for failed platforms

Performance Considerations

  • Parallel processing of multiple social media platforms
  • Efficient HTTP client usage with connection pooling
  • Optimized retry policies to prevent unnecessary delays
  • Configurable timeouts to prevent long-running requests

Requirements

  • .NET 8.0 or higher
  • ASP.NET Core for dependency injection
  • HTTP client support

Best Practices

  1. Configure appropriate timeouts for your use case
  2. Implement platform-specific error handling
  3. Use cancellation tokens for long-running operations
  4. Monitor and log failed requests
  5. Handle rate limiting appropriately

License

This library is available under the MIT License.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and feature requests, please open an issue in the GitHub repository.


Thank you for using SocialCounter. We look forward to seeing how you use it in your projects!

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 87 12/2/2024
1.0.5 78 12/2/2024
1.0.4 85 12/2/2024
1.0.3 86 12/1/2024
1.0.2 90 12/1/2024
1.0.0 92 12/1/2024