namespace SharedContracts.Python.RTN; public class RankTorrentName : IRankTorrentName { private readonly IPythonEngineService _pythonEngineService; private const string RtnModuleName = "RTN"; private dynamic? _rtn; public RankTorrentName(IPythonEngineService pythonEngineService) { _pythonEngineService = pythonEngineService; InitModules(); } public ParseTorrentTitleResponse Parse(string title, bool trashGarbage = true) => _pythonEngineService.ExecutePythonOperationWithDefault( () => { var result = _rtn?.parse(title, trashGarbage); return ParseResult(result); }, new ParseTorrentTitleResponse(false, null), nameof(Parse), throwOnErrors: false, logErrors: false); private static ParseTorrentTitleResponse ParseResult(dynamic result) { if (result == null) { return new(false, null); } var json = result.model_dump_json()?.As(); if (json is null || string.IsNullOrEmpty(json)) { return new(false, null); } var mediaType = result.GetAttr("type")?.As(); if (string.IsNullOrEmpty(mediaType)) { return new(false, null); } var response = JsonSerializer.Deserialize(json); response.IsMovie = mediaType.Equals("movie", StringComparison.OrdinalIgnoreCase); return new(true, response); } private void InitModules() => _rtn = _pythonEngineService.ExecutePythonOperation(() => { _pythonEngineService.Sys.path.append(Path.Combine(AppContext.BaseDirectory, "python")); return Py.Import(RtnModuleName); }, nameof(InitModules), throwOnErrors: false); }