Beispiel #1
0
        public static FB_KEYWORD getFBKeywordInfoFromFBViaTwinglyMaExcluded(String fbKeywordKeyword, String access_token, String twinglyApi15Url)
        {
            // create object and set values whatever available
            var fbKeyword = new FB_KEYWORD();

            fbKeyword.keyword = fbKeywordKeyword;
            fbKeyword.date_oldest_retrieve      = DateTime.Today;
            fbKeyword.date_latest_retrieve      = DateTime.Today;
            fbKeyword.matched_posts_count       = 0;
            fbKeyword.matched_comments_count    = 0;
            fbKeyword.matched_total_count       = 0;
            fbKeyword.social_stats_likes        = 0;
            fbKeyword.social_stats_comments     = 0;
            fbKeyword.social_stats_shares       = 0;
            fbKeyword.matched_posts_count_ma    = 0;
            fbKeyword.matched_comments_count_ma = 0;
            fbKeyword.matched_total_count_ma    = 0;
            fbKeyword.social_stats_likes_ma     = 0;
            fbKeyword.social_stats_comments_ma  = 0;
            fbKeyword.social_stats_shares_ma    = 0;

            // assign object rest of value from FB via twingly
            // getKeywordInfoFromFBViaTwingly(fbKeyword, twinglyApi15Url, access_token);
            getKeywordInfoFromFBViaTwingly(fbKeyword, twinglyApi15Url, access_token, limitToMorocco: false);

            //
            return(fbKeyword);
        }
Beispiel #2
0
        private static void getKeywordInfoFromFBViaTwingly(FB_KEYWORD fbKeyword, String twinglyApi15Url, String access_token, bool limitToMorocco = false)
        {
            String url = twinglyApi15Url + "chart" + "?apikey=" + access_token + "&one=\"" + fbKeyword.keyword + "\"";

            // limit to Morocco
            if (limitToMorocco)
            {
                url += "&country=ma";
            }

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());

                String  objText  = reader.ReadToEnd();
                JObject jObjects = JObject.Parse(objText);
                JObject Objects  = new JObject(jObjects);
                JArray  items    = (JArray)Objects["data"];

                //
                foreach (var item in items)
                {
                    // update date of latest and oldest
                    DateTime time_iso8610 = Convert.ToDateTime(item["time_iso8610"]);
                    if (time_iso8610 < fbKeyword.date_oldest_retrieve)
                    {
                        fbKeyword.date_oldest_retrieve = time_iso8610;
                    }
                    if (time_iso8610 > fbKeyword.date_latest_retrieve)
                    {
                        fbKeyword.date_latest_retrieve = time_iso8610;
                    }

                    // cumulate
                    if (!limitToMorocco)
                    {
                        fbKeyword.matched_posts_count    += Convert.ToInt32(item["matched_posts_count"]);
                        fbKeyword.matched_comments_count += Convert.ToInt32(item["matched_comments_count"]);
                        fbKeyword.matched_total_count    += Convert.ToInt32(item["matched_total_count"]);
                        fbKeyword.social_stats_likes     += Convert.ToInt32(item["social_stats"]["likes"]);
                        fbKeyword.social_stats_comments  += Convert.ToInt32(item["social_stats"]["comments"]);
                        fbKeyword.social_stats_shares    += Convert.ToInt32(item["social_stats"]["shares"]);
                    }
                    else
                    {
                        fbKeyword.matched_posts_count_ma    += Convert.ToInt32(item["matched_posts_count"]);
                        fbKeyword.matched_comments_count_ma += Convert.ToInt32(item["matched_comments_count"]);
                        fbKeyword.matched_total_count_ma    += Convert.ToInt32(item["matched_total_count"]);
                        fbKeyword.social_stats_likes_ma     += Convert.ToInt32(item["social_stats"]["likes"]);
                        fbKeyword.social_stats_comments_ma  += Convert.ToInt32(item["social_stats"]["comments"]);
                        fbKeyword.social_stats_shares_ma    += Convert.ToInt32(item["social_stats"]["shares"]);
                    }
                }
            }
        }
Beispiel #3
0
        public void ut_170721_test_get_keyword_frequencies()
        {
            // "yan3al"
            String fbKeywordKeyword1 = "ينعل";
            String fbKeywordKeyword2 = "ينعآل";

            String twinglyApiKey   = "2A4CF6A4-4968-46EF-862F-2881EF597A55";
            String twinglyApi15Url = "https://data.twingly.net/socialfeed/a/api/v1.5/";

            //
            FB_KEYWORD fbKeyword1 = OADRJNLPCommon.Business.Business.getFBKeywordInfoFromFBViaTwingly(fbKeywordKeyword1, twinglyApiKey, twinglyApi15Url);
            FB_KEYWORD fbKeyword2 = OADRJNLPCommon.Business.Business.getFBKeywordInfoFromFBViaTwingly(fbKeywordKeyword2, twinglyApiKey, twinglyApi15Url);

            //
            int occurrenceKeyword1 = fbKeyword1.matched_total_count_ma;
            int occurrenceKeyword2 = fbKeyword2.matched_total_count_ma;

            //
            Assert.IsTrue(occurrenceKeyword1 > occurrenceKeyword2);

            // TODO assert "way greater"
            Assert.IsTrue((occurrenceKeyword1 - occurrenceKeyword2) > occurrenceKeyword2 / 2);
        }
Beispiel #4
0
        public static String getMostPopularVariantFromFBViaTwingly(List <String> variants, String twinglyApi15Url, String twinglyApiKey)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            //
            FB_KEYWORD mostPopular = null;

            foreach (String variant in variants)
            {
                FB_KEYWORD fbKeyword = getFBKeywordInfoFromFBViaTwinglyMaOnly(variant, twinglyApiKey, twinglyApi15Url);
                if (mostPopular == null)
                {
                    mostPopular = fbKeyword;
                }
                else if (mostPopular.matched_total_count_ma < fbKeyword.matched_total_count_ma)
                {
                    mostPopular = fbKeyword;
                }

                // small timer to workaout the twingly limit of 60 sec
                // the code that you want to measure comes here
                var elapsedMs = watch.ElapsedMilliseconds;
                if (elapsedMs > 55000)
                {
                    System.Threading.Thread.Sleep(80000);
                    watch.Reset();
                }
            }

            // if no occurences at all, then serach not morococco only
            if (mostPopular.matched_total_count_ma == 0)
            {
                foreach (String variant in variants)
                {
                    FB_KEYWORD fbKeyword = getFBKeywordInfoFromFBViaTwinglyMaExcluded(variant, twinglyApiKey, twinglyApi15Url);
                    if (mostPopular == null)
                    {
                        mostPopular = fbKeyword;
                    }
                    else if (mostPopular.matched_total_count < fbKeyword.matched_total_count)
                    {
                        mostPopular = fbKeyword;
                    }

                    // small timer to workaout the twingly limit of 60 sec
                    // the code that you want to measure comes here
                    var elapsedMs = watch.ElapsedMilliseconds;
                    if (elapsedMs > 55000)
                    {
                        System.Threading.Thread.Sleep(80000);
                        watch.Reset();
                    }
                }
                watch.Stop();
            }
            else
            {
                watch.Stop();

                //
                return(mostPopular.keyword);
            }


            // worst case return empty string
            if (mostPopular.matched_total_count == 0)
            {
                return(String.Empty);
            }

            //
            return(mostPopular.keyword);
        }