Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the given header was not found #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class Program
static string SteamId;
static string DownloadFolder => $"Screenshots{SteamId}";

static Dictionary<string, string> MimeToExtension = new Dictionary<string, string>()
{
{ "image/jpeg", ".jpg" },
{ "image/png", ".png" },
{ "image/gif", ".gif" },
{ "image/webp", ".webp" },
};

static async Task Main( string[] args )
{
Console.WriteLine( "Hello, want to download some screenshots yeah?" );
Expand Down Expand Up @@ -81,7 +89,7 @@ static async Task ScanPages()
Console.WriteLine( $"Retrying incase it was a server error.." );

await Task.Delay( fails * 1000 );
}
}

await Task.Delay( 100 );
page++;
Expand Down Expand Up @@ -166,13 +174,11 @@ private static async Task<bool> DownloadImage( ulong file )

var download = await client.GetAsync( imageUrl );

var cd = download.Content.Headers.GetValues( "Content-Disposition" ).First();

var filename = Regex.Match( cd, "filename\\*?=(?:UTF-8'')?\"?(.+)" ).Groups[1].Value.Trim( ';', ' ', '"' );

var fileId = GetStringBetween(imageUrl, "ugc/", "/");
var extension = GetFileExtension(download.Content.Headers.GetValues("Content-Type").First());
var data = await download.Content.ReadAsByteArrayAsync();

System.IO.File.WriteAllBytes( $"{DownloadFolder}/{filename}", data );
System.IO.File.WriteAllBytes( $"{DownloadFolder}/{fileId}{extension}", data );

return true;
}
Expand All @@ -190,5 +196,22 @@ private static async Task<bool> DownloadImage( ulong file )
return false;
}
}

private static string GetStringBetween(string s, string startWord, string endWord)
{
int Pos1 = s.IndexOf(startWord) + startWord.Length;
int Pos2 = s.IndexOf(endWord, Pos1);
return s.Substring(Pos1, Pos2 - Pos1);
}

private static string GetFileExtension(string mimeType)
{
MimeToExtension.TryGetValue(mimeType, out string extension);
if (extension == null)
{
throw new ArgumentException($"Mimetype: {mimeType} not found in the dictionary!");
}
return extension;
}
}
}