// Token check with direct parsing public static int TokenCheckDirectVoid( int i, string[] tokens, RequestParams parameters, int bdayDay, VkUsers users) { // First tokens selecting if (i < tokens.Length) { parameters.Token = tokens[i]; Console.WriteLine("\nToken are blocked. Selected next {0} token\n", i); Console.WriteLine("Lets try again parsing {0} day\n", bdayDay); return(1); } // If all tokens in list are lock - check token list again else { i = 0; Console.WriteLine("\n.Start again from 0 token\n"); try { while (users.response.Count == 0 && i < tokens.Length) { Console.WriteLine("\n.Try token: {0} \n", i); parameters.Token = tokens[i]; var response = VkRequest.VkSearchResponse(parameters); users = VkUsers.VkDeserealiseResponse(response); i++; // No more working tokens -> exit if (i == tokens.Length) { Console.WriteLine("\nTokens are ended. Parsing is finished!\n"); return(2); } } } catch (Exception ex) { Console.WriteLine("\nWhile cycle exception:\n{0}", ex); return(3); } Console.WriteLine("\nTokens are ended. Try start from {0} token again\n", i); Console.WriteLine("Lets try again parsing {0} day\n", bdayDay); return(4); } }
// Get users from Vk (isWriteInFile == false), Get users and write id into file (isWriteInFile == true) public static VkUsers GetWriteUsers(RequestParams parameters, int delayFrom, int delayTo, string outputFile, bool isWriteInFile) { var randomGen = new Random(); Thread.Sleep(randomGen.Next(delayFrom, delayTo)); var response = VkRequest.VkSearchResponse(parameters); var users = VkUsers.VkDeserealiseResponse(response); if (isWriteInFile) { WriteInFile(users, outputFile, parameters.City); } return(users); }
// Write users id in file (streaming or union) public static void WriteInFile(VkUsers users, string outputFile, int city) { using (var stream = new StreamWriter(outputFile + "_" + city + ".txt", true)) { foreach (var user in users.response) { /* * var oldUsers = File.ReadAllLines(OutputFile + ".txt"); * string[] newUsers = { user.uid.ToString() }; * var unionUsers = oldUsers.Union(newUsers).ToArray(); * File.WriteAllLines(OutputFile + ".txt", unionUsers); */ stream.WriteLine(user.uid.ToString()); } } }
// Token checking for Name parsing :: Find one Ivan in Moscow public static bool TokenCheck(RequestParams parameters, int delayFrom) { Thread.Sleep(delayFrom); VkUsers.RequestParamsInit(parameters); parameters.Name = "Иван"; parameters.City = 1; parameters.Count = 1; var response = VkRequest.VkSearchResponse(parameters); var users = VkUsers.VkDeserealiseResponse(response); if (users == null || users.response.Count == 0) { return(false); } return(users.response.Count > 0); }
public static VkUsers VkDeserealiseResponse(string str) { try { var jr = new JsonTextReader(new StringReader(str)); var jObject = JObject.Load(jr); var m = new VkUsers { response = jObject["response"].Children() .Where(c => c.HasValues) .Select(c => c.ToObject <UsersResponse>()) .ToList() }; return(m); } catch (Exception ex) { Console.WriteLine("VkDeserealiseResponce Exception : {0}", ex); return(null); } }
// Token checking for Random parsing :: Check nearest day public static bool TokenCheckRandom(RequestParams parameters, int delayFrom) { Thread.Sleep(delayFrom); // Check for start or end of month: if (parameters.BirthDay < Month.DaysInMonth[parameters.BirthMonth]) { ++parameters.BirthDay; } else { --parameters.BirthDay; } var response = VkRequest.VkSearchResponse(parameters); var users = VkUsers.VkDeserealiseResponse(response); if (users == null || users.response.Count == 0) { return(true); } return(users.response.Count == 0); }
// Parsing functions :: Only by Birthday :: Direct token list parsing public static void ParsingUsersByBdayDirect( out int usersFound, out string parsingStatus, RequestParams parameters, int birthYearFrom, int birthYearTo, int delayFrom, int delayTo, bool isAllStatusParsing, // If it true - parse all types of users status bool isDownParse, string outputFile) // If it true - parse from higher year to lower { usersFound = 0; var i = 0; var tokens = File.ReadAllLines(TokensFile); parameters.Token = File.ReadAllLines(TokensFile)[0]; if (isDownParse) { #region Parsing cycle from down to up (ex. from 2005 to 1950) for (var bdayYear = birthYearTo; bdayYear >= birthYearFrom; bdayYear--) { Console.WriteLine("\nCurrent year is : {0}\n", bdayYear); for (var bdayMonth = 1; bdayMonth < 13; bdayMonth++) { Console.WriteLine("\nCurrent month is : {0}\n", bdayMonth); for (var bdayDay = 1; bdayDay < Month.DaysInMonth[bdayMonth] + 1; bdayDay++) { parameters.BirthDay = bdayDay; parameters.BirthMonth = bdayMonth; parameters.BirthYear = bdayYear; var users = new VkUsers(); // Parsing all users from current date by each status type: if (isAllStatusParsing) { for (var statusType = 1; statusType < 8; statusType++) { parameters.Status = statusType; users = Utils.GetWriteUsers(parameters, delayFrom, delayTo, outputFile, true); usersFound += users.response.Count; Console.WriteLine("Users with status {0} founded : {1}", statusType, users.response.Count); } } // Parsing all users from current date else { users = Utils.GetWriteUsers(parameters, delayFrom, delayTo, outputFile, true); usersFound += users.response.Count; Console.WriteLine("Users with B-Day {0}.{1}.{2} founded : {3}", bdayDay, bdayMonth, bdayYear, users.response.Count); } # region Token checking: if (users.response.Count == 0) { i++; var check = Utils.TokenCheckDirectVoid(i, tokens, parameters, bdayDay, users); if (check == 1) { bdayDay--; } if (check == 2 || check == 3) { parsingStatus = "Parsing not full completed!"; return; } if (check == 4) { bdayDay--; } } # endregion } } Console.WriteLine("\nAfter {0} year, finded {1} users : {0}\n", bdayYear, usersFound); } #endregion }