Example #1
0
        /// <summary>
        /// Initializes Twitter API
        /// </summary>
        private static bool InitializeTwitterApi(string accessToken, string accessTokenSecret)
        {
            if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(accessTokenSecret))
            {
                _twitter = new TwitterAPI(accessToken, accessTokenSecret);
            }

            if ((!string.IsNullOrEmpty(_twitter.oAuthTwitter.Token) && !string.IsNullOrEmpty(_twitter.oAuthTwitter.TokenSecret)))
            {
                // Verify that stored token and secret are still valid
                if (_twitter.VerifyCredentials())
                {
                    InitializeTwitterApi();

                    return(true);
                }
                else
                {
                    // If stored token/secret are not valid, clear then from registry
                    CommonFunctions.SetSetting("Twitter_AccessToken", "");
                    CommonFunctions.SetSetting("Twitter_AccessTokenSecret", "");

                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Initialized Facebook session
        /// </summary>
        private void InitializeFacebook()
        {
            if (bool.Parse(CommonFunctions.GetSetting("EventsEnabled_Service_Facebook")))
            {
                _fbApi = new FacebookAPI
                {
                    IsDesktopApplication = true,
                    ApplicationKey       = _fbAppId,
                };

                // If persistent session key was found, use it, else login
                if (!string.IsNullOrEmpty(_fbAccessToken = CommonFunctions.GetSetting("Facebook_AccessToken")) && !_fbAccessToken.Equals("false"))
                {
                    _fbApi.AccessToken = _fbAccessToken;

                    try
                    {
                        string uid = _fbApi.GetLoggedInUser();

                        // If uid was not found, session is invalid
                        if (string.IsNullOrEmpty(uid))
                        {
                            _fbApi.AccessToken = "";

                            CommonFunctions.SetSetting("Facebook_AccessToken", "");

                            FacebookLogin(false);
                        }
                    }
                    catch (Exception)
                    {
                        _fbApi.AccessToken = "";

                        CommonFunctions.SetSetting("Facebook_AccessToken", "");

                        FacebookLogin(false);
                    }
                }
                else
                {
                    FacebookLogin(false);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Displays Facebook login dialog
        /// </summary>
        /// <param name="forceLogin">Force login</param>
        /// <returns>True if login succeeded, false if failed.</returns>
        private bool FacebookLogin(bool forceLogin)
        {
            bool retVal = false;

            try
            {
                // If connection to Facebook was successfull
                if (_fbApi.ConnectToFacebook(forceLogin))
                {
                    retVal = true;

                    if (!_fbApi.SessionExpires)
                    {
                        // If session doesn't expire and connect was successfull, store session info in settings file
                        try
                        {
                            CommonFunctions.SetSetting("Facebook_AccessToken", _fbApi.AccessToken);
                        }
                        catch (UnauthorizedAccessException)
                        {
                            _host.MediaCenterEnvironment.Dialog("Offline access data could not be stored in registry. Check that you have permissions to registry.",
                                                                AppName, DialogButtons.Ok, 10, false);
                        }
                    }
                }
                else
                {
                    _host.MediaCenterEnvironment.Dialog("Facebook login failed.", AppName, DialogButtons.Ok, 10, true);
                }
            }
            catch (Facebook.Exceptions.FacebookInvalidUserException)
            {
                _host.MediaCenterEnvironment.Dialog("Facebook login failed, invalid user.", AppName, DialogButtons.Ok, 10, true);
            }
            catch (Exception)
            {
                _host.MediaCenterEnvironment.Dialog("Error occurred while logging into Facebook.", AppName, DialogButtons.Ok, 10, true);
            }

            return(retVal);
        }
Example #4
0
        /// <summary>
        /// Updates Facebook status
        /// </summary>
        /// <param name="newStatus"></param>
        private void UpdateFacebook(string[] newStatus)
        {
            if (bool.Parse(CommonFunctions.GetSetting("EventsEnabled_Service_Facebook")))
            {
                if (MyAddIn.FbApi != null && !string.IsNullOrEmpty(MyAddIn.FbApi.AccessToken))
                {
                    try
                    {
                        bool updateConfirmed = true;

                        // Confirm Facebook update from the user
                        if (bool.Parse(CommonFunctions.GetSetting("ConfirmUpdate_Facebook")))
                        {
                            DialogResult confirmUpdateResult = ShowDialog("Publish to Facebook: '" + newStatus[0] + "'?", true, 10, DialogButtons.Yes | DialogButtons.No);

                            // If update isn't confirmed or dialog times out, do not prompt for confirmation again
                            if (confirmUpdateResult == DialogResult.No || confirmUpdateResult == DialogResult.Timeout)
                            {
                                updateConfirmed = false;
                                _previousStatus = newStatus[0];
                            }
                        }

                        if (updateConfirmed)
                        {
                            try
                            {
                                attachment attachment    = null;
                                bool       streamEnabled = bool.Parse(CommonFunctions.GetSetting("Service_Facebook_StreamEnabled"));
                                bool       statusEnabled = bool.Parse(CommonFunctions.GetSetting("Service_Facebook_StatusEnabled"));

                                if (streamEnabled)
                                {
                                    string amazonLocaleApiUrl = CommonFunctions.GetSetting("Service_All_AmazonLocaleUrl");

                                    // If Amazon queries are enabled for current media
                                    if (!string.IsNullOrEmpty(newStatus[1]))
                                    {
                                        string[] amazonDetails = { "", "", "", "", "", "", "" };
                                        // Title, DetailUrl, ImageUrl, Error, AverageRating, Artist

                                        amazonDetails = Query.SearchItems(newStatus[1], newStatus[2], amazonDetails, amazonLocaleApiUrl);

                                        // If ASIN exists, continue search
                                        if (!string.IsNullOrEmpty(amazonDetails[4]))
                                        {
                                            amazonDetails = Query.LookupItem(amazonDetails, amazonLocaleApiUrl);

                                            if (string.IsNullOrEmpty(amazonDetails[3]))
                                            {
                                                attachment = new attachment();

                                                // For videos/tv, show video/tv title as a link
                                                if (newStatus[2].Equals("Video") && newStatus[2].StartsWith("TV"))
                                                {
                                                    attachment.caption = "";
                                                    attachment.name    = amazonDetails[0]; // Artist
                                                }
                                                else
                                                {
                                                    attachment.caption = amazonDetails[0]; // Album
                                                    attachment.name    = amazonDetails[6]; // Artist
                                                }

                                                attachment.href        = amazonDetails[1];
                                                attachment.description = null;
                                                attachment.properties  = new attachment_property()
                                                {
                                                    category = null, /*new attachment_category()
                                                                      * {
                                                                      * href = "",
                                                                      * text = ""
                                                                      * }*/
                                                    ratings  = !string.IsNullOrEmpty(amazonDetails[5]) ? amazonDetails[5] + " stars" : null
                                                };

                                                attachment.media = new List <attachment_media>()
                                                {
                                                    new attachment_media_image()
                                                    {
                                                        src  = amazonDetails[2],
                                                        href = amazonDetails[1]
                                                    }
                                                };
                                            }
                                        }
                                        else
                                        {
                                            attachment = null;
                                        }
                                    }
                                    else
                                    {
                                        attachment = null;
                                    }

                                    // Finally post to user's Wall and News feed
                                    if (!string.IsNullOrEmpty(MyAddIn.FbApi.StreamPublish(newStatus[0], attachment, null, "", 0)))
                                    {
                                        _previousStatus = newStatus[0];

                                        MyAddIn.StatusHasBeenChanged = true;
                                    }
                                }

                                if (statusEnabled)
                                {
                                    if (string.IsNullOrEmpty(MyAddIn.FbApi.SetStatus(newStatus[0], true)))
                                    {
                                        _previousStatus = newStatus[0];

                                        MyAddIn.StatusHasBeenChanged = true;
                                    }
                                }
                            }
                            catch (Facebook.Exceptions.FacebookSigningException)
                            {
                                MyAddIn.FbApi.AccessToken = "";
                                CommonFunctions.SetSetting("Facebook_AccessToken", "");

                                ShowDialog("Facebook session is invalid, opening login screen...", false, 2, DialogButtons.Ok);
                            }
                            catch (Facebook.Exceptions.FacebookTimeoutException)
                            {
                                MyAddIn.FbApi.AccessToken = "";
                                CommonFunctions.SetSetting("Facebook_AccessToken", "");

                                ShowDialog("Facebook session is invalid, opening login screen...", false, 2, DialogButtons.Ok);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        ShowDialog("Unable to publish to Facebook." + e.Message, false, 2, DialogButtons.Ok);
                    }
                }
            }
        }