Skip to content

Commit

Permalink
- Add GoFile.io indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaBear84 committed May 22, 2022
1 parent 60b7ed7 commit b6178cf
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/OpenDirectoryDownloader/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ public class Constants
{
public const string GoogleDriveDomain = "drive.google.com";
public const string BlitzfilesTechDomain = "blitzfiles.tech";

public const string GoFileIoDomain = "gofile.io";
public const string Parameters_GdIndex_RootId = "GDINDEX_ROOTID";
public const string Parameters_GoFileIOAccountToken = "GOFILE_ACCOUNTTOKEN";

public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
public const string Parameters_Password = "PASSWORD";
public const string Parameters_GdIndex_RootId = "GdIndex_RootId";
public const string Parameters_FtpEncryptionMode = "FtpEncryptionMode";

public const long NoFileSize = 0;
public const string Root = "ROOT";
public const string Ftp_Max_Connections = "MAX_CONNECTIONS";
Expand Down
6 changes: 6 additions & 0 deletions src/OpenDirectoryDownloader/DirectoryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using OpenDirectoryDownloader.Site.GDIndex.GdIndex;
using OpenDirectoryDownloader.Site.GDIndex.Go2Index;
using OpenDirectoryDownloader.Site.GDIndex.GoIndex;
using OpenDirectoryDownloader.Site.GoFileIO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -66,6 +67,11 @@ public static async Task<WebDirectory> ParseHtml(WebDirectory webDirectory, stri
return await BlitzfilesTechParser.ParseIndex(httpClient, webDirectory);
}

if (webDirectory.Uri.Host == Constants.GoFileIoDomain)
{
return await GoFileIOParser.ParseIndex(httpClient, webDirectory);
}

if (httpClient is not null)
{
foreach (IHtmlScriptElement script in htmlDocument.Scripts.Where(s => s.Source is not null))
Expand Down
10 changes: 8 additions & 2 deletions src/OpenDirectoryDownloader/OpenDirectoryIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ public async void StartIndexingAsync()

Console.WriteLine(Statistics.GetSessionStats(Session, onlyRedditStats: true, includeExtensions: true));

if (!OpenDirectoryIndexerSettings.CommandLineOptions.NoUrls && Session.Root.Uri.Host != Constants.GoogleDriveDomain && Session.Root.Uri.Host != Constants.BlitzfilesTechDomain)
if (!OpenDirectoryIndexerSettings.CommandLineOptions.NoUrls &&
Session.Root.Uri.Host != Constants.GoogleDriveDomain &&
Session.Root.Uri.Host != Constants.BlitzfilesTechDomain &&
Session.Root.Uri.Host != Constants.GoFileIoDomain)
{
if (Session.TotalFiles > 0)
{
Expand Down Expand Up @@ -442,7 +445,10 @@ public async void StartIndexingAsync()

distinctUrls = null;

if (OpenDirectoryIndexerSettings.CommandLineOptions.Speedtest && Session.Root.Uri.Host != Constants.GoogleDriveDomain && Session.Root.Uri.Host != Constants.BlitzfilesTechDomain)
if (OpenDirectoryIndexerSettings.CommandLineOptions.Speedtest &&
Session.Root.Uri.Host != Constants.GoogleDriveDomain &&
Session.Root.Uri.Host != Constants.BlitzfilesTechDomain &&
Session.Root.Uri.Host != Constants.GoFileIoDomain)
{
if (Session.TotalFiles > 0)
{
Expand Down
134 changes: 134 additions & 0 deletions src/OpenDirectoryDownloader/Site/GoFileIO/GoFileIOListingResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var goFileListing = GoFileIOResult.FromJson(jsonString);

namespace OpenDirectoryDownloader.Site.GoFileIO
{
using System;
using System.Collections.Generic;

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public partial class GoFileIOListingResult
{
[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("data")]
public Data Data { get; set; }
}

public partial class Data
{
// Only for /createAccount
[JsonProperty("token")]
public string Token { get; set; }

[JsonProperty("isOwner")]
public bool IsOwner { get; set; }

[JsonProperty("id")]
public Guid Id { get; set; }

[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("parentFolder")]
public Guid ParentFolder { get; set; }

[JsonProperty("code")]
public string Code { get; set; }

[JsonProperty("createTime")]
public long CreateTime { get; set; }

[JsonProperty("public")]
public bool Public { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

[JsonProperty("childs")]
public Guid[] Childs { get; set; }

[JsonProperty("totalDownloadCount")]
public long TotalDownloadCount { get; set; }

[JsonProperty("totalSize")]
public long TotalSize { get; set; }

[JsonProperty("contents")]
public Dictionary<string, Content> Contents { get; set; }
}

public partial class Content
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("parentFolder")]
public Guid ParentFolder { get; set; }

[JsonProperty("createTime")]
public long CreateTime { get; set; }

[JsonProperty("size")]
public long Size { get; set; }

[JsonProperty("downloadCount")]
public long DownloadCount { get; set; }

[JsonProperty("md5")]
public string Md5 { get; set; }

[JsonProperty("mimetype")]
public string Mimetype { get; set; }

[JsonProperty("serverChoosen")]
public string ServerChoosen { get; set; }

[JsonProperty("directLink")]
public Uri DirectLink { get; set; }

[JsonProperty("link")]
public Uri Link { get; set; }
}

public partial class GoFileIOListingResult
{
public static GoFileIOListingResult FromJson(string json) => JsonConvert.DeserializeObject<GoFileIOListingResult>(json, Converter.Settings);
}

public static class Serialize
{
public static string ToJson(this GoFileIOListingResult self) => JsonConvert.SerializeObject(self, Converter.Settings);
}

internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}
153 changes: 153 additions & 0 deletions src/OpenDirectoryDownloader/Site/GoFileIO/GoFileIOParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using NLog;
using OpenDirectoryDownloader.Shared.Models;
using System;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace OpenDirectoryDownloader.Site.GoFileIO;

/// <summary>
/// Similar to GoFile.IO
/// </summary>
public static class GoFileIOParser
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static readonly Regex FolderHashRegex = new Regex(@".*?\/d\/(?<FolderHash>.*)");
private const string Parser = "GoFileIO";
private const string StatusOK = "ok";
private const string ApiBaseAddress = "https://api.gofile.io";

public static async Task<WebDirectory> ParseIndex(HttpClient httpClient, WebDirectory webDirectory)
{
try
{
string driveHash = GetDriveHash(webDirectory);

if (!OpenDirectoryIndexer.Session.Parameters.ContainsKey(Constants.Parameters_GoFileIOAccountToken))
{
Console.WriteLine($"{Parser} creating temporary account.");
Logger.Info($"{Parser} creating temporary account.");

HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"{ApiBaseAddress}/createAccount");

if (httpResponseMessage.IsSuccessStatusCode)
{
string responseJson = await httpResponseMessage.Content.ReadAsStringAsync();

GoFileIOListingResult response = GoFileIOListingResult.FromJson(responseJson);

if (response.Status != StatusOK)
{
throw new Exception($"Error creating account: {response.Status}");
}

OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_GoFileIOAccountToken] = response.Data.Token;

webDirectory = await ScanAsync(httpClient, webDirectory);
}
}
else
{
webDirectory = await ScanAsync(httpClient, webDirectory);
}
}
catch (Exception ex)
{
Logger.Error(ex, $"Error parsing {Parser} for URL: {webDirectory.Url}");
webDirectory.Error = true;

OpenDirectoryIndexer.Session.Errors++;

if (!OpenDirectoryIndexer.Session.UrlsWithErrors.Contains(webDirectory.Url))
{
OpenDirectoryIndexer.Session.UrlsWithErrors.Add(webDirectory.Url);
}

throw;
}

return webDirectory;
}

private static string GetDriveHash(WebDirectory webDirectory)
{
Match driveHashRegexMatch = FolderHashRegex.Match(webDirectory.Url);

if (!driveHashRegexMatch.Success)
{
throw new Exception("Error getting drivehash");
}

return driveHashRegexMatch.Groups["FolderHash"].Value;
}

private static async Task<WebDirectory> ScanAsync(HttpClient httpClient, WebDirectory webDirectory)
{
Logger.Debug($"Retrieving listings for {webDirectory.Uri}");

webDirectory.Parser = Parser;

try
{
string driveHash = GetDriveHash(webDirectory);

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

HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(GetApiListingUrl(driveHash, OpenDirectoryIndexer.Session.Parameters[Constants.Parameters_GoFileIOAccountToken]));

webDirectory.ParsedSuccessfully = httpResponseMessage.IsSuccessStatusCode;
httpResponseMessage.EnsureSuccessStatusCode();

string responseJson = await httpResponseMessage.Content.ReadAsStringAsync();

GoFileIOListingResult indexResponse = GoFileIOListingResult.FromJson(responseJson);

if (indexResponse.Status != StatusOK)
{
throw new Exception($"Error retrieving listing for {webDirectory.Uri}. Error: {indexResponse.Status}");
}

foreach (Content entry in indexResponse.Data.Contents.Values)
{
if (entry.Type == "folder")
{
webDirectory.Subdirectories.Add(new WebDirectory(webDirectory)
{
Parser = Parser,
Url = GetFolderUrl(entry.Id),
Name = entry.Name
});
}
else
{
webDirectory.Files.Add(new WebFile
{
Url = entry.DirectLink.ToString(),
FileName = entry.Name,
FileSize = entry.Size
});
}
}
}
catch (Exception ex)
{
Logger.Error(ex, $"Error processing {Parser} for URL: {webDirectory.Url}");
webDirectory.Error = true;

OpenDirectoryIndexer.Session.Errors++;

if (!OpenDirectoryIndexer.Session.UrlsWithErrors.Contains(webDirectory.Url))
{
OpenDirectoryIndexer.Session.UrlsWithErrors.Add(webDirectory.Url);
}

//throw;
}

return webDirectory;
}

private static string GetFolderUrl(string driveHash) => $"https://gofile.io/d/{driveHash}";
private static string GetApiListingUrl(string driveHash, string accountToken) => $"{ApiBaseAddress}/getContent?contentId={Uri.EscapeDataString(driveHash)}&token={Uri.EscapeDataString(accountToken)}&websiteToken=12345";
}

0 comments on commit b6178cf

Please sign in to comment.