~jaapz-b/t00mblr/trunk

29 by Jaap Broekhuizen
Added files
1
#!/usr/bin/env python
2
3
#	t00mblr - a GTK tumblr client
4
#	By: Jaap Broekhuizen <jaapz.b@gmail.com>
5
#	Website: blog.jaapz.nl
6
#	
7
#	License:
8
#	This file is part of t00mblr.
9
#
10
#   t00mblr is free software: you can redistribute it and/or modify
11
#   it under the terms of the GNU General Public License as published by
12
#   the Free Software Foundation, either version 3 of the License, or
13
#   (at your option) any later version.
14
#
15
#   t00mblr is distributed in the hope that it will be useful,
16
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
#   GNU General Public License for more details.
19
#
20
#   You should have received a copy of the GNU General Public License
21
#   along with t00mblr.  If not, see <http://www.gnu.org/licenses/>.
22
23
'''This are the common functions'''
24
25
import gnomekeyring as gk, ConfigParser, sys
26
27
from tumblr import *
28
from debug import *
29
d = debugging(False)
30
31
config = None
32
config_path = "t00mblr.cfg"
33
auth = None
34
	
35
def get_config():
36
	"""
37
		Gets the configuration
38
	"""
39
	config.read(config_path)
40
		
41
def get_password(username):
42
	"""
43
		Gets the password
44
	"""
45
	try:
46
		attrs = { "app" : "t00mblr", "username" : username }
47
		items = gk.find_items_sync(gk.ITEM_NETWORK_PASSWORD, attrs)
48
		if len(items):
49
			return items[0].secret
50
	except Exception, e:
51
		return False
52
53
def set_password(username, password):
54
	"""
55
		Sets the password
56
	"""
57
	kr = gk.get_default_keyring_sync()
58
	attrs = { "app" : "t00mblr", "username" : username }
59
	try:
60
		gk.item_create_sync (kr, gk.ITEM_NETWORK_PASSWORD, "t00mblr", attrs,
61
				password, True)
62
	except:
63
		return False
64
		
65
def check_password_changed(username, password):
66
	"""
67
		Checks whether the password has changed
68
	"""
69
	if get_password(username) != password:
70
		return True
71
	else:
72
		return False
73
		
74
def authenticate():
75
	"""
76
		Authenticates to the tumblr API
77
	"""
78
	d.debug("Trying to authenticate")
79
	get_config()
80
	
81
	# get the vars we need
82
	blog = config.get("User", "blog")
83
	password = get_password(blog)
84
	email = config.get("User", "email")
85
	
86
	auth = Api(blog, email, password)
87
	
88
	try:
89
		auth.auth_check()
90
		d.debug("Authentication succesfull!")
91
		return auth
92
	except TumblrAuthError:
93
		d.error("Error authenticating! You've probably used incorrect login data...")
94
		return False
95
	except TumblrRequestError:
96
		d.error("Error connecting! Do you have a working internet connection?")
97
		return False
98
	
99
for a in sys.argv:
100
	if a == "--debug":
101
		d.set_mode(True)