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="******"&password="******"\"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 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: "******"Password: "******"\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); } }