republique

helper scripts for www.republique-numerique.fr
git clone https://a3nm.net/git/republique/
Log | Files | Refs | README

vote.py (2969B)


      1 #!/usr/bin/python3 -u
      2 # Automate tasks with www.republique-numerique.fr
      3 # Only to facilitate your life, please use responsibly
      4 
      5 from bs4 import BeautifulSoup
      6 import json
      7 import requests
      8 import sys
      9 import time
     10 from common import HEADERS, HEADERS_JSON
     11 
     12 def login():
     13     """return a requests session and API token"""
     14 
     15     LOGIN = "https://www.republique-numerique.fr/login"
     16     LOGIN_ACTION = "https://www.republique-numerique.fr/login_check"
     17     API_TOKEN = "https://www.republique-numerique.fr/get_api_token"
     18 
     19     s = requests.Session()
     20 
     21     data = s.get(LOGIN, headers=HEADERS)
     22     time.sleep(1)
     23     login_tree = BeautifulSoup(data.text, 'html.parser')
     24 
     25     csrf = None
     26     for i in login_tree.find_all('input'):
     27         try:
     28             name = i['name']
     29         except KeyError:
     30             name = ""
     31         if name != '_csrf_token':
     32             continue
     33         csrf = i['value']
     34         break
     35 
     36     if not csrf:
     37         print("Could not retrieve CSRF token for login", file=sys.stderr)
     38         sys.exit(2)
     39 
     40     data = {
     41             '_csrf_token': csrf,
     42             '_username': user,
     43             '_password': password,
     44             '_remember_me': 'off',
     45             }
     46 
     47     response = s.post(LOGIN_ACTION, headers=HEADERS, data=data)
     48     time.sleep(1)
     49 
     50     response = s.get(API_TOKEN, headers=HEADERS_JSON)
     51     time.sleep(1)
     52     jdata = json.loads(response.text)
     53     try:
     54         token = jdata['token']
     55     except KeyError:
     56         print("Could not retrieve API token during login\n"
     57         "Maybe the EMAIL/PASSWORD are invalid or the account doesn't exist?",
     58         file=sys.stderr)
     59         sys.exit(2)
     60     return s, token
     61 
     62 
     63 def vote(s, token, res, v):
     64     """vote for res with value v using session s and API token"""
     65     
     66     headers = {
     67             'Authorization': "Bearer " + token,
     68             'Content-Type': "application/json"
     69             }
     70     headers.update(HEADERS_JSON)
     71     data = json.dumps({'value' : v})
     72     # deleting is HTTP method delete, not implemented here
     73     r = s.put('https://www.republique-numerique.fr/api/%s/votes' % res,
     74             headers=headers, data=data)
     75     time.sleep(1)
     76     return (r.status_code)
     77 
     78 
     79 if __name__ == '__main__':
     80     try:
     81         user = sys.argv[1]
     82         password = sys.argv[2]
     83     except IndexError:
     84         print("""Usage: %s EMAIL PASSWORD
     85 Vote as indicated on stdin with republique-numerique.fr account EMAIL/PASSWORD
     86 See README for vote format and details
     87 You must register local account EMAIL/PASSWORD on republique-numerique.fr first"""
     88             % sys.argv[0], file=sys.stderr)
     89         sys.exit(1)
     90 
     91     s, token = login()
     92 
     93     for l in sys.stdin.readlines():
     94         f = l.strip().split(' ')
     95         v = vote(s, token, f[0], f[1])
     96         if v != requests.codes.no_content:
     97             print("Vote for %s failed with status code %d" % (f[0], v),
     98                     file=sys.stderr)
     99         else:
    100             print("Successfully voted %s for %s" % (f[1], f[0]),
    101                     file=sys.stderr)
    102