forked from arcanecfg/Instagram-Private-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
259 lines (224 loc) · 10.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Title: Instagram Private Scraper
* Author: Arcanecfg
www.WastedWolf.com
www.YouTube.com/Arcanecfg
* Description: Scrape public and private photos from Instagram profiles
without using the official API.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace InstagramPrivateScraper
{
class Program
{
static string targetUser;
static void Main(string[] args)
{
DisplayLoadInfo();
string repeatAnswer = "n";
do
{
Console.ForegroundColor = ConsoleColor.White;
InstagramAccount.cookies = new CookieContainer();
Console.Write("Target Username: ");
targetUser = Console.ReadLine();
//Start FetchPosts() as a separate task
Task fetchTask = new Task(FetchPosts);
fetchTask.Start();
//Wait until FetchPosts() is complete
Console.WriteLine("Fetching profile info...\n");
fetchTask.Wait();
Console.WriteLine("Done.");
Console.Write("\nPerform another scrape? (y/n): ");
repeatAnswer = Console.ReadLine();
} while (repeatAnswer == "y");
}
private static void FetchPosts()
{
List<string> postList = new List<string>();
//The ?__a=1 parameter returns just the JSON without any HTML
string pageUrl = "https://www.instagram.com/" + targetUser + "/?__a=1";
//Flag separates the first iteration from the rest
bool flag = false;
//WebClient used for downloading the webpage initially
WebClient wc = new WebClient();
try
{
string rawSrc = wc.DownloadString("https://www.instagram.com/" + targetUser + "/?__a=1");
Console.WriteLine("Name: {0}", ParseText(rawSrc, "\"full_name\": \"", "\", \"has_blocked_viewer"));
string mediaCount;
//Private account
if (rawSrc.Contains("\"media\": {\"nodes\": [], \"count\": "))
{
mediaCount = ParseText(rawSrc, "\"media\": {\"nodes\": [], \"count\": ", ", \"page_info\"");
}
//Public account
else
{
mediaCount = ParseText(rawSrc, "}}], \"count\": ", ", \"page_info");
}
Console.WriteLine("Photos: {0}", mediaCount);
Console.WriteLine("Biography: {0}", ParseText(rawSrc, "\"biography\": \"", "\", \"blocked_by_viewer"));
if (rawSrc.Contains("\"is_private\": true"))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nAccount is private; please login to your account.");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Username: ");
InstagramAccount.username = Console.ReadLine();
Console.Write("Password: ");
InstagramAccount.password = Console.ReadLine();
//Task to login to your account
Task loginTask = new Task(LogIn);
loginTask.Start();
Console.WriteLine("\nLogging in...");
loginTask.Wait();
}
//queryId is a constant stored in the ConsumerCommons.js file — no longer required
//Console.WriteLine("Fetching query ID...");
//string rawJs = new WebClient().DownloadString("https://www.instagram.com/static/bundles/ConsumerCommons.js/db149d8f0b6c.js");
//string queryId = ParseText(rawJs, "queryId:\"", "\",");
//profileId was required for the first endpoint — no longer required.
//string profileId = ParseText2(rawSrc, "\"owner\": {\"id\": \"", "\"},");
//Console.WriteLine("Query ID: " + queryId);
Console.WriteLine("\nFetching posts...");
do
{
rawSrc = HTTPMethods.GET(pageUrl, InstagramAccount.cookies, "https://www.instagram.com/" + targetUser + "/");
string json;
//First iteration
if (rawSrc.Contains("\"media\": {\"nodes\": "))
{
//Split the raw string into a valid JSON format
json = ParseText(rawSrc, "\"media\": {\"nodes\": ", "}}], \"count\"") + "}}]";
//flag=false indicates first iteration
flag = false;
}
else
{
//JSON keys are different from first iteration
json = ParseText(rawSrc, "\"edges\": ", "}]}}}") + "}]";
//flag=true indicates all other iterations
flag = true;
}
string end_cursor = string.Empty;
//If there are more pages with posts
if (rawSrc.Contains("\"has_next_page\": true"))
{
end_cursor = ParseText(rawSrc, "{\"has_next_page\": true, \"end_cursor\": \"", "\"}");
}
//Deserialize JSON
dynamic dyn = JsonConvert.DeserializeObject(json);
foreach (var post in dyn)
{
if (flag == false)
//First iteration uses display_src key
postList.Add((string)post.display_src + "$" + (string)post.code);
else
//Subsequent iterations use display_url
postList.Add((string)post.display_url + "$" + (string)post.code);
}
//Pagination for multiple pages
pageUrl = "https://www.instagram.com/" + targetUser + "/?__a=1&max_id=" + end_cursor;
//The original endpoint I was using — complicated and unnecessary
//pageUrl = "https://www.instagram.com/graphql/query/?query_id="+ queryId +"&variables={\"id\":\""+ profileId + "\",\"first\":12,\"after\":\"" + end_cursor + "\"}" ;
}
while (rawSrc.Contains("\"has_next_page\": true"));
Console.WriteLine("Finished fetching {0} posts.", postList.Count);
//Create new directory for user
if (!Directory.Exists(targetUser))
Directory.CreateDirectory(targetUser);
Console.WriteLine("Created new directory {0}", targetUser);
DownloadMedia(postList);
postList.Clear();
}
catch (Exception ex)
{
Console.WriteLine("\nAn error occured: {0}", ex.Message);
}
}
private static void LogIn()
{
try
{
//Fetch csrftoken using a GET request and store the cookies
var csrfToken = ParseText(HTTPMethods.GET(Constants.GetUrl, InstagramAccount.cookies, Constants.Referer), "csrf_token\": \"", "\", \"viewer");
//Use the fetched csrftoken and saved cookies for logging in
if (HTTPMethods.POST("username=" + InstagramAccount.username + "&password=" + InstagramAccount.password, InstagramAccount.cookies, csrfToken, "\"authenticated\": true", Constants.Referer))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Successfully logged in");
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error logging in.");
}
}
catch (Exception ex)
{
Console.WriteLine("An error occured while logging in: {0}", ex.Message);
}
Console.ForegroundColor = ConsoleColor.White;
}
private static void DownloadMedia(List<string>postList)
{
int downloadCounter = 0;
WebClient picDownloader;
Console.WriteLine("Starting download...");
Console.ForegroundColor = ConsoleColor.DarkGreen;
//Repeat for all posts in postList
Parallel.ForEach(postList, post =>
{
//postInfo contains post's shortcode and URL
string[] postInfo = post.Split('$');
picDownloader = new WebClient();
picDownloader.DownloadFile(postInfo[0], targetUser + @"\" + postInfo[1] + ".jpg");
downloadCounter++;
Console.WriteLine("[{1}/{2}] Downloaded {0}.jpg", postInfo[1], downloadCounter, postList.Count);
});
Console.ForegroundColor = ConsoleColor.White;
}
public static string ParseText(string value, string a, string b)
{
try
{
//Index of first character of first string
int posA = value.IndexOf(a);
//Only check for beginning of second string starting from the index of first string
int posB = value.Substring(posA).IndexOf(b) + posA; //Add posA to get the appropriate index
//Skip the indices containing the rest of the first string
posA += a.Length;
return value.Substring(posA, posB - posA);
}
catch { return "?"; }
}
static void CenterPrintString(string msg)
{
Console.SetCursorPosition((Console.WindowWidth - msg.Length) / 2, Console.CursorTop);
Console.WriteLine(msg);
}
static void DisplayLoadInfo()
{
Console.Title = Constants.title;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine(Environment.NewLine);
CenterPrintString(Constants.title);
Console.ForegroundColor = ConsoleColor.White;
CenterPrintString(Constants.author);
CenterPrintString("Release: 09/12/2017");
CenterPrintString(Constants.separator);
Console.WriteLine();
Console.ResetColor();
}
}
}