示例#1
0
        public void ScanFacebookExecuted(object param)
        {
            // Okay I apologize in advance for this shit.... Read on if you dare...
            //
            // Purpose of this block of code:
            // To scrape facebook as fast as possible, asynchronously.
            //
            // How it's achieved:
            // Parallel foreach loops iterating over urls and async web requests
            //
            // Why this is garbage:
            // Parallel foreach is used to iterate over the urls as fast as possible. However, I'm using
            // async web requests inside of the parallel loops. Unfortunately this breaks C# apparently.
            // When you await an async request inside of a parallel foreach, the parallel loop is no longer
            // blocking and exits immediately. This means that that part at the bottom, the part that updates the
            // UI, will run prior to the results being returned.... Yeah... fml
            //
            // How I worked around this:
            // I create a List<Object>. When a request goes out we add one to the list. When a response comes back
            // we remove one from the list. At the base of the function we have an infinite non-blocking loop that
            // waits for the requests count to equal zero. When it's zero it sorts our list based on confidence score
            // and updates the UI.
            //
            // Problems with this method:
            // Currently I'm not accounting for failed requests.... I have no idea what would happen. Probably nothing good.
            // Like seriously... I don't expect the application to suddenly get better if a request fails... Fix this...

            List <Object> requests = new List <Object>();

            List <UsernameLinkModel> tempLinks = new List <UsernameLinkModel>();

            try
            {
                IEnumerable <DBLinkModel> tmpCache = DatabaseSearchResults.Where(pm => pm.IsSelected);
                foreach (var item in tmpCache)
                {
                    var temp = TargetLinks.Where(tl => tl.id == item.Id && tl.usernames.Count > 0);
                    if (temp.Count() > 0)
                    {
                        tempLinks.Add(temp.FirstOrDefault());
                        tmpCache = tmpCache.Where(tl => tl.Id != item.Id);
                    }
                }

                TargetLinks.Clear();
                TopTargets.Clear();

                object outerLock = new object();
                object innerLock = new object();

                // First we run over our list of voter db targets
                Parallel.ForEach(tmpCache, async pm =>
                {
                    DBLinkModel target = pm;

                    Console.WriteLine("Running search on target: " + target.firstname + " " + target.lastname);

                    lock (outerLock)
                    {
                        requests.Add(new Object());
                    }

                    // foreach voter db target, try and see if there are any facebook users in ohio with that name
                    string source = await ExternalBrowser.CallExternalBrowser(FacebookStuff.FormatQueryURL(target), true);

                    // lock adding and removing requests, because you know, threads, shared resources, fml
                    lock (outerLock)
                    {
                        requests.RemoveAt(requests.Count - 1);
                    }

                    // Link made
                    TargetLinks.Add(FacebookStuff.ExtractUsernamesFromSource(target, source));

                    if (requests.Count == 0)
                    {
                        // For each link made
                        Parallel.ForEach(TargetLinks, targetLink =>
                        {
                            // Run over every possible user and score it
                            Parallel.ForEach(targetLink.usernames, async username =>
                            {
                                lock (innerLock)
                                {
                                    requests.Add(new Object());
                                }

                                // Get the source for this possible facebook match
                                string profileSource = await ExternalBrowser.CallExternalBrowser(username.ProfileLink);

                                lock (innerLock)
                                {
                                    requests.RemoveAt(requests.Count - 1);
                                }

                                // Parse and score the source
                                username.ConfidenceScore = RelationshipStuff.GetConfidenceLevel(SelectedPerson, profileSource);
                                username.UserModelLinkId = targetLink.id;
                                username.FullName        = targetLink.name;

                                // wait until alllllllll the requests are done. If you're curious why in 2017 I need to do this, read the
                                // above giant comment block
                                while (requests.Count > 0)
                                {
                                    System.Windows.Forms.Application.DoEvents();
                                }

                                Console.WriteLine("SCAN COMPLETED AT: " + DateTime.Now.ToString());
                                Sort();
                            });
                        });
                    }
                });
            }
            catch (Exception err) { Console.WriteLine(err.Message); /*Nom nom nom*/ }
        }