Skip to content

Commit 35f9dc4

Browse files
committed
Feat: barely working scraper
1 parent 24ce9f1 commit 35f9dc4

19 files changed

+420
-34
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ jobs:
2121
dotnet-version: '9.x'
2222

2323
- name: Restore dependencies
24-
run: dotnet restore TEMPLATE.sln
24+
run: dotnet restore CodeOfChaos.Twitch.sln
2525

2626
- name: Build
27-
run: dotnet build TEMPLATE.sln --configuration Release --no-restore
27+
run: dotnet build CodeOfChaos.Twitch.sln --configuration Release --no-restore
2828

2929
# Ensure that the tests must pass
3030
# The job will fail automatically if any test fails because `dotnet test` exits with a non-zero code
3131
- name: Run tests - Extensions
3232
run: dotnet run -c Release --no-restore --no-build
33-
working-directory: "tests/Tests.TEMPLATE"
33+
working-directory: "tests/Tests.CodeOfChaos.Twitch"
3434

3535
- name: Publish to NuGet
3636
env:
3737
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
3838
run: |
39-
dotnet nuget push src/TEMPLATE/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate
39+
dotnet nuget push src/CodeOfChaos.Twitch/bin/Release/*.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate

TEMPLATE.sln renamed to CodeOfChaos.Twitch.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TEMPLATE", "src\TEMPLATE\TEMPLATE.csproj", "{64B26DED-68C3-47FF-B409-1C8FAD4F9176}"
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeOfChaos.Twitch", "src\CodeOfChaos.Twitch\CodeOfChaos.Twitch.csproj", "{64B26DED-68C3-47FF-B409-1C8FAD4F9176}"
44
EndProject
5-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.TEMPLATE", "tests\Tests.TEMPLATE\Tests.TEMPLATE.csproj", "{26284571-0E09-4BAF-8C2B-DF87DCC1BA0B}"
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.CodeOfChaos.Twitch", "tests\Tests.CodeOfChaos.Twitch\Tests.CodeOfChaos.Twitch.csproj", "{26284571-0E09-4BAF-8C2B-DF87DCC1BA0B}"
66
EndProject
77
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{197E72AD-DEAB-4350-AFC3-A3BB38720BF5}"
88
EndProject
99
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{8DD280D4-1E14-4D5E-AFE6-58DD8F079DCC}"
1010
EndProject
1111
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{AF1A203C-6EF1-440E-BB3C-55B1DBFE9C19}"
1212
EndProject
13-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tools.TEMPLATE", "src\Tools.TEMPLATE\Tools.TEMPLATE.csproj", "{ADEADD97-0AFA-4D9E-970B-9FFB932949B3}"
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tools.CodeOfChaos.Twitch", "src\Tools.CodeOfChaos.Twitch\Tools.CodeOfChaos.Twitch.csproj", "{ADEADD97-0AFA-4D9E-970B-9FFB932949B3}"
1414
EndProject
1515
Global
1616
GlobalSection(SolutionConfigurationPlatforms) = preSolution

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
Go into src/tools.CodeOfChaos.Twitch and open up a terminal here, then execute the following: ( see [here](https://playwright.dev/dotnet/docs/intro) why this is needed)
3+
````cmd
4+
pwsh bin/Debug/net8.0/playwright.ps1 install
5+
````
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="AterraEngine.Unions" Version="3.8.0" />
12+
<PackageReference Include="AterraEngine.Unions.Generators" Version="3.8.0">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
16+
<PackageReference Include="RestSharp" Version="112.1.0" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using AterraEngine.Unions;
5+
using RestSharp;
6+
using System.Net;
7+
8+
namespace CodeOfChaos.Twitch;
9+
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
// Code
12+
// ---------------------------------------------------------------------------------------------------------------------
13+
public class TwitchApiClient(string clientId, string clientSecret ) {
14+
private readonly RestClient _client = new(new RestClientOptions("https://api.twitch.tv/helix/"));
15+
16+
public async ValueTask<StartCommercialResult> StartCommercialAsync(string broadcasterId, int length, CancellationToken ct = default) {
17+
var request = new RestRequest("channels/commercial", Method.Get)
18+
.AddHeader("Client-ID", clientId)
19+
.AddHeader("Authorization", $"Bearer {clientSecret}");
20+
21+
request.AddJsonBody(new Dictionary<string,string> {
22+
["broadcaster_id"] = broadcasterId,
23+
["length"]= length.ToString()
24+
});
25+
26+
RestResponse<StartCommercialResponse> response = await _client.ExecuteAsync<StartCommercialResponse>(request, ct);
27+
return response switch {
28+
{ IsSuccessful: true, Data: not null } => new Ok<StartCommercialResponse>(response.Data),
29+
{ StatusCode : HttpStatusCode.Unauthorized } => new Unauthorized(),
30+
{ StatusCode : HttpStatusCode.NotFound } => new NotFound(),
31+
{ StatusCode : HttpStatusCode.TooManyRequests } => new TooManyRequests(),
32+
33+
// Undocumented response
34+
_ => throw new NotImplementedException(response.Content)
35+
};
36+
}
37+
}
38+
39+
public record StartCommercialResponse(int Length, string Message, int RetryAfter);
40+
41+
public record struct Ok<T>(T Value) : IValue<T>;
42+
43+
public record struct BadRequest();
44+
public record struct Unauthorized();
45+
public record struct Forbidden();
46+
public record struct NotFound();
47+
public record struct InternalServerError();
48+
public record struct ServiceUnavailable();
49+
public record struct GatewayTimeout();
50+
public record struct TooManyRequests();
51+
52+
public readonly partial record struct StartCommercialResult() : IUnion<Ok<StartCommercialResponse>, BadRequest, Unauthorized, NotFound, TooManyRequests> {
53+
54+
}

src/TEMPLATE/TEMPLATE.csproj

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using System.Xml.Serialization;
5+
6+
namespace Tools.CodeOfChaos.Twitch.Commands.TwitchApiReferenceScraper.Dto;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class RequestBodyParameter() {
11+
[XmlAttribute] public string Name { get; init; }
12+
[XmlAttribute] public string Type { get; init; }
13+
[XmlText] public string Description { get; init; }
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using System.Xml.Serialization;
5+
6+
namespace Tools.CodeOfChaos.Twitch.Commands.TwitchApiReferenceScraper.Dto;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class RequestQueryParameter {
11+
[XmlAttribute] public string Name { get; init; }
12+
[XmlAttribute] public string Type { get; init; }
13+
[XmlAttribute] public bool Required { get; init; }
14+
[XmlText] public string Description { get; init; }
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using System.Xml.Serialization;
5+
6+
namespace Tools.CodeOfChaos.Twitch.Commands.TwitchApiReferenceScraper.Dto;
7+
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class ResponseBodyParameter() {
12+
[XmlAttribute]public string Name { get; init; }
13+
[XmlAttribute]public string Type { get; init; }
14+
[XmlText]public string Description { get; init; }
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using System.Xml.Serialization;
5+
6+
namespace Tools.CodeOfChaos.Twitch.Commands.TwitchApiReferenceScraper.Dto;
7+
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
// Code
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
public class TwitchApiDocumentation() {
12+
public string Name { get; init; }
13+
public string RestCommand { get; init; }
14+
public string Url { get; init; }
15+
public string[] AuthScopes { get; init; }
16+
public bool RequiresOAuth { get; init; }
17+
public RequestQueryParameter[] QueryParameters { get; init; }
18+
public RequestBodyParameter[] BodyParameters { get; init; }
19+
public ResponseBodyParameter[] ResponseParameters { get; init; }
20+
public string Description { get; init; }
21+
}

0 commit comments

Comments
 (0)