def __init__(self, username):
        # Set up token url
        self.url_req_token = "http://www.flickr.com/services/oauth/request_token"
        self.url_access_token = "http://www.flickr.com/services/oauth/access_token"

        # Set up app keys
        self.keys = hidden.keys()

        # Initialization of database
        self.db_init()

        cur.execute('''SELECT user_id,token_id FROM Users WHERE name = ? ''',
                    (username, ))

        user_result = cur.fetchone()
        print user_result
        print

        if user_result is None:

            print "Username not found in database. Re-initialize user authorization..."
            print
            self.user_authorize()
        else:
            print "User logged before. Auto log-in......"
            print
            self.user_id = user_result[0]

            token_id = user_result[1]
            cur.execute('''SELECT token, secret FROM Tokens WHERE id = ?''',
                        (token_id, ))
            result = cur.fetchone()

            self.tokens["token"] = result[0]
            self.tokens["token_secret"] = result[1]
	def __init__(self,username):
		# Set up token url
		self.url_req_token = "http://www.flickr.com/services/oauth/request_token"
		self.url_access_token = "http://www.flickr.com/services/oauth/access_token"

		# Set up app keys
		self.keys = hidden.keys()

		# Initialization of database
		self.db_init()

		cur.execute('''SELECT user_id,token_id FROM Users WHERE name = ? ''',(username, ) )

		user_result = cur.fetchone()
		print  user_result
		print

		if user_result is None:

			print "Username not found in database. Re-initialize user authorization..."
			print
			self.user_authorize()
		else:
			print "User logged before. Auto log-in......"
			print
			self.user_id = user_result[0]

			token_id = user_result[1]
			cur.execute('''SELECT token, secret FROM Tokens WHERE id = ?''',(token_id, ) )
			result = cur.fetchone()

			self.tokens["token"] = result[0]
			self.tokens["token_secret"] = result[1]
    def __init__(self,
                 tokens,
                 nojsoncallback=True,
                 format='json',
                 parameters=None):

        self.keys = hidden.keys()
        self.dbtokens = tokens

        self.dbtokens["token"].encode("ascii")
        self.dbtokens["token_secret"].encode('ascii')

        if self.dbtokens is not None:

            self.consumer = oauth.OAuthConsumer(
                self.keys["oauth_consumer_key"],
                self.keys["oauth_consumer_secret"])
            self.token = oauth.OAuthToken(
                self.dbtokens["token"].encode("ascii"),
                self.dbtokens["token_secret"].encode("ascii"))

            if nojsoncallback:
                self.nojsoncallback = 1
            else:
                self.nojsoncallback = 0
            if not parameters:
                parameters = {}

            self.url = "https://api.flickr.com/services/rest"

            defaults = {
                "format": format,
                "nojsoncallback": self.nojsoncallback,
                "oauth_timestamp": oauth.generate_timestamp(),
                "oauth_nonce": oauth.generate_nonce(),
                "signature_method": "HMAC-SHA1",
                "oauth_token": self.token.key,
                "api_key": self.consumer.key
            }
            #print defaults
            self.parameters = defaults

        else:
            print "token is none"
	def __init__(self, tokens, nojsoncallback=True, format='json', parameters=None):

		self.keys = hidden.keys()
		self.dbtokens = tokens

		self.dbtokens["token"].encode("ascii")
		self.dbtokens["token_secret"].encode('ascii')

		if self.dbtokens is not None:

			self.consumer = oauth.OAuthConsumer(self.keys["oauth_consumer_key"], self.keys["oauth_consumer_secret"])
			self.token = oauth.OAuthToken(self.dbtokens["token"].encode("ascii"), self.dbtokens["token_secret"].encode("ascii"))

			if nojsoncallback:
				self.nojsoncallback = 1
			else:
				self.nojsoncallback = 0
			if not parameters:
				parameters = {}

			self.url = "https://api.flickr.com/services/rest"

			defaults = {
				"format": format,
				"nojsoncallback": self.nojsoncallback,
				"oauth_timestamp": oauth.generate_timestamp(),
				"oauth_nonce": oauth.generate_nonce(),
				"signature_method": "HMAC-SHA1",
				"oauth_token": self.token.key,
				"api_key": self.consumer.key
			}
			#print defaults
			self.parameters = defaults

		else:
			print "token is none"
    def user_authorize(self):

        defaults = hidden.keys().copy()
        defaults["oauth_timestamp"] = oauth.generate_timestamp()
        defaults["oauth_nonce"] = oauth.generate_nonce()
        defaults["oauth_signature_method"] = "HMAC-SHA1"
        defaults["oauth_version"] = "1.0"
        defaults["oauth_callback"] = "https://www.flickr.com/"

        # Setup the consumer with api_key and api_secret
        consumer = oauth.OAuthConsumer(defaults["oauth_consumer_key"],
                                       defaults["oauth_consumer_secret"])
        # Create request
        oauth_req = oauth.OAuthRequest(http_method="GET",
                                       http_url=self.url_req_token,
                                       parameters=defaults)
        # Create signature
        oauth_req.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
                               consumer, None)

        url = oauth_req.to_url()

        print '* Calling Flickr...'
        print
        connection = urllib.urlopen(url)
        data = connection.read()

        request_token = {
            "oauth_token": re.findall("oauth_token=(.+)&", data)[0],
            "oauth_token_secret": re.findall("oauth_token_secret=(.+)",
                                             data)[0]
        }

        #print request_token
        token = oauth.OAuthToken(request_token["oauth_token"],
                                 request_token["oauth_token_secret"])

        print "Go to the following link in your browser:"
        print "http://www.flickr.com/services/oauth/authorize?oauth_token=%s&perms=read" % request_token[
            'oauth_token']
        print

        oauth_verifier = raw_input("Enter the verifier - ")
        print

        defaults["oauth_token"] = request_token["oauth_token"]
        defaults["oauth_verifier"] = oauth_verifier

        del defaults["oauth_consumer_secret"]

        oauth_req = oauth.OAuthRequest(http_method="GET",
                                       http_url=self.url_access_token,
                                       parameters=defaults)
        oauth_req.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
                               consumer, token)

        url = oauth_req.to_url()
        connection = urllib.urlopen(url)
        data = connection.read()

        defaults["oauth_token"] = re.findall("oauth_token=(.+?)&", data)[0]
        defaults["oauth_token_secret"] = re.findall(
            "oauth_token_secret=(.+?)&", data)[0]
        defaults["username"] = re.findall("username=(.+)", data)[0]
        defaults["user_nsid"] = re.findall("user_nsid=(.+?)&", data)[0]

        self.tokens["token"] = defaults["oauth_token"]
        self.tokens["token_secret"] = defaults["oauth_token_secret"]

        # Replace %40 in user_id, or the request url would be wrong
        if "%40" in defaults["user_nsid"]:
            self.user_id = defaults["user_nsid"].replace("%40", "@")
            print self.user_id
        else:
            self.user_id = defaults["user_nsid"]

        cur.execute('''INSERT INTO Tokens (token, secret) VALUES (?, ?)''',
                    (defaults["oauth_token"], defaults["oauth_token_secret"]))

        # Named placeholders style
        cur.execute('SELECT id FROM Tokens WHERE token=:t AND secret=:ts', {
            "t": defaults["oauth_token"],
            "ts": defaults["oauth_token_secret"]
        })
        token_id = cur.fetchone()[0]

        # Store username in database
        cur.execute(
            '''
			INSERT OR IGNORE INTO Users (name, token_id, user_id) VALUES (?, ?, ?)''',
            (defaults["username"], token_id, self.user_id))

        conn.commit()
	def user_authorize(self):

		defaults = hidden.keys().copy()
		defaults["oauth_timestamp"] = oauth.generate_timestamp()
		defaults["oauth_nonce"] = oauth.generate_nonce()
		defaults["oauth_signature_method"] = "HMAC-SHA1"
		defaults["oauth_version"] = "1.0"
		defaults["oauth_callback"] = "https://www.flickr.com/"

		# Setup the consumer with api_key and api_secret
		consumer = oauth.OAuthConsumer(defaults["oauth_consumer_key"], defaults["oauth_consumer_secret"])
		# Create request
		oauth_req = oauth.OAuthRequest(http_method="GET", http_url=self.url_req_token, parameters=defaults)
		# Create signature
		oauth_req.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),consumer, None)

		url = oauth_req.to_url()

		print '* Calling Flickr...'
		print
		connection = urllib.urlopen(url)
		data = connection.read()

		request_token = {
			"oauth_token": re.findall("oauth_token=(.+)&",data)[0],
			"oauth_token_secret": re.findall("oauth_token_secret=(.+)",data)[0]
		}

		#print request_token
		token = oauth.OAuthToken(request_token["oauth_token"], request_token["oauth_token_secret"])

		print "Go to the following link in your browser:"
		print "http://www.flickr.com/services/oauth/authorize?oauth_token=%s&perms=read" % request_token['oauth_token']
		print

		oauth_verifier = raw_input("Enter the verifier - ")
		print

		defaults["oauth_token"] = request_token["oauth_token"]
		defaults["oauth_verifier"] = oauth_verifier

		del defaults["oauth_consumer_secret"]

		oauth_req = oauth.OAuthRequest(http_method="GET", http_url=self.url_access_token, parameters=defaults)
		oauth_req.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),consumer, token)

		url = oauth_req.to_url()
		connection = urllib.urlopen(url)
		data = connection.read()

		defaults["oauth_token"] = re.findall("oauth_token=(.+?)&", data)[0]
		defaults["oauth_token_secret"] = re.findall("oauth_token_secret=(.+?)&", data)[0]
		defaults["username"] = re.findall("username=(.+)",data)[0]
		defaults["user_nsid"] = re.findall("user_nsid=(.+?)&",data)[0]

		self.tokens["token"] = defaults["oauth_token"]
		self.tokens["token_secret"] = defaults["oauth_token_secret"]

		# Replace %40 in user_id, or the request url would be wrong
		if "%40" in defaults["user_nsid"]:
			self.user_id = defaults["user_nsid"].replace("%40","@")
			print self.user_id
		else:
			self.user_id = defaults["user_nsid"]

		cur.execute('''INSERT INTO Tokens (token, secret) VALUES (?, ?)''',
			(defaults["oauth_token"], defaults["oauth_token_secret"]) )

		# Named placeholders style
		cur.execute('SELECT id FROM Tokens WHERE token=:t AND secret=:ts', {"t":defaults["oauth_token"], "ts":defaults["oauth_token_secret"]} )
		token_id = cur.fetchone()[0]

		# Store username in database
		cur.execute('''
			INSERT OR IGNORE INTO Users (name, token_id, user_id) VALUES (?, ?, ?)'''
			,(defaults["username"], token_id, self.user_id) )

		conn.commit()