Skip to content

Commit

Permalink
Merge pull request #30 from iPromKnight/hotfix/torrentparsing
Browse files Browse the repository at this point in the history
fix: torrent parsing v2
  • Loading branch information
iPromKnight authored Nov 11, 2024
2 parents 860c069 + 067f7f1 commit c9195cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RUN dotnet publish -c Release --no-restore -a $TARGETARCH -o /app/out
# Run Stage
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine3.19
RUN apk add --update --no-cache \
python3=3.11.9-r1 \
py3-pip=23.3.1-r0 \
curl=8.9.1-r0 \
python3=~3.11 \
py3-pip=~23.3 \
curl=~8.9 \
&& ln -sf python3 /usr/bin/python
ENV DOTNET_RUNNING_IN_CONTAINER=true
ENV DOTNET_gcServer=1
Expand Down
29 changes: 21 additions & 8 deletions src/Zilean.DmmScraper/Features/Dmm/DmmPageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<List<ExtractedDmmEntry>> ProcessPageAsync(string filePath, str
using var memoryStream = new MemoryStream(byteArray, 0, byteCount);
using var json = await JsonDocument.ParseAsync(memoryStream, cancellationToken: cancellationToken);

var torrents = json.RootElement.TryGetProperty("torrents", out var torrentsElement) && torrentsElement.ValueKind == JsonValueKind.Array
var torrents = json.RootElement.ValueKind == JsonValueKind.Object && json.RootElement.TryGetProperty("torrents", out var torrentsElement)
? torrentsElement.EnumerateArray().Select(ParsePageContent).OfType<ExtractedDmmEntry>().ToList()
: json.RootElement.EnumerateArray().Select(ParsePageContent).OfType<ExtractedDmmEntry>().ToList();

Expand All @@ -44,7 +44,7 @@ public async Task<List<ExtractedDmmEntry>> ProcessPageAsync(string filePath, str
}

var sanitizedTorrents = torrents
.Where(x=>x.InfoHash.Length == 40 && x.Filesize > 0)
.Where(x=> x.Filesize > 0)
.GroupBy(x => x.InfoHash)
.Select(group => group.FirstOrDefault())
.Where(x => !string.IsNullOrEmpty(x.Filename))
Expand All @@ -65,12 +65,25 @@ public async Task<List<ExtractedDmmEntry>> ProcessPageAsync(string filePath, str
}
}

private static ExtractedDmmEntry? ParsePageContent(JsonElement item) =>
item.TryGetProperty("filename", out var filenameElement) &&
item.TryGetProperty("bytes", out var filesizeElement) &&
item.TryGetProperty("hash", out var hashElement)
? new ExtractedDmmEntry(hashElement.GetString(), filenameElement.GetString().Replace(".", " ", StringComparison.Ordinal), filesizeElement.GetInt64(), null)
: null;
private static ExtractedDmmEntry? ParsePageContent(JsonElement item)
{
var filename = item.TryGetProperty("filename", out var filenameElement);
var filesize = item.TryGetProperty("bytes", out var filesizeElement);
var hash = item.TryGetProperty("hash", out var hashElement);

if (!filename || !filesize || !hash)
{
return null;
}

var fileText = filenameElement.GetString();
var fileSize = filesizeElement.GetInt64();
var hashText = hashElement.GetString();

var entry = new ExtractedDmmEntry(hashText, fileText.Replace(".", " ", StringComparison.Ordinal), fileSize, null);

return entry;
}

public void Dispose()
{
Expand Down

0 comments on commit c9195cc

Please sign in to comment.