Beispiel #1
0
        private void Log_Click(object sender, EventArgs e)
        {
            // Used to isolate errors
            EasyLogger.Error("The log file path is: " + EasyLogger.LogFile);

            // Used to isolate warnings
            EasyLogger.Warning("The log file path is: " + EasyLogger.LogFile);

            // Used to isolate information
            EasyLogger.Info("The log file path is: " + EasyLogger.LogFile);
        }
        private static void Main()
        {
            int tries = 60;

            TryAgain :;
            try
            {
                if (!SQLConnection.IsServerConnected(Properties.Settings.Default.FollowersConnectionString))
                {
                    tries--;
                    if (tries > 0)
                    {
                        EasyLogger.Warning("SQL Server is not responding. I'll try again " + tries + " more times until I require user input...");
                        Thread.Sleep(2000);
                        goto TryAgain;
                    }
                    else
                    {
                        DialogResult result = MessageBox.Show("SQL Server is not responding. Would you like to try again?", "Social Post Scheduler", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (result == DialogResult.Yes)
                        {
                            goto TryAgain;
                        }
                        else
                        {
                            Environment.Exit(0);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                EasyLogger.Error(ex);
            }

            try
            {
                System.Reflection.Assembly assembly = typeof(Program).Assembly;
                GuidAttribute attribute             = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];
                string        id = attribute.Value;

                StartApp();
            }
            catch (Exception ex)
            {
                EasyLogger.Error("Program - @Main(1): " + ex);
                MessageBox.Show(ex.Message, "TwitterFollower", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #3
0
        private async Task <string> FollowAsync()
        {
            string response = null;

            Follow twitter = new Follow(_API_KEY, _ACCESS_TOKEN, _API_SECRET_KEY, _ACCESS_TOKEN_SECRET, _BEARER_TOKEN);

            response = await Task.Run(() => twitter.FollowAsync(_SCREEN_NAME));

            if (response != "OK")
            {
                EasyLogger.Warning(response);

                MessageBox.Show(response, "Social Post Scheduler", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                EasyLogger.Info("Requested message posted successfully!");
            }

            return("OK");
        }
        /// <summary>
        /// Publish a post with image
        /// </summary>
        /// <returns>result</returns>
        /// <param name="message">message to publish</param>
        /// <param name="pathToImage">image to attach</param>
        internal string PublishTweet(string message)
        {
            try
            {
                Task <string> rezText = Task.Run(async() =>
                {
                    string response = await Tweet(TweetLimit.CutTweetToLimit(message));
                    return(response);
                });

                if (rezText.Result != "OK")
                {
                    EasyLogger.Warning(rezText.Result);
                }

                return("OK");
            }
            catch (Exception ex)
            {
                EasyLogger.Error("TweetMessage - @PublishTweet(1): " + ex);
                return(ex.Message);
            }
        }
        /// <summary>
        /// {instagram_id}/media?image_url={image_url}&access_token={access_token}
        /// {instagram_id}/media_publish?creation_id={creation-id}&access_token={access_token}
        /// </summary>
        internal static void PostToInstagram()
        {
            try
            {
                string USER_TOKEN = GetToken.RenewAccessToken(Properties.Settings.Default.InstagramToken, Properties.Settings.Default.InstagramAppSecret, Properties.Settings.Default.InstagramAppID);
                if (USER_TOKEN.Contains("ERROR:"))
                {
                    EasyLogger.Error(USER_TOKEN);
                }
                else
                {
                    if (USER_TOKEN != "The remote server returned an error: (400) Bad Request.")
                    {
                        EasyLogger.Info("Your Instagram user token has been renewed successfully!");

                        Properties.Settings.Default.InstagramToken = USER_TOKEN;
                        Properties.Settings.Default.Save();
                        Properties.Settings.Default.Reload();
                    }
                    else
                    {
                        EasyLogger.Warning("Error: The remote server returned an error: (400) Bad Request.");
                        return;
                    }

                    HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(Properties.Settings.Default.WebsiteFeed);
                    hwr.Accept = "text/xml, */*";
                    hwr.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us");
                    hwr.UserAgent = "Mozilla";
                    hwr.KeepAlive = true;
                    hwr.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

                    using (var resp = (HttpWebResponse)hwr.GetResponse())
                    {
                        using (Stream s = resp.GetResponseStream())
                        {
                            string   cs = String.IsNullOrEmpty(resp.CharacterSet) ? "UTF-8" : resp.CharacterSet;
                            Encoding e  = Encoding.GetEncoding(cs);

                            using (StreamReader sr = new StreamReader(s, e))
                            {
                                var allXml = sr.ReadToEnd();

                                allXml = Regex.Replace(allXml,
                                                       "(.*)<script type='text/javascript'>.+?</script>(.*)",
                                                       "$1$2",
                                                       RegexOptions.Singleline);

                                using (XmlReader xmlr = XmlReader.Create(new StringReader(allXml)))
                                {
                                    SyndicationFeed        feedContent = SyndicationFeed.Load(xmlr);
                                    List <SyndicationItem> feedItems   = new List <SyndicationItem>();

                                    foreach (SyndicationItem item in feedContent.Items)
                                    {
                                        feedItems.Add(item);
                                    }
                                    feedItems.Reverse();
                                    feedContent.Items = feedItems;

                                    if (null == feedContent)
                                    {
                                        return;
                                    }

                                    foreach (SyndicationItem item in feedContent.Items)
                                    {
                                        string quota_total = string.Empty;
                                        string quota_usage = string.Empty;
                                        try
                                        {
                                            using (WebClient wb = new WebClient())
                                            {
                                                string responseInString = wb.DownloadString("https://graph.facebook.com/" + Properties.Settings.Default.InstagramPageID + "/content_publishing_limit?fields=config&access_token=" + Properties.Settings.Default.InstagramToken);

                                                JObject jdata = JObject.Parse(responseInString);
                                                JToken  array = jdata["data"];
                                                foreach (JToken info in array)
                                                {
                                                    quota_total = info["config"]["quota_total"].ToString();
                                                }
                                            }

                                            using (WebClient wb = new WebClient())
                                            {
                                                string responseInString = wb.DownloadString("https://graph.facebook.com/" + Properties.Settings.Default.InstagramPageID + "/content_publishing_limit?fields=quota_usage&access_token=" + Properties.Settings.Default.InstagramToken);

                                                JObject jdata = JObject.Parse(responseInString);
                                                JToken  array = jdata["data"];

                                                foreach (JToken info in array)
                                                {
                                                    quota_usage = info["quota_usage"].ToString();
                                                }
                                            }
                                        }
                                        catch { }

                                        int usage = Convert.ToInt32(quota_usage);
                                        int total = Convert.ToInt32(quota_total);

                                        if (usage >= total)
                                        {
                                            EasyLogger.Info("Instagram post quota reached!");
                                            break;
                                        }

                                        try
                                        {
                                            System.Collections.ObjectModel.Collection <SyndicationLink> links = item.Links;

                                            string         title = item.Title.Text + " - " + links[0].Uri.ToString();
                                            string         image = ExtensionMethods.GetEnclosureUri(item);
                                            DateTimeOffset date  = item.PublishDate;

                                            if (StringToDTOffset(Properties.Settings.Default.PublishDate) < date)
                                            {
                                                Task <string> success    = PostAsync(image, title);
                                                int           posts_used = Convert.ToInt32(quota_usage);
                                                posts_used++;
                                                EasyLogger.Info(success.Result + Environment.NewLine + "Instagram Post usage for the day: " + posts_used + " of " + quota_total);

                                                Properties.Settings.Default.PublishDate = date.ToString();
                                                Properties.Settings.Default.Save();
                                                Properties.Settings.Default.Reload();
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            EasyLogger.Error(ex);
                                            continue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                EasyLogger.Error(ex);
            }
        }
        internal static async Task <string> PostMessage(string PAGE_NAME, string USER_ID, string PAGE_ID, string USER_TOKEN, bool isImage, string MESSAGE_STRING, string IMAGE_PATH, string LINK_URL)
        {
            try
            {
                using (WebClient wb = new WebClient())
                {
                    string responseInString = wb.DownloadString("https://graph.facebook.com/" + USER_ID + "/accounts?access_token=" + USER_TOKEN);

                    JObject jdata = JObject.Parse(responseInString);
                    JToken  array = jdata["data"];

                    foreach (JToken account in array)
                    {
                        if (account["name"].ToString().Equals(PAGE_NAME))
                        {
                            string PAGE_TOKEN = account["access_token"].ToString();

                            try
                            {
                                string response = null;

                                if (!isImage)
                                {
                                    response = await Task.Run(() => FacebookText.PostAsync(PAGE_ID, PAGE_TOKEN, MESSAGE_STRING, LINK_URL));

                                    if (response != "OK")
                                    {
                                        EasyLogger.Warning(response);

                                        Form.ActiveForm.TopMost     = true;
                                        Form.ActiveForm.WindowState = FormWindowState.Minimized;
                                        Form.ActiveForm.Show();
                                        Form.ActiveForm.ShowInTaskbar = true;
                                        Form.ActiveForm.WindowState   = FormWindowState.Normal;
                                        Form.ActiveForm.WindowState   = Properties.Settings.Default.windowState;
                                        Form.ActiveForm.BringToFront();
                                        Form.ActiveForm.Focus();
                                        Form.ActiveForm.Activate();
                                        Form.ActiveForm.TopMost = false;

                                        Scheduler.tabControlOne.SelectedIndex = 4;
                                    }
                                    else
                                    {
                                        EasyLogger.Info("Requested message posted successfully!");

                                        return("OK");
                                    }
                                }
                                else if (isImage)
                                {
                                    response = await Task.Run(() => FacebookImage.PostAsync(PAGE_ID, PAGE_TOKEN, MESSAGE_STRING, IMAGE_PATH));

                                    if (response != "OK")
                                    {
                                        EasyLogger.Warning(response);

                                        Form.ActiveForm.TopMost     = true;
                                        Form.ActiveForm.WindowState = FormWindowState.Minimized;
                                        Form.ActiveForm.Show();
                                        Form.ActiveForm.ShowInTaskbar = true;
                                        Form.ActiveForm.WindowState   = FormWindowState.Normal;
                                        Form.ActiveForm.WindowState   = Properties.Settings.Default.windowState;
                                        Form.ActiveForm.BringToFront();
                                        Form.ActiveForm.Focus();
                                        Form.ActiveForm.Activate();
                                        Form.ActiveForm.TopMost = false;

                                        Scheduler.tabControlOne.SelectedIndex = 4;
                                    }
                                    else
                                    {
                                        EasyLogger.Info("Requested message posted successfully!");

                                        return("OK");
                                    }
                                }

                                return("OK");
                            }
                            catch (Exception ex)
                            {
                                EasyLogger.Error("GetToken - @PostMessage(1): " + ex);

                                return("Error: " + ex.Message);
                            }
                        }
                    }
                }

                return(string.Empty);
            }
            catch (Exception ex)
            {
                EasyLogger.Error("GetToken - @GetPageToken(1): " + ex);

                return(ex.Message);
            }
        }