code details:
1. Read file.
2. create threads to processs each line.
3. store results after procssing. Here it will get some information from youtube and then
also use genderize api to check user demographic details.
5. Reading of lines causes creation of new thread. and once all N number of thread created
it wait for all to ,complete and store data.
6. Then it will start creating new set of threads of next read operation.
7. Is is good mix of async and sync program to parallal programming.
namespace Recon
{
class Program
{
static HttpClient client = new HttpClient();
static StreamWriter writer = new StreamWriter("c:\\temp\\result\\artFluck.txt");
static void Main(string[] args)
{
int NUMBER_OF_THREADS = 30;
string[] delimit = { "group" };
foreach (string fileName in Directory.GetFiles("c:\\temp\\", "*.*"))
{
string[] fileLines = File.ReadAllLines(fileName);
int ThreadNumber = 1;
List<Task> tasksToWait = new List<Task>();
foreach (string line in fileLines)
{
var name = line.Replace("https://www.youtube.com/", "");
if (delimit.Any(s => name.Contains(s))) continue;
var extractName = name.Split('.');
if (extractName.Length > 0)
{
if (ThreadNumber <= NUMBER_OF_THREADS)
{
ThreadNumber++;
Task task = new Task(() =>
{
CreateTask(extractName[0], line);
});
task.Start();
tasksToWait.Add(task);
}
else
{
Task.WaitAll(tasksToWait.ToArray());
tasksToWait.Clear();
ThreadNumber = 1;
}
}
}
}
}
private static void CreateTask(string name, string line)
{
var response = GetData(name);
if (response.Result == true)
{
//Multiple threads wirte to same file. so need thread safe design.
try
{
writer.WriteLineAsync(line);
}
catch (Exception ex)
{
Console.Write(line);
}
}
}
private static async Task<bool> GetData(string name)
{
HttpResponseMessage response =
await client.GetAsync("https://api.genderize.io/?name=" + name
var content = response.Content.ReadAsStringAsync();
var value = content.Result.ToString();
if (value.Contains("female")) return true;
else return false;
}
}
}
No comments:
Post a Comment