@@ -26,7 +26,7 @@ public class WebhookDump : IPlugin
26
26
27
27
private readonly FileTracker _fileTracker ;
28
28
29
- private readonly HashSet < int > _episodesMissingReferences = [ ] ;
29
+ private readonly HashSet < int > _anidbTrackedEpisodeIds = [ ] ;
30
30
31
31
public static void ConfigureServices ( IServiceCollection services )
32
32
{
@@ -95,23 +95,27 @@ private void OnFileNotMatched(object sender, FileNotMatchedEventArgs fileNotMatc
95
95
private void OnFileMatched ( object sender , FileEventArgs fileMatchedEvent )
96
96
{
97
97
var video = fileMatchedEvent . Video ;
98
+ var series = fileMatchedEvent . Series ;
98
99
var episodes = fileMatchedEvent . Episodes ;
99
100
100
- if ( video . CrossReferences . Count == 0 && episodes . Count > 0 )
101
+ if ( episodes . Count == 0 && series . Count == 0 )
101
102
{
102
- _episodesMissingReferences . Add ( episodes [ 0 ] . ID ) ;
103
+ foreach ( var crossReference in video . CrossReferences )
104
+ _anidbTrackedEpisodeIds . Add ( crossReference . AnidbEpisodeID ) ;
103
105
return ;
104
106
}
105
107
106
- if ( fileMatchedEvent . Series . Count == 0 || fileMatchedEvent . Episodes . Count == 0 )
108
+ if ( episodes . Count == 0 || series . Count == 0 ) // This is just a safeguard...
107
109
{
108
- Logger . Debug ( $ "I don't think this should ever be seen, but if it is... Let @fearnlj01 know it can happen (please).") ;
110
+ // I don't know in what circumstance this code will execute...
111
+ Logger . Error ( "I don't think this should ever be seen, but if it is... Let @fearnlj01 know it can happen (please)." ) ;
109
112
return ;
110
113
}
111
114
112
- AttemptMatchedUpdate ( fileMatchedEvent . Series [ 0 ] , fileMatchedEvent . Episodes [ 0 ] , video ) ;
115
+ AttemptMatchedUpdate ( series [ 0 ] , episodes [ 0 ] , video ) ;
113
116
}
114
117
118
+ #nullable enable
115
119
private async void AttemptMatchedUpdate ( ISeries series , IEpisode episode , IVideo video )
116
120
{
117
121
if (
@@ -126,7 +130,7 @@ private async void AttemptMatchedUpdate(ISeries series, IEpisode episode, IVideo
126
130
try
127
131
{
128
132
var poster = await _shokoHelper . GetSeriesPoster ( series ) ;
129
- var imageStream = await _shokoHelper . GetImageStream ( poster ) ;
133
+ var imageStream = poster != null ? await _shokoHelper . GetImageStream ( poster ) : null ;
130
134
131
135
await _discordHelper . PatchWebhook ( video , series , episode , imageStream , messageId ) ;
132
136
_messageTracker . TryRemoveMessage ( video . ID ) ;
@@ -136,19 +140,16 @@ private async void AttemptMatchedUpdate(ISeries series, IEpisode episode, IVideo
136
140
Logger . Debug ( "Exception: {ex}" , ex ) ;
137
141
}
138
142
}
143
+ #nullable disable
139
144
140
145
private async void OnAVDumpEvent ( object sender , AVDumpEventArgs dumpEvent )
141
146
{
142
147
if ( dumpEvent . Type != AVDumpEventType . Success || ! _settings . Webhook . Enabled )
143
148
return ;
144
149
145
- for ( var i = 0 ; i < dumpEvent . VideoIDs ? . Count && i < dumpEvent . ED2Ks ? . Count ; i ++ )
150
+ foreach ( var videoId in dumpEvent . VideoIDs ?? Array . Empty < int > ( ) )
146
151
{
147
- var ed2K = dumpEvent . ED2Ks [ i ] ;
148
- var fileId = dumpEvent . VideoIDs [ i ] ;
149
-
150
- if ( ! _fileTracker . TryGetValue ( fileId , out var video ) )
151
- continue ;
152
+ if ( ! _fileTracker . TryGetValue ( videoId , out var video ) ) continue ;
152
153
153
154
var searchResult = await _shokoHelper . MatchTitle ( video . EarliestKnownName ) ;
154
155
@@ -174,27 +175,28 @@ private async void OnAVDumpEvent(object sender, AVDumpEventArgs dumpEvent)
174
175
} ) ;
175
176
176
177
searchResult . List = searchResult . List . Take ( 3 ) . ToList ( ) ;
177
- var messageId = await _discordHelper . SendWebhook ( video , ed2K , searchResult ) ;
178
+ var messageId = await _discordHelper . SendWebhook ( video , searchResult ) ;
178
179
179
180
_ = _messageTracker . TryAddMessage ( video . ID , messageId ) ;
180
181
}
181
182
}
182
183
183
184
private void OnEpisodeUpdatedEvent ( object sender , EpisodeInfoUpdatedEventArgs episodeInfoUpdatedEvent )
184
185
{
185
- var episodeInfo = episodeInfoUpdatedEvent . EpisodeInfo ;
186
- var updateReason = episodeInfoUpdatedEvent . Reason ;
187
- var seriesInfo = episodeInfoUpdatedEvent . SeriesInfo ;
186
+ if ( episodeInfoUpdatedEvent . Reason is not ( UpdateReason . Added or UpdateReason . Updated ) ) return ;
188
187
189
- if ( ! _episodesMissingReferences . Contains ( episodeInfo . ID ) || episodeInfo . VideoList . Count == 0 ||
190
- ( updateReason != UpdateReason . Added && updateReason != UpdateReason . Updated )
191
- )
192
- {
193
- return ;
194
- }
188
+ var episode = episodeInfoUpdatedEvent . EpisodeInfo ;
189
+ var series = episodeInfoUpdatedEvent . SeriesInfo ;
190
+
191
+ var anidbEpisodeIds = episode . CrossReferences . Select ( reference => reference . AnidbEpisodeID ) . ToHashSet ( ) ;
192
+
193
+ if ( episode . VideoList . Count == 0 ) return ;
194
+
195
+ var removedLog = anidbEpisodeIds . Select ( eid => _anidbTrackedEpisodeIds . Remove ( eid ) ) . ToList ( ) ;
196
+ if ( ! removedLog . Any ( removedBool => removedBool ) ) return ;
195
197
196
- AttemptMatchedUpdate ( seriesInfo , episodeInfo , episodeInfo . VideoList [ 0 ] ) ;
197
- _episodesMissingReferences . Remove ( episodeInfo . ID ) ;
198
+ Logger . Debug ( "Attempting to update webhook by episode added/updated." ) ;
199
+ AttemptMatchedUpdate ( series , episode , episode . VideoList [ 0 ] ) ;
198
200
}
199
201
200
202
private async Task WaitForRescan ( IVideo video , int matchAttempts = 1 )
0 commit comments