2014-10-27 16:29:04 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-03-03 13:41:41 -06:00
|
|
|
import random
|
2014-10-27 16:29:04 -06:00
|
|
|
import BaseHTTPServer
|
|
|
|
import webbrowser
|
|
|
|
from twython import Twython, TwythonError
|
2015-02-04 17:00:03 -06:00
|
|
|
from keys import keyring
|
2014-11-12 20:41:29 -06:00
|
|
|
import authorisationHandler
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2015-02-04 17:00:03 -06:00
|
|
|
|
2014-10-27 16:29:04 -06:00
|
|
|
class twitter(object):
|
|
|
|
|
2015-03-08 00:36:41 -06:00
|
|
|
def login(self, user_key, user_secret, verify_credentials):
|
2015-02-04 17:00:03 -06:00
|
|
|
self.twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), user_key, user_secret)
|
2015-03-08 00:36:41 -06:00
|
|
|
if verify_credentials == True:
|
|
|
|
self.credentials = self.twitter.verify_credentials()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2014-11-12 20:41:29 -06:00
|
|
|
def authorise(self, settings):
|
2015-03-03 13:41:41 -06:00
|
|
|
authorisationHandler.logged = False
|
|
|
|
port = random.randint(30000, 66000)
|
|
|
|
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', port), authorisationHandler.handler)
|
2015-03-01 20:41:02 -06:00
|
|
|
twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), auth_endpoint='authorize')
|
2015-03-03 13:41:41 -06:00
|
|
|
auth = twitter.get_authentication_tokens("http://127.0.0.1:{0}".format(port,))
|
2014-10-27 16:29:04 -06:00
|
|
|
webbrowser.open_new_tab(auth['auth_url'])
|
2014-11-12 20:41:29 -06:00
|
|
|
while authorisationHandler.logged == False:
|
2014-10-27 16:29:04 -06:00
|
|
|
httpd.handle_request()
|
2015-02-04 17:00:03 -06:00
|
|
|
self.twitter = Twython(keyring.get("api_key"), keyring.get("api_secret"), auth['oauth_token'], auth['oauth_token_secret'])
|
2014-11-12 20:41:29 -06:00
|
|
|
final = self.twitter.get_authorized_tokens(authorisationHandler.verifier)
|
|
|
|
self.save_configuration(settings, final["oauth_token"], final["oauth_token_secret"])
|
2015-03-03 13:41:41 -06:00
|
|
|
httpd.server_close()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2014-11-12 20:41:29 -06:00
|
|
|
def save_configuration(self, settings, user_key, user_secret):
|
|
|
|
settings["twitter"]["user_key"] = user_key
|
|
|
|
settings["twitter"]["user_secret"] = user_secret
|
|
|
|
settings.write()
|