-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
194 lines (148 loc) · 6.23 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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;
}
}
}
}