~jaapz-b/t00mblr/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python

#	t00mblr - a GTK tumblr client
#	By: Jaap Broekhuizen <jaapz.b@gmail.com>
#	Website: blog.jaapz.nl
#	
#	License:
#	This file is part of t00mblr.
#
#   t00mblr is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   t00mblr is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with t00mblr.  If not, see <http://www.gnu.org/licenses/>.

'''This are the common functions'''

import gnomekeyring as gk, ConfigParser, sys

from tumblr import *
from debug import *
d = debugging(False)

config = None
config_path = "t00mblr.cfg"
auth = None
	
def get_config():
	"""
		Gets the configuration
	"""
	config.read(config_path)
		
def get_password(username):
	"""
		Gets the password
	"""
	try:
		attrs = { "app" : "t00mblr", "username" : username }
		items = gk.find_items_sync(gk.ITEM_NETWORK_PASSWORD, attrs)
		if len(items):
			return items[0].secret
	except Exception, e:
		return False

def set_password(username, password):
	"""
		Sets the password
	"""
	kr = gk.get_default_keyring_sync()
	attrs = { "app" : "t00mblr", "username" : username }
	try:
		gk.item_create_sync (kr, gk.ITEM_NETWORK_PASSWORD, "t00mblr", attrs,
				password, True)
	except:
		return False
		
def check_password_changed(username, password):
	"""
		Checks whether the password has changed
	"""
	if get_password(username) != password:
		return True
	else:
		return False
		
def authenticate():
	"""
		Authenticates to the tumblr API
	"""
	d.debug("Trying to authenticate")
	get_config()
	
	# get the vars we need
	blog = config.get("User", "blog")
	password = get_password(blog)
	email = config.get("User", "email")
	
	auth = Api(blog, email, password)
	
	try:
		auth.auth_check()
		d.debug("Authentication succesfull!")
		return auth
	except TumblrAuthError:
		d.error("Error authenticating! You've probably used incorrect login data...")
		return False
	except TumblrRequestError:
		d.error("Error connecting! Do you have a working internet connection?")
		return False
	
for a in sys.argv:
	if a == "--debug":
		d.set_mode(True)