10
10
using System . Text . Json . Serialization ;
11
11
using System ;
12
12
using CustomMetadataDB . Helpers . Configuration ;
13
+ using System . Text . RegularExpressions ;
13
14
14
15
namespace CustomMetadataDB . Helpers
15
16
{
@@ -36,6 +37,7 @@ public static PersonInfo CreatePerson(string name, string provider_id)
36
37
ProviderIds = new ProviderIdDictionary ( new Dictionary < string , string > { { Constants . PLUGIN_EXTERNAL_ID , provider_id } } ) ,
37
38
} ;
38
39
}
40
+
39
41
public static MetadataResult < Series > ToSeries ( DTO data )
40
42
{
41
43
Logger ? . Info ( $ "Processing { data } .") ;
@@ -90,13 +92,143 @@ public static MetadataResult<Series> ToSeries(DTO data)
90
92
Item = item
91
93
} ;
92
94
}
93
- private static MetadataResult < Series > ErrorOut ( )
95
+
96
+ public static EpisodeInfo FileToInfo ( string file , DateTime ? file_date = null )
94
97
{
95
- return new MetadataResult < Series >
98
+
99
+ //-- get only the file stem
100
+ string filename = System . IO . Path . GetFileNameWithoutExtension ( file ) ;
101
+
102
+ Match matcher = null ;
103
+
104
+ for ( int i = 0 ; i < Constants . EPISODE_MATCHERS . Length ; i ++ )
96
105
{
97
- HasMetadata = true ,
98
- Item = new Series ( )
106
+ matcher = Constants . EPISODE_MATCHERS [ i ] . Match ( filename ) ;
107
+ if ( ! matcher . Success )
108
+ {
109
+ continue ;
110
+ }
111
+ break ;
112
+ }
113
+
114
+ if ( ! matcher . Success )
115
+ {
116
+ Logger ? . Info ( $ "No match found for { file } .") ;
117
+ return new EpisodeInfo ( ) ;
118
+ }
119
+
120
+ string series = matcher . Groups [ "series" ] . Success ? matcher . Groups [ "series" ] . Value : "" ;
121
+ string year = matcher . Groups [ "year" ] . Success ? matcher . Groups [ "year" ] . Value : "" ;
122
+ year = ( year . Length == 2 ) ? "20" + year : year ;
123
+ string month = matcher . Groups [ "month" ] . Success ? matcher . Groups [ "month" ] . Value : "" ;
124
+ string day = matcher . Groups [ "day" ] . Success ? matcher . Groups [ "day" ] . Value : "" ;
125
+ string episode = matcher . Groups [ "episode" ] . Success ? matcher . Groups [ "episode" ] . Value : "" ;
126
+
127
+ string season = matcher . Groups [ "season" ] . Success ? matcher . Groups [ "season" ] . Value : "" ;
128
+ season = season == "" ? year + month : season ;
129
+
130
+ string broadcastDate = ( year != "" && month != "" && day != "" ) ? year + "-" + month + "-" + day : "" ;
131
+ if ( broadcastDate == "" && file_date != null )
132
+ {
133
+ broadcastDate = file_date ? . ToString ( "yyyy-MM-dd" ) ?? "" ;
134
+ }
135
+
136
+ string title = matcher . Groups [ "title" ] . Success ? matcher . Groups [ "title" ] . Value : "" ;
137
+ if ( title != "" )
138
+ {
139
+ if ( ! string . IsNullOrEmpty ( series ) && title != series && title . ToLower ( ) . Contains ( series . ToLower ( ) ) )
140
+ {
141
+ title = title . Replace ( series , "" , StringComparison . OrdinalIgnoreCase ) . Trim ( ) ;
142
+ }
143
+
144
+ if ( title == "" && title == series && broadcastDate != "" )
145
+ {
146
+ title = broadcastDate ;
147
+ }
148
+
149
+ // -- replace double spaces with single space
150
+ title = Regex . Replace ( title , @"\[.+?\]" , " " ) . Trim ( '-' ) . Trim ( ) ;
151
+ title = Regex . Replace ( title , @"\s+" , " " ) ;
152
+ title = title . Trim ( ) . Trim ( '-' ) . Trim ( ) ;
153
+
154
+ if ( matcher . Groups [ "epNumber" ] . Success )
155
+ {
156
+ title = matcher . Groups [ "epNumber" ] . Value + " - " + title ;
157
+ }
158
+ else if ( title != "" && broadcastDate != "" && broadcastDate != title )
159
+ {
160
+ title = $ "{ broadcastDate . Replace ( "-" , "" ) } ~ { title } ";
161
+ }
162
+ }
163
+
164
+ if ( episode == "" )
165
+ {
166
+ episode = "1" + month + day ;
167
+
168
+ // get the modified date of the file
169
+ if ( System . IO . File . Exists ( file ) || file_date != null )
170
+ {
171
+ episode += ( file_date ?? System . IO . File . GetLastWriteTimeUtc ( file ) ) . ToString ( "mmss" ) ;
172
+ }
173
+ }
174
+
175
+ episode = ( episode == "" ) ? int . Parse ( '1' + month + day ) . ToString ( ) : episode ;
176
+
177
+ EpisodeInfo item = new ( )
178
+ {
179
+ IndexNumber = int . Parse ( episode ) ,
180
+ Name = title ,
181
+ Year = int . Parse ( year ) ,
182
+ ParentIndexNumber = int . Parse ( season )
183
+ } ;
184
+
185
+ item . SetProviderId ( Constants . PLUGIN_EXTERNAL_ID , item . IndexNumber . ToString ( ) ) ;
186
+
187
+ // -- Set the PremiereDate if we have a year, month and day
188
+ if ( year != "" && month != "" && day != "" )
189
+ {
190
+ item . PremiereDate = new DateTime ( int . Parse ( year ) , int . Parse ( month ) , int . Parse ( day ) ) ;
191
+ }
192
+
193
+ return item ;
194
+ }
195
+
196
+ public static MetadataResult < Episode > ToEpisode ( EpisodeInfo data )
197
+ {
198
+ if ( data . Name == "" )
199
+ {
200
+ Logger ? . Warn ( $ "No metadata found for '{ data } '.") ;
201
+ return ErrorOutEpisode ( ) ;
202
+ }
203
+
204
+ Logger ? . Debug ( $ "Processing { data } .") ;
205
+
206
+ Episode item = new ( )
207
+ {
208
+ Name = data . Name ,
209
+ IndexNumber = data . IndexNumber ,
210
+ ParentIndexNumber = data . ParentIndexNumber ,
99
211
} ;
212
+
213
+ if ( data . PremiereDate is DateTimeOffset time )
214
+ {
215
+ item . PremiereDate = time ;
216
+ item . ProductionYear = time . Year ;
217
+ }
218
+
219
+ item . SetProviderId ( Constants . PLUGIN_EXTERNAL_ID , data . ProviderIds [ Constants . PLUGIN_EXTERNAL_ID ] ) ;
220
+
221
+ return new MetadataResult < Episode > { HasMetadata = true , Item = item } ;
222
+ }
223
+
224
+ private static MetadataResult < Series > ErrorOut ( )
225
+ {
226
+ return new MetadataResult < Series > { HasMetadata = false , Item = new Series ( ) } ;
227
+ }
228
+
229
+ private static MetadataResult < Episode > ErrorOutEpisode ( )
230
+ {
231
+ return new MetadataResult < Episode > { HasMetadata = false , Item = new Episode ( ) } ;
100
232
}
101
233
}
102
234
}
0 commit comments