Skip to content

Commit

Permalink
supporting vote api for aniskip.
Browse files Browse the repository at this point in the history
respect auto update settings.
  • Loading branch information
insomniachi committed Jan 13, 2023
1 parent c8340d6 commit 0f834a7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 17 deletions.
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/ITimestampsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public interface ITimestampsService
{
Task<AniSkipResult> GetTimeStamps(long id, int ep, double duration);
Task SubmitTimeStamp(long id, int ep, string skipType, Interval interval, double episodeLength);
Task Vote(string skipId, bool isThumpsUp);
}
}
2 changes: 1 addition & 1 deletion Totoro.Core/Contracts/IViewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IViewService
Task Authenticate(ListServiceType type);
Task PlayVideo(string title, string url);
Task<T> SelectModel<T>(IEnumerable<object> models) where T : class;
Task SubmitTimeStamp(long malId, int ep, VideoStream stream, double duration, double introStart);
Task SubmitTimeStamp(long malId, int ep, VideoStream stream, AniSkipResult existingResult, double duration, double introStart);
Task<bool> Question(string title, string message);
Task<Unit> Information(string title, string message);
}
18 changes: 18 additions & 0 deletions Totoro.Core/Services/TimestampsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ public async Task SubmitTimeStamp(long id, int ep, string skipType, Interval int
this.Log().Info("Submitted : {0}", response.IsSuccessStatusCode);
}

public async Task Vote(string skipId, bool isThumpsUp)
{
var voteType = isThumpsUp ? "upvote" : "downvote";
var postData = new Dictionary<string, string>()
{
["voteType"] = voteType
};

using var content = new FormUrlEncodedContent(postData);
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

using var request = new HttpRequestMessage(HttpMethod.Post, $"https://api.aniskip.com/v2/skip-times/vote/{skipId}");
request.Content = content;
var response = await _httpClient.SendAsync(request);
this.Log().Info($"Vote ({voteType}) submitted");
}

private async ValueTask<long> GetMalId(long id)
{
if (_settings.DefaultListService == ListServiceType.MyAnimeList)
Expand Down
6 changes: 4 additions & 2 deletions Totoro.Core/Services/WindowsUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public class WindowsUpdateService : ReactiveObject, IUpdateService, IEnableLogge
public IObservable<VersionInfo> OnUpdateAvailable => _onUpdate;

public WindowsUpdateService(HttpClient httpClient,
ISettings settings)
ILocalSettingsService localSettingsService)
{
var autoUpdateEnabled = localSettingsService.ReadSetting(nameof(ISettings.AutoUpdate), true);

_httpClient = httpClient;
_onUpdate = Observable
.Timer(TimeSpan.Zero, TimeSpan.FromHours(1))
.Where(_ => settings.AutoUpdate)
.Where(_ => autoUpdateEnabled)
.ObserveOn(RxApp.TaskpoolScheduler)
.SelectMany(_ => httpClient.GetStreamAsync("https://api.github.com/repos/athulrajts/AnimDL.GUI/releases/latest"))
.Select(x => JsonNode.Parse(x))
Expand Down
2 changes: 1 addition & 1 deletion Totoro.Core/ViewModels/WatchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private void OnSubmitTimeStamps()
RxApp.MainThreadScheduler.Schedule(async () =>
{
MediaPlayer.Pause();
await _viewService.SubmitTimeStamp(Anime.Id, CurrentEpisode.Value, SelectedStream, CurrentMediaDuration, _userSkipOpeningTime == 0 ? 0 : _userSkipOpeningTime - 5);
await _viewService.SubmitTimeStamp(Anime.Id, CurrentEpisode.Value, SelectedStream, AniSkipResult, CurrentMediaDuration, _userSkipOpeningTime == 0 ? 0 : _userSkipOpeningTime - 5);
MediaPlayer.Play();
});
}
Expand Down
47 changes: 40 additions & 7 deletions Totoro.WinUI/Dialogs/ViewModels/SubmitTimeStampsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using Totoro.WinUI.Media;
using Totoro.WinUI.Media;

namespace Totoro.WinUI.Dialogs.ViewModels;

Expand All @@ -18,6 +17,22 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService)
SkipNearEnd = ReactiveCommand.Create(() => MediaPlayer.Seek(TimeSpan.FromSeconds(EndPosition - 5)));
Submit = ReactiveCommand.CreateFromTask(SubmitTimeStamp);

var canVote = this.WhenAnyValue(x => x.SelectedTimeStampType, x => x.ExistingResult)
.Where(x => x.Item2 is not null)
.Select(tuple =>
{
(string type, AniSkipResult result) = tuple;
return type switch
{
"OP" => result.Opening is not null,
"ED" => result.Ending is not null,
_ => false
};
});

