Skip to content

Commit

Permalink
- Add Mediafire paging support
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaBear84 committed Aug 8, 2022
1 parent 90dc2aa commit b467653
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/OpenDirectoryDownloader/Site/Mediafire/MediafireParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,35 @@ private static async Task<WebDirectory> ScanAsync(HttpClient httpClient, WebDire
{
string folderId = GetFolderId(webDirectory);

Logger.Warn($"Retrieving listings for {webDirectory.Uri}");

foreach (string listingType in new string[2] { "folders", "files" })
{
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(GetApiListingUrl(folderId, listingType));
bool moreChunks = false;
int chunkNumber = 1;

webDirectory.ParsedSuccessfully = httpResponseMessage.IsSuccessStatusCode;
httpResponseMessage.EnsureSuccessStatusCode();
do
{
Logger.Warn($"Retrieving {listingType} listing for {webDirectory.Uri}, page {chunkNumber}");

string responseJson = await httpResponseMessage.Content.ReadAsStringAsync();
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(GetApiListingUrl(folderId, listingType, chunkNumber));

MediafireResult indexResponse = MediafireResult.FromJson(responseJson);
webDirectory.ParsedSuccessfully = httpResponseMessage.IsSuccessStatusCode;
httpResponseMessage.EnsureSuccessStatusCode();

if (indexResponse.Response.Result != StatusSuccess)
{
throw new Exception($"Error retrieving listing for {webDirectory.Uri}. Error: {indexResponse.Response.Result}");
}
string responseJson = await httpResponseMessage.Content.ReadAsStringAsync();

MediafireResult indexResponse = MediafireResult.FromJson(responseJson);

// Nice boolean value Mediafire..
moreChunks = indexResponse.Response.FolderContent.MoreChunks == "yes";

if (indexResponse.Response.Result != StatusSuccess)
{
throw new Exception($"Error retrieving {listingType} listing for {webDirectory.Uri}, page {chunkNumber}. Error: {indexResponse.Response.Result}");
}

ProcessListing(webDirectory, indexResponse);
ProcessListing(webDirectory, indexResponse);
chunkNumber++;
} while (moreChunks);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -138,5 +148,5 @@ private static void ProcessListing(WebDirectory webDirectory, MediafireResult in
}

private static string GetFolderUrl(string folderId) => $"https://www.mediafire.com/folder/{folderId}";
private static string GetApiListingUrl(string folderId, string type, int chunk = 1) => $"{ApiBaseAddress}/folder/get_content.php?content_type={type}&filter=all&order_by=name&order_direction=asc&chunk={chunk}&version=1.5&folder_key={folderId}&response_format=json";
private static string GetApiListingUrl(string folderId, string type, int chunkNumber = 1) => $"{ApiBaseAddress}/folder/get_content.php?content_type={type}&filter=all&order_by=name&order_direction=asc&chunk={chunkNumber}&version=1.5&folder_key={folderId}&response_format=json";
}

0 comments on commit b467653

Please sign in to comment.