commit 8f8640a1c024c2ef85fa8e8d9297ea289134472d
parent a3050adf535906293b4d1bcf99f1044353746a8a
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Sun, 11 Oct 2015 01:04:53 +0200
continue
Diffstat:
republique.py | | | 151 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 74 insertions(+), 77 deletions(-)
diff --git a/republique.py b/republique.py
@@ -2,33 +2,10 @@
# Automate tasks with www.republique-numerique.fr
# Only to facilitate your life, please use responsibly
-#import sys
-#import bs4
-#
-## http://stackoverflow.com/a/14044031
-## https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=801476
-#sys.modules['BeautifulSoup'] = bs4
-#
-import sys
from bs4 import BeautifulSoup
import json
-#from lxml.html.soupparser import fromstring
-#from urllib.request import Request, urlopen
import requests
-
-
-try:
- user = sys.argv[1]
- password = sys.argv[2]
-except IndexError:
- print(("Usage: %s USERNAME PASSWORD\n"
- "(only works with local republique-numerique.fr accounts") %
- sys.argv[0], file=sys.stderr)
- sys.exit(1)
-
-LOGIN = "https://www.republique-numerique.fr/login"
-LOGIN_ACTION = "https://www.republique-numerique.fr/login_check"
-API_TOKEN = "https://www.republique-numerique.fr/get_api_token"
+import sys
HEADERS = { 'User-Agent': 'Mozilla' }
HEADERS_JSON = {
@@ -36,64 +13,84 @@ HEADERS_JSON = {
}
HEADERS_JSON.update(HEADERS)
-#def urlparse(url):
- # http://stackoverflow.com/a/29546832
- #req = Request(LOGIN, headers=headers)
- #f = urlopen(req)
- #data = f.read().decode('utf-8')
-s = requests.Session()
+def login():
+ """return a requests session and API token"""
-data = s.get(LOGIN, headers=HEADERS)
-login_tree = BeautifulSoup(data.text, 'html.parser')
+ LOGIN = "https://www.republique-numerique.fr/login"
+ LOGIN_ACTION = "https://www.republique-numerique.fr/login_check"
+ API_TOKEN = "https://www.republique-numerique.fr/get_api_token"
-#login_tree = urlparse(LOGIN)
-#inputs = login_tree.xpath('//input[@name="_csrf_token"]/@value')
+ s = requests.Session()
-csrf = None
-for i in login_tree.find_all('input'):
- try:
- name = i['name']
- except KeyError:
- name = ""
- if name != '_csrf_token':
- continue
- csrf = i['value']
- break
-
-if not csrf:
- print("Could not retrieve CSRF token for login", file=sys.stderr)
- sys.exit(2)
-
-data = {
- '_csrf_token': csrf,
- '_username': user,
- '_password': password,
- '_remember_me': 'off',
- }
+ data = s.get(LOGIN, headers=HEADERS)
+ login_tree = BeautifulSoup(data.text, 'html.parser')
-response = s.post(LOGIN_ACTION, headers=HEADERS, data=data)
-print(response)
-#print(response.text)
-print(s.cookies)
-
-response = s.get(API_TOKEN, headers=HEADERS_JSON)
-jdata = json.loads(response.text)
-try:
- token = jdata['token']
-except KeyError:
- print("Could not retrieve API token during login", file=sys.stderr)
- sys.exit(2)
-
-headers = {
- 'Authorization': "Bearer " + token,
- 'Content-Type': "application/json"
- }
-headers.update(HEADERS_JSON)
-data = ('{"value": %d}' % -1)
-r = s.put('https://www.republique-numerique.fr/api/opinions/63/votes',
- headers=headers, data=data)
-print(r)
+ csrf = None
+ for i in login_tree.find_all('input'):
+ try:
+ name = i['name']
+ except KeyError:
+ name = ""
+ if name != '_csrf_token':
+ continue
+ csrf = i['value']
+ break
+
+ if not csrf:
+ print("Could not retrieve CSRF token for login", file=sys.stderr)
+ sys.exit(2)
+
+ data = {
+ '_csrf_token': csrf,
+ '_username': user,
+ '_password': password,
+ '_remember_me': 'off',
+ }
+ response = s.post(LOGIN_ACTION, headers=HEADERS, data=data)
+ response = s.get(API_TOKEN, headers=HEADERS_JSON)
+ jdata = json.loads(response.text)
+ try:
+ token = jdata['token']
+ except KeyError:
+ print("Could not retrieve API token during login", file=sys.stderr)
+ sys.exit(2)
+ return s, token
+
+def vote(s, token, res, v):
+ """vote for res with value v using session s and API token"""
+
+ headers = {
+ 'Authorization': "Bearer " + token,
+ 'Content-Type': "application/json"
+ }
+ headers.update(HEADERS_JSON)
+ data = json.dumps({'value' : v})
+ # deleting is HTTP method delete, not implemented here
+ r = s.put('https://www.republique-numerique.fr/api/%s/votes' % res,
+ headers=headers, data=data)
+ return (r.status_code)
+
+if __name__ == '__main__':
+ try:
+ user = sys.argv[1]
+ password = sys.argv[2]
+ except IndexError:
+ print(("Usage: %s USERNAME PASSWORD\n"
+ "(your USERNAME and PASSWORD"
+ "with a local republique-numerique.fr account)\n"
+ "Performs votes given on stdin, see README") %
+ sys.argv[0], file=sys.stderr)
+ sys.exit(1)
+
+ s, token = login()
+
+ for l in sys.stdin.readlines():
+ f = l.strip().split(' ')
+ v = vote(s, token, f[0], f[1])
+ if v != requests.codes.no_content:
+ print("Vote for %s failed with status code %d" % (res, code),
+ file=sys.stderr)