### BangumiNet.Api: Setup API Client Source: https://context7.com/ajtn123/banguminet/llms.txt Sets up the API client for BangumiNet, supporting Legacy API, Open API V0, and Private API P1. Authentication can be handled via Bearer token or session cookie. Requires a valid User-Agent header. ```csharp using BangumiNet.Api; using BangumiNet.Api.P1; using BangumiNet.Api.V0; using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary; // Get your access token from: https://next.bgm.tv/demo/access-token string accessToken = "your_access_token_here"; // Create authentication provider (supports both Bearer token and session cookie) var authProvider = new BangumiAuthenticationProvider(accessToken); // For anonymous access (no authentication required) // var authProvider = new AnonymousAuthenticationProvider(); // Create HttpClient with required User-Agent header // User-Agent format: AppName/Version (URL) or AppName/Version (Contact) var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.TryAddWithoutValidation( "User-Agent", "BangumiNet/1.0.0 (https://github.com/ajtn123/BangumiNet)" ); // Create API clients - each client needs its own RequestAdapter var requestAdapterP1 = new HttpClientRequestAdapter(authProvider, httpClient: httpClient); var clientP1 = new BangumiNet.Api.P1.ApiClient(requestAdapterP1); var requestAdapterV0 = new HttpClientRequestAdapter(authProvider, httpClient: httpClient); var clientV0 = new BangumiNet.Api.V0.ApiClient(requestAdapterV0); // Expected: clientP1 and clientV0 ready for API calls ``` -------------------------------- ### BangumiNet.Api: Get Subject Details Source: https://context7.com/ajtn123/banguminet/llms.txt Retrieves detailed information about a specific subject (anime, book, game, etc.) using its ID from the Bangumi API. Includes metadata, ratings, collection statistics, and summary. Requires an initialized API client. ```csharp using BangumiNet.Api.V0; using BangumiNet.Common; // Get subject by ID (e.g., 9912 = "Code Geass") int subjectId = 9912; var subject = await clientV0.V0.Subjects[subjectId].GetAsync(); Console.WriteLine($"Name: {subject?.Name}"); Console.WriteLine($"Chinese Name: {subject?.NameCn}"); Console.WriteLine($"Type: {(SubjectType?)subject?.Type}"); Console.WriteLine($"Summary: {subject?.Summary?.Substring(0, 100)}..."); Console.WriteLine($"Rating: {subject?.Rating?.Score} ({subject?.Rating?.Total} votes)"); Console.WriteLine($"Rank: #{subject?.Rating?.Rank}"); Console.WriteLine($"Air Date: {subject?.Date}"); // Access collection statistics Console.WriteLine($"Wish: {subject?.Collection?.Wish}"); Console.WriteLine($"Done: {subject?.Collection?.Done}"); Console.WriteLine($"Doing: {subject?.Collection?.Doing}"); // Expected output: // Name: コードギアス 反逆のルルーシュ // Chinese Name: Code Geass 反叛的鲁路修 // Type: Anime // Summary: 超大国ブリタニア帝国に占領された日本... // Rating: 8.9 (12500 votes) // Rank: #15 // Air Date: 2006-10-05 ``` -------------------------------- ### BangumiNet.Api: Get Calendar (Airing Schedule) Source: https://context7.com/ajtn123/banguminet/llms.txt Retrieves the weekly airing schedule for anime shows from the Bangumi API. The data is organized by day of the week and includes details about the anime airing on each day. Requires an initialized API client. ```csharp using BangumiNet.Api.P1; // Fetch the calendar data var calendar = await clientP1.P1.Calendar.GetAsync(); // calendar contains an array of days, each with anime airing that day foreach (var day in calendar ?? []) { Console.WriteLine($"Day: {day.Weekday?.Id} - {day.Weekday?.Cn}"); foreach (var item in day.Items ?? []) { Console.WriteLine($" - {item.NameCn ?? item.Name} (ID: {item.Id})"); } } // Expected output: // Day: 1 - 星期一 // - 葬送的芙莉莲 (ID: 400602) // - 药屋少女的呢喃 (ID: 425998) // Day: 2 - 星期二 // - ... ``` -------------------------------- ### API Pagination Helper with Limit/Offset (C#) Source: https://context7.com/ajtn123/banguminet/llms.txt Demonstrates how to use the BangumiNet.Api.Helpers for paginated API requests. It shows methods like SetPage, Paging, Limit, and Offset to control the data retrieval. ```csharp using BangumiNet.Api.Helpers; // Using SetPage helper (page number starts from 1) var page1 = await clientV0.V0.Subjects.GetAsync(config => { config.SetPage(page: 1, size: 25); // Gets items 0-24 config.QueryParameters.Type = 2; // Anime }); var page2 = await clientV0.V0.Subjects.GetAsync(config => { config.SetPage(page: 2, size: 25); // Gets items 25-49 config.QueryParameters.Type = 2; }); // Using Paging helper directly with limit/offset var results = await clientP1.P1.Users["sai"].Collections.Subjects.GetAsync( config => config.Paging(limit: 50, offset: 100) ); // Using individual Limit/Offset helpers var customPage = await clientV0.V0.Characters.GetAsync(config => { config.Limit(10); config.Offset(20); }); ``` -------------------------------- ### Load Anime Metadata from bangumi-data (C#) Source: https://context7.com/ajtn123/banguminet/llms.txt Demonstrates how to load anime broadcast metadata from the bangumi-data project for streaming site links and airing schedules. It shows loading from a CDN and individual files. ```csharp using BangumiNet.BangumiData; using BangumiNet.BangumiData.Models; // Load from CDN using var httpClient = new HttpClient(); await using var stream = await httpClient.GetStreamAsync( "https://unpkg.com/bangumi-data@0.3/dist/data.json" ); var data = await BangumiDataLoader.LoadAsync(stream); Console.WriteLine($"Loaded {data.Items.Length} anime items"); Console.WriteLine($"Site metadata for {data.SiteMeta.Count} streaming sites"); // Find anime by title var anime = data.Items.FirstOrDefault(i => i.Title.Contains("Frieren", StringComparison.OrdinalIgnoreCase)); if (anime.Sites != null) { Console.WriteLine($"Title: {anime.Title}"); Console.WriteLine($"Type: {anime.ItemType}"); Console.WriteLine($"Language: {anime.Language}"); Console.WriteLine($"Begin: {anime.Begin}"); Console.WriteLine($"Broadcast: {anime.Broadcast}"); // Get streaming site links foreach (var site in anime.Sites) { var siteMeta = data.SiteMeta.GetValueOrDefault(site.SiteName); Console.WriteLine($" {siteMeta?.Title}: {site.Id}"); } } // Load individual files var items = await BangumiDataLoader.LoadItemsAsync( await httpClient.GetStreamAsync("https://unpkg.com/bangumi-data@0.3/dist/items.json") ); var sites = await BangumiDataLoader.LoadSitesAsync( await httpClient.GetStreamAsync("https://unpkg.com/bangumi-data@0.3/dist/sites.json") ); ``` -------------------------------- ### Search Subjects using Keywords and Filters (C#) Source: https://context7.com/ajtn123/banguminet/llms.txt Searches for subjects (anime, books, etc.) using keywords and applies filters such as subject type. It demonstrates how to specify search criteria and paginate results. Dependencies include the BangumiNet.Api library. ```csharp using BangumiNet.Api.V0.V0.Search.Subjects; // Search for anime with keyword "Gundam" var searchResults = await clientV0.V0.Search.Subjects.PostAsync( new SubjectsPostRequestBody { Keyword = "Gundam", Filter = new SubjectsPostRequestBody_filter { Type = [2] // 2 = Anime (1=Book, 3=Music, 4=Game, 6=Real) } }, config => { config.QueryParameters.Limit = 10; config.QueryParameters.Offset = 0; } ); Console.WriteLine($"Total results: {searchResults?.Total}"); foreach (var item in searchResults?.Data ?? []) { Console.WriteLine($"[{item.Id}] {item.NameCn ?? item.Name} ({item.Date})"); Console.WriteLine($" Score: {item.Score}, Rank: #{item.Rank}"); } // Expected output: // Total results: 245 // [253] 机动战士高达 (1979-04-07) // Score: 8.2, Rank: #156 // [324] 机动战士高达SEED (2002-10-05) // Score: 7.8, Rank: #892 ``` -------------------------------- ### Using Subject Types and Enums in C# Source: https://context7.com/ajtn123/banguminet/llms.txt Demonstrates the usage of various common enums within the BangumiNet.Common and BangumiNet.Api.ExtraEnums namespaces. This includes SubjectType, AnimeType, GamePlatform, CollectionType, EpisodeCollectionType, TimelineCategory, and FilterMode. The code shows how to instantiate these enums and print their string representations, including custom SC (Simplified Chinese) formatting for certain types. ```csharp using BangumiNet.Common; using BangumiNet.Api.ExtraEnums; // Subject types SubjectType type = SubjectType.Anime; Console.WriteLine($"Type: {type}"); // Anime // Anime subtypes AnimeType animeType = AnimeType.TV; Console.WriteLine($"Anime Type: {animeType}"); // TV, Web, OVA, Movie // Game platforms GamePlatform platform = GamePlatform.NintendoSwitch; Console.WriteLine($"Platform: {platform}"); // NintendoSwitch // Collection types CollectionType status = CollectionType.Doing; Console.WriteLine($"Status: {status.ToStringSC()}"); // 在看 // Episode collection types EpisodeCollectionType epStatus = EpisodeCollectionType.Done; Console.WriteLine($"Episode: {epStatus.ToStringSC()}"); // 看过 // Timeline categories TimelineCategory category = TimelineCategory.Subject; Console.WriteLine($"Category: {category.ToStringSC()}"); // 收藏条目 // Timeline filter modes FilterMode mode = FilterMode.Friends; Console.WriteLine($"Filter: {mode.ToStringSC()}"); // 仅好友 ``` -------------------------------- ### Retrieve User Information and Collections (C#) Source: https://context7.com/ajtn123/banguminet/llms.txt Fetches profile information for the currently authenticated user and for other users by their username. It also demonstrates retrieving a user's subject collections, with options to filter by subject type and collection status. Uses BangumiNet.Api. ```csharp // Get current authenticated user info var me = await clientV0.V0.Me.GetAsync(); Console.WriteLine($"Username: {me?.Username}"); Console.WriteLine($"Nickname: {me?.Nickname}"); Console.WriteLine($"User ID: {me?.Id}"); Console.WriteLine($"Avatar: {me?.Avatar?.Large}"); // Get another user's profile by username var user = await clientV0.V0.Users["sai"].GetAsync(); Console.WriteLine($"User: {user?.Nickname} (@{user?.Username})"); Console.WriteLine($"Sign: {user?.Sign}"); // Get user's anime collection (watching) var collections = await clientP1.P1.Users["sai"].Collections.Subjects.GetAsync(config => { config.QueryParameters.SubjectType = 2; // Anime config.QueryParameters.Type = 3; // Doing (watching) config.QueryParameters.Limit = 20; }); foreach (var item in collections?.Data ?? []) { Console.WriteLine($"- {item.Subject?.NameCn ?? item.Subject?.Name}"); } ``` -------------------------------- ### Subscribe to Timeline Events via SSE (C#) Source: https://context7.com/ajtn123/banguminet/llms.txt Subscribes to real-time timeline events using Server-Sent Events (SSE) for live updates. This allows applications to react to changes as they happen. Requires BangumiNet.Api, P1.Models, ExtraEnums, and a CancellationToken for managing the stream lifecycle. ```csharp using BangumiNet.Api.Misc; using BangumiNet.Api.P1.Models; using BangumiNet.Api.ExtraEnums; // Create cancellation token for graceful shutdown var cts = new CancellationTokenSource(); // Initialize timeline event stream var events = new TimelineEventStream(httpClient, accessToken); // Subscribe to all timeline events Console.WriteLine("Listening for timeline events..."); try { await foreach (var timeline in events.StartAsync( FilterMode.All, // All = global, Friends = friends only TimelineCategory.Subject, // Filter by category (null for all) cts.Token)) { Console.WriteLine($"[{timeline.CreatedAt}] User {timeline.User?.Nickname}:"); Console.WriteLine($" Action: {timeline.Cat} - {timeline.Type}"); Console.WriteLine($" Memo: {timeline.Memo}"); // Process events... // Call cts.Cancel() when done to close the connection } } catch (OperationCanceledException) { Console.WriteLine("Timeline stream stopped."); } // IMPORTANT: Always cancel to close the SSE connection // cts.Cancel(); ``` -------------------------------- ### Load Wiki Archive Data (C#) Source: https://context7.com/ajtn123/banguminet/llms.txt Shows how to load data from Bangumi Wiki Archive JSON Lines files for offline access. It covers loading subjects, characters, and person-character relations using ArchiveLoader. ```csharp using BangumiNet.Archive; using BangumiNet.Archive.Models; // Download archive files from: https://github.com/bangumi/Archive // Load subjects (anime, books, games, etc.) await using var subjectStream = File.OpenRead("subject.jsonlines"); var subjects = await ArchiveLoader.Load(subjectStream).ToArrayAsync(); Console.WriteLine($"Loaded {subjects.Length} subjects"); foreach (var subject in subjects.Take(5)) { Console.WriteLine($"[{subject.Id}] {subject.NameCn ?? subject.Name}"); Console.WriteLine($" Type: {subject.Type}, Score: {subject.Score}, Rank: #{subject.Rank}"); Console.WriteLine($" Date: {subject.ReleaseDate}, NSFW: {subject.IsNsfw}"); } // Load characters await using var characterStream = File.OpenRead("character.jsonlines"); var characters = await ArchiveLoader.Load(characterStream).ToArrayAsync(); // Load person-character relations await using var relationStream = File.OpenRead("person-characters.jsonlines"); var relations = await ArchiveLoader.Load(relationStream).ToArrayAsync(); // Available archive types: // - Subject (subject.jsonlines) // - Person (person.jsonlines) // - Character (character.jsonlines) // - Episode (episode.jsonlines) // - SubjectRelation (subject-relations.jsonlines) // - SubjectCharacterRelation (subject-characters.jsonlines) // - SubjectPersonRelation (subject-persons.jsonlines) // - PersonCharacterRelation (person-characters.jsonlines) ``` -------------------------------- ### Manage User Collection Status (C#) Source: https://context7.com/ajtn123/banguminet/llms.txt Updates a user's collection status for a specific subject, allowing for setting status (wish, doing, done, etc.), rating, comments, and tags. It also shows how to mark individual episodes as collected. Requires BangumiNet.Api and ExtraEnums. ```csharp using BangumiNet.Api.ExtraEnums; // Update collection status for a subject int subjectId = 9912; // Set subject to "Doing" (currently watching) await clientV0.V0.Users.Minus.Collections[subjectId].PatchAsync(new() { Type = (int)CollectionType.Doing, // 1=Wish, 2=Done, 3=Doing, 4=OnHold, 5=Dropped Rate = 9, // Rating 1-10 Comment = "Amazing anime!", Private = false, // Public collection Tags = ["sci-fi", "mecha"] }); // Mark episode as watched int episodeId = 77560; await clientV0.V0.Users.Minus.Collections.Minus.Episodes[episodeId].PutAsync(new() { Type = (int)EpisodeCollectionType.Done // 0=Uncollected, 1=Wish, 2=Done, 3=Dropped }); Console.WriteLine("Collection updated successfully!"); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.