Revision 663737326139 () - Diff

Link to this snippet: https://friendpaste.com/4IyqA2qaONYkpyUQDKLHxR
Embed:
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
import _midgard as midgard

# Set up the repository config
configuration = midgard.config()
configuration.dbtype = 'SQLite'
configuration.database = 'midgardexample'
# this will store to SQLite in ~/.midgard2/data/midgardexample.db

# Open a Midgard repository connection
connection = midgard.connection()
connection.open_config(configuration)

import urllib, urllib2
import simplejson as json

class QaikuUser():
user = None

def __init__(self, username):
try:
self.user = midgard.db.user({'login': username, 'authtype': 'APIkey'})
except:
self.user = midgard.db.user()
self.user.login = username
self.user.authtype = 'APIkey'
self.user.active = True
self.user.create()
if (self.user.password is None):
self.ask_password()

self.user.log_in()
def ask_password(self):
password = raw_input('Please type your Qaiku API key: ')
if (self.check_password(password) is False):
self.ask_password()
self.user.password = password
self.user.update()
def check_password(self, password):
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'adventure_tablet/0.1')]
try:
params = urllib.urlencode({'apikey': password})
url = 'http://www.qaiku.com/api/statuses/user_timeline.json?%s' % params
req = opener.open(url)
except urllib2.HTTPError, e:
print('Sorry, authorization failed.')
return False
except urllib2.URLError, e:
print("Connection failed, error %s. Try again later" % (e.message))
return False

return True
# Match OS user to Midgard user
import getpass
username = getpass.getuser()
user = QaikuUser(username)
print "Can has Qaiku"