VoteUp = ReactiveCommand.Create(() => Vote(true), canVote);
VoteDown = ReactiveCommand.Create(() => Vote(false), canVote);

MediaPlayer
.PositionChanged
.ObserveOn(RxApp.MainThreadScheduler)
Expand All @@ -27,18 +42,21 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService)
.DistinctUntilChanged()
.Subscribe(x => MediaPlayer.Seek(TimeSpan.FromSeconds(x)));

this.WhenAnyValue(x => x.SelectedTimeStampType)
.Subscribe(type =>
this.WhenAnyValue(x => x.SelectedTimeStampType, x => x.ExistingResult)
.Subscribe(tuple =>
{
(string type, AniSkipResult result) = tuple;

if (type == "OP")
{
StartPosition = SuggestedStartPosition;
StartPosition = result?.Opening?.Interval?.StartTime ?? SuggestedStartPosition;
EndPosition = result?.Opening?.Interval?.EndTime ?? StartPosition + 90;
}
else if (type == "ED")
{
StartPosition = SuggestedEndPosition;
StartPosition = result?.Ending?.Interval?.StartTime ?? SuggestedEndPosition;
EndPosition = result?.Ending?.Interval?.EndTime ?? StartPosition + 90;
}
EndPosition = StartPosition + 90;
});

this.WhenAnyValue(x => x.Stream)
Expand All @@ -51,6 +69,7 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService)
[Reactive] public string SelectedTimeStampType { get; set; } = "OP";
[Reactive] public double CurrentPlayerPosition { get; set; }
[Reactive] public VideoStream Stream { get; set; }
[Reactive] public AniSkipResult ExistingResult { get; set; }
public long MalId { get; set; }
public int Episode { get; set; }
public double Duration { get; set; }
Expand All @@ -64,6 +83,8 @@ public SubmitTimeStampsViewModel(ITimestampsService timestampsService)
public ICommand SetEndPosition { get; }
public ICommand SkipNearEnd { get; }
public ICommand Submit { get; }
public ICommand VoteUp { get; }
public ICommand VoteDown { get; }

private void Play()
{
Expand All @@ -79,6 +100,18 @@ private void Play()
});
}

private void Vote(bool vote)
{
if (SelectedTimeStampType == "OP" && ExistingResult.Opening is { } op)
{
_timestampsService.Vote(op.SkipId, vote);
}
else if (SelectedTimeStampType == "ED" && ExistingResult.Ending is { } ed)
{
_timestampsService.Vote(ed.SkipId, vote);
}
}

public async Task SubmitTimeStamp()
{
await _timestampsService.SubmitTimeStamp(MalId, Episode, SelectedTimeStampType.ToLower(), new Interval { StartTime = StartPosition, EndTime = EndPosition }, Duration);
Expand Down
20 changes: 16 additions & 4 deletions Totoro.WinUI/Dialogs/Views/SubmitTimeStampsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,22 @@
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
</Grid>
<ComboBox
Width="360"
ItemsSource="{x:Bind ViewModel.TimeStampTypes}"
SelectedItem="{x:Bind ViewModel.SelectedTimeStampType, Mode=TwoWay}" />

<StackPanel
HorizontalAlignment="Center"
Orientation="Horizontal"
Spacing="3">
<ComboBox
Width="264"
ItemsSource="{x:Bind ViewModel.TimeStampTypes}"
SelectedItem="{x:Bind ViewModel.SelectedTimeStampType, Mode=TwoWay}" />
<Button Command="{x:Bind ViewModel.VoteUp}">
<FontIcon Foreground="Green" Glyph="&#xeb0f;" />
</Button>
<Button Command="{x:Bind ViewModel.VoteDown}">
<FontIcon Foreground="Red" Glyph="&#xeb11;" />
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="166" Text="{x:Bind ViewModel.StartPosition, Mode=TwoWay}" />
<TextBlock
Expand Down
5 changes: 3 additions & 2 deletions Totoro.WinUI/Services/ViewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ await _contentDialogService.ShowDialog(vm, d =>
return vm.SelectedModel as T;
}

public async Task SubmitTimeStamp(long malId, int ep, VideoStream stream, double duration, double introStart)
public async Task SubmitTimeStamp(long malId, int ep, VideoStream stream, AniSkipResult existingResult, double duration, double introStart)
{
var vm = new SubmitTimeStampsViewModel(App.GetService<ITimestampsService>()) // TODO fix later
{
Expand All @@ -122,7 +122,8 @@ public async Task SubmitTimeStamp(long malId, int ep, VideoStream stream, double
StartPosition = introStart,
SuggestedStartPosition = introStart,
EndPosition = introStart + 85,
Duration = duration
Duration = duration,
ExistingResult = existingResult
};

await _contentDialogService.ShowDialog(vm, d =>
Expand Down

0 comments on commit 0f834a7

Please sign in to comment.