commit cc159c20792ddd923f0aa0996e56d8d8994f8ba2
parent 8f8640a1c024c2ef85fa8e8d9297ea289134472d
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Sun, 11 Oct 2015 01:04:59 +0200
voted
Diffstat:
republique.py | | | 96 | ------------------------------------------------------------------------------- |
vote.py | | | 96 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 96 insertions(+), 96 deletions(-)
diff --git a/republique.py b/republique.py
@@ -1,96 +0,0 @@
-#!/usr/bin/python3
-# Automate tasks with www.republique-numerique.fr
-# Only to facilitate your life, please use responsibly
-
-from bs4 import BeautifulSoup
-import json
-import requests
-import sys
-
-HEADERS = { 'User-Agent': 'Mozilla' }
-HEADERS_JSON = {
- 'Accept': "application/json"
- }
-HEADERS_JSON.update(HEADERS)
-
-def login():
- """return a requests session and API token"""
-
- 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"
-
- s = requests.Session()
-
- data = s.get(LOGIN, headers=HEADERS)
- login_tree = BeautifulSoup(data.text, 'html.parser')
-
- 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)
-
-
diff --git a/vote.py b/vote.py
@@ -0,0 +1,96 @@
+#!/usr/bin/python3
+# Automate tasks with www.republique-numerique.fr
+# Only to facilitate your life, please use responsibly
+
+from bs4 import BeautifulSoup
+import json
+import requests
+import sys
+
+HEADERS = { 'User-Agent': 'Mozilla' }
+HEADERS_JSON = {
+ 'Accept': "application/json"
+ }
+HEADERS_JSON.update(HEADERS)
+
+def login():
+ """return a requests session and API token"""
+
+ 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"
+
+ s = requests.Session()
+
+ data = s.get(LOGIN, headers=HEADERS)
+ login_tree = BeautifulSoup(data.text, 'html.parser')
+
+ 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)
+
+