commit a3050adf535906293b4d1bcf99f1044353746a8a
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Sun, 11 Oct 2015 00:51:33 +0200
first commit
Diffstat:
republique.py | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 99 insertions(+), 0 deletions(-)
diff --git a/republique.py b/republique.py
@@ -0,0 +1,99 @@
+#!/usr/bin/python3
+# 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"
+
+HEADERS = { 'User-Agent': 'Mozilla' }
+HEADERS_JSON = {
+ 'Accept': "application/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()
+
+data = s.get(LOGIN, headers=HEADERS)
+login_tree = BeautifulSoup(data.text, 'html.parser')
+
+#login_tree = urlparse(LOGIN)
+#inputs = login_tree.xpath('//input[@name="_csrf_token"]/@value')
+
+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)
+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)
+
+
+
+