Example #1
0
class TwitterUserRegistration(sublime_plugin.WindowCommand):
    def __init__(self, window):
        self.window = window

    def register(self):
        self.settingsController = SublimeTweetSettingsController()
        self.proxy_host, self.proxy_port = None, None
        if self.settingsController.s["use_proxy"]:
            (self.proxy_host, self.proxy_port) = self.settingsController.s["twitter_proxy_config"]["http"].split(":")
            try:
                self.proxy_host, self.proxy_port = str(self.proxy_host), int(self.proxy_port)
            except:
                print "Can't parse your proxy settings, sorry"
                sublime.status_message("Can't parse your proxy settings, sorry")
                return

        if not self.settingsController.s["twitter_have_token"]:
            self.TwitterAccessTokenRequester = TwitterAccessTokenRequester(
                consumer_key, consumer_secret, self.proxy_host, self.proxy_port
            )
            try:
                url = self.TwitterAccessTokenRequester.getTokenFirstStep()
            except:
                print "Problems with the first step. Try again.\n"
                sublime.status_message("Please, try calling me again. We have some problems with auth")
                return

            if url:
                message = "I've opened a browser for you. Please authorize me and enter the pin:"
                self.window.show_input_panel(message, "", self.on_entered_pin, None, None)
                import webbrowser

                webbrowser.open(url)
            else:
                print "Please try again later"

    def on_entered_pin(self, text):
        try:
            pin = int(text)
            keys = self.TwitterAccessTokenRequester.getTokenSecondStep(pin)
        except ValueError:
            print "We have some problems with pin. Please, try again."
            return
        except:
            print "We have some problems with the second auth step. Please, try again."
            sublime.status_message("We have some problems with the second auth step. Please, try again.")
            return

        self.access_token_key, self.access_token_secret = keys
        sublime.status_message("You are authorized me, thanks! Now you can tweet from Sublime Text 2.")
        self.settingsController.s["twitter_have_token"] = True
        self.settingsController.s["twitter_access_token_key"] = self.access_token_key
        self.settingsController.s["twitter_access_token_secret"] = self.access_token_secret
        self.settingsController.saveSettings()
class TwitterUserRegistration(sublime_plugin.WindowCommand):
    def __init__(self, window):
        self.window = window

    def register(self):
        self.settingsController = SublimeTweetSettingsController()
        self.proxy_host, self.proxy_port = None, None
        if (self.settingsController.s['use_proxy']):
            (self.proxy_host, self.proxy_port) = self.settingsController.s['twitter_proxy_config']['http'].split(':')
            try:
                self.proxy_host, self.proxy_port = str(self.proxy_host), int(self.proxy_port)
            except:
                print 'Can\'t parse your proxy settings, sorry'
                sublime.status_message('Can\'t parse your proxy settings, sorry')
                return

        if (not self.settingsController.s['twitter_have_token']):
            self.TwitterAccessTokenRequester = TwitterAccessTokenRequester(consumer_key, consumer_secret, self.proxy_host, self.proxy_port)
            try:
                url = self.TwitterAccessTokenRequester.getTokenFirstStep()
            except:
                print 'Problems with the first step. Try again.\n'
                sublime.status_message('Please, try calling me again. We have some problems with auth')
                return

            if (url):
                message = 'I\'ve opened a browser for you. Please authorize me and enter the pin:'
                self.window.show_input_panel(message, '', self.on_entered_pin, None, None)
                import webbrowser
                webbrowser.open(url)
            else:
                print 'Please try again later'

    def on_entered_pin(self, text):
        try:
            pin = int(text)
            keys = self.TwitterAccessTokenRequester.getTokenSecondStep(pin)
        except ValueError:
            print 'We have some problems with pin. Please, try again.'
            return
        except:
            print 'We have some problems with the second auth step. Please, try again.'
            sublime.status_message('We have some problems with the second auth step. Please, try again.')
            return

        self.access_token_key, self.access_token_secret = keys
        sublime.status_message('You are authorized me, thanks! Now you can tweet from Sublime Text 2.')
        self.settingsController.s['twitter_have_token'] = True
        self.settingsController.s['twitter_access_token_key'] = self.access_token_key
        self.settingsController.s['twitter_access_token_secret'] = self.access_token_secret
        self.settingsController.saveSettings()