Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
garrynewman committed Mar 18, 2020
1 parent bee57e7 commit d8fe8d0
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 4 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
config/site.php
files/cache/*
files/tmp/*
.htaccess
.vs
bin
obj
*.user
publish
194 changes: 194 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SteamDownloader
{
class Program
{
static List<ulong> Files = new List<ulong>();
static string SteamId;
static string DownloadFolder => $"Screenshots{SteamId}";

static async Task Main( string[] args )
{
Console.WriteLine( "Hello, want to download some screenshots yeah?" );
Console.WriteLine();
Console.WriteLine( "Your SteamId is a long number, usually starting with 7.. ie 76561197960279927" );
Console.WriteLine( "You can paste in this window by right clicking" );

AskForSteamId:

Console.WriteLine();
Console.WriteLine( "Please type your SteamId:" );

SteamId = Console.ReadLine().Trim();

if ( !long.TryParse( SteamId, out var _ ) )
{
Console.Write( "That doesn't look right." );
goto AskForSteamId;
}

// Make sure the folder exists
if ( !System.IO.Directory.Exists( DownloadFolder ) )
System.IO.Directory.CreateDirectory( DownloadFolder );

// Scan all the pages for files
await ScanPages();

// Remove any duplicates
Files = Files.Distinct().ToList();

if ( Files.Count == 0 )
{
Console.WriteLine();
Console.WriteLine( "No screenshots found. Is the profile set to private?" );
return;
}

Console.WriteLine( $"Found {Files.Count()} Screenshots" );

// Download them all
await DownloadImages();

Console.WriteLine();
Console.WriteLine( $"All done! You can see the screenshots in \"/{DownloadFolder}/\"" );
}

static async Task ScanPages()
{
int page = 1;
while ( true )
{
Console.WriteLine( $"Getting Page {page} ({Files.Count} screenshots found)" );

int fails = 0;
while ( !await GetPage( page, SteamId ) )
{
fails++;
Console.WriteLine( $"Page {page} didn't have any screenshots" );

if ( fails > 3 )
{
Console.WriteLine( $"I think we're at the end. Lets start grabbing the screenshots." );
return;
}

Console.WriteLine( $"Retrying incase it was a server error.." );

await Task.Delay( fails * 1000 );
}

await Task.Delay( 100 );
page++;
}
}

private static async Task<bool> GetPage( int pageid, string targetAccount )
{
try
{
using ( var client = new HttpClient() )
{
var response = await client.GetStringAsync( $"https://steamcommunity.com/profiles/{targetAccount}/screenshots?p={pageid}&browsefilter=myfiles&view=grid&privacy=30" );

var matches = Regex.Matches( response, "steamcommunity\\.com/sharedfiles/filedetails/\\?id\\=([0-9]+?)\"", RegexOptions.Singleline | RegexOptions.IgnoreCase );

if ( matches.Count == 0 )
return false;

foreach ( Match match in matches )
{
Files.Add( ulong.Parse( match.Groups[1].Value ) );
}
}

return true;
}
catch ( System.Exception e )
{
Console.Error.WriteLine( e.Message );
return false;
}
}

static async Task DownloadImages()
{
var tasks = new List<Task>();

foreach ( var file in Files )
{
var t = DownloadImage( file );

tasks.Add( t );

while ( tasks.Count > 16 )
{
await Task.WhenAny( tasks );
tasks.RemoveAll( x => x.IsCompleted );
}
}

await Task.WhenAll( tasks );
}

private static async Task<bool> DownloadImage( ulong file )
{
int tries = 0;

Retry:

tries++;

try
{
using ( var client = new HttpClient() )
{
client.DefaultRequestHeaders.Add( "user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36" );

var response = await client.GetStringAsync( $"https://steamcommunity.com/sharedfiles/filedetails/?id={file}" );

var matches = Regex.Matches( response, "\\<a href\\=\"(https\\://steamuserimages-a.akamaihd.net/ugc/([A-Z0-9/].+?))\"", RegexOptions.Singleline | RegexOptions.IgnoreCase );

if ( matches.Count == 0 )
{
Console.WriteLine( $"[{file}] - couldn't find image link" );
return false;
}

var imageUrl = matches.First().Groups[1].Value;

Console.WriteLine( $"[{file}] - downloading {imageUrl}" );

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 data = await download.Content.ReadAsByteArrayAsync();

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

return true;
}
}
catch ( System.Exception e )
{
Console.Error.WriteLine( e.Message );

if ( tries < 3 )
{
await Task.Delay( 3000 * tries );
goto Retry;
}

return false;
}
}
}
}
17 changes: 17 additions & 0 deletions Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishDir>publish\</PublishDir>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishReadyToRun>True</PublishReadyToRun>
</PropertyGroup>
</Project>
11 changes: 11 additions & 0 deletions SteamScreenshotDownloader.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions SteamScreenshotDownloader.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteamScreenshotDownloader", "SteamScreenshotDownloader.csproj", "{650E8F81-5678-4CE3-831F-B5E4A89418E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{650E8F81-5678-4CE3-831F-B5E4A89418E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{650E8F81-5678-4CE3-831F-B5E4A89418E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{650E8F81-5678-4CE3-831F-B5E4A89418E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{650E8F81-5678-4CE3-831F-B5E4A89418E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6D77E2C3-3B47-4B11-A043-7FEE8473D3C1}
EndGlobalSection
EndGlobal

1 comment on commit d8fe8d0

@Buggem
Copy link

@Buggem Buggem commented on d8fe8d0 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This program is very helpful but an update to it to download Videos would be nice. (the new Steam client update introduced this)

Please sign in to comment.