-
Notifications
You must be signed in to change notification settings - Fork 10
/
addcard.py
executable file
·91 lines (71 loc) · 2.12 KB
/
addcard.py
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
#!/usr/bin/env python
from urllib import urlencode
import urllib2, cookielib
from lxml import etree
from lxml.cssselect import CSSSelector
import getpass
import sys
import os
import time
from RFUID import rfid
BASE_URL = 'https://london.hackspace.org.uk/'
cookiejar = cookielib.CookieJar()
processor = urllib2.HTTPCookieProcessor(cookiejar)
opener = urllib2.build_opener(processor)
urllib2.install_opener(opener)
def browse(url, params=None):
if params is not None:
params = urlencode(params)
page = urllib2.urlopen(BASE_URL + url, params)
return etree.HTML(page.read())
find_exception = CSSSelector('.alert-danger')
if len(sys.argv) > 1:
print 'Checking for card... (scan card on the RFID reader attached to this computer)'
uid = None
while uid is None:
try:
with rfid.Pcsc.reader() as reader:
for tag in reader.pn532.scan():
uid = tag.uid.upper()
break
except rfid.NoCardException:
pass
time.sleep(0.1)
print 'Card UID is %s' % uid
else:
uid = raw_input('Card UID: ')
email = raw_input('Email: ')
password = getpass.getpass('Password: ')
print
login = browse('login.php')
token = login.xpath('//input[@name="token"]')[0]
logged_in = browse('login.php', {
'token': token.attrib['value'],
'email': email,
'password': password,
'submit': 'Log In',
})
exc = find_exception(logged_in)
if exc:
print 'Could not authenticate:'
print
print etree.tostring(exc[0], method="text", encoding='utf-8', pretty_print=True)
sys.exit(1)
logout_a = logged_in.xpath('//a[@href="/logout"]')
if not logout_a:
print 'Could not log in'
sys.exit(1)
addcard = browse('/members/addcard.php')
token = addcard.xpath('//input[@name="token"]')[0]
card_added = browse('/members/addcard.php', {
'token': token.attrib['value'],
'uid': uid,
'submit': 'Add',
})
exc = find_exception(card_added)
if exc:
print 'Could not modify entry:'
print
print etree.tostring(exc[0], method="text", encoding='utf-8', pretty_print=True)
sys.exit(1)
print "Card %s successfully added" % uid