public async Task <ScrapeDto> CreateScrapeAsync(CancellationToken cancellationToken = default) { var scrape = new Scrape { StartDate = _clock.UtcNow }; _context.Add(scrape); await _context.SaveChangesAsync(cancellationToken); return(_mapper.Map <ScrapeDto>(scrape)); }
public async Task DownloadMovieTorrentAsync(int movieId, int torrentId, CancellationToken cancellationToken = default) { var options = _options.Value; var movie = await _movieService.GetAsync(movieId, cancellationToken); var torrent = movie.MovieSources .SelectMany(x => x.Torrents) .FirstOrDefault(x => x.Id == torrentId); if (torrent is null) { throw BadRequestException.Create($"torrent with id {torrentId} does not exist on movie with id {movieId}"); } if (!(movie.LocalMovie is null)) { throw BadRequestException.Create($"movie with id {movieId} is already downloaded"); } if (movie.TransmissionStatus.HasValue) { throw BadRequestException.Create($"movie with id {movieId} is currently downloading"); } var torrentName = $"{movie.Title} ({movie.Year})"; var client = new Client(options.RpcUri.ToString()); var torrents = await client.TorrentGetAsync(TorrentFields.ALL_FIELDS); if (torrents.Torrents.Any(x => x.Name == torrentName)) { throw BadRequestException.Create($"movie with id {movieId} is currently downloading"); } // get magnet uri var trs = string.Join('&', options.Trackers.Select(x => $"tr={x}")); var magnetUri = $"magnet:?xt=urn:btih:{torrent.Hash}&dn={Uri.EscapeDataString(torrentName)}&{trs}"; var result = await client.TorrentAddAsync(new NewTorrent { Filename = magnetUri }); _logger.LogInformation("added torrent {name}", result.Name); var context = new TransmissionContext { ExternalId = result.ID, Name = result.Name, MagnetUri = magnetUri, MovieId = movie.Id, TorrentId = torrent.Id, Statuses = new List <TransmissionContextStatus> { GetStatus(TransmissionStatusCode.Started) } }; _context.Add(context); await _context.SaveChangesAsync(cancellationToken); _logger.LogInformation("saved context for torrent {name}", result.Name); }