summaryrefslogtreecommitdiff
path: root/lib/utils.py
blob: 0ad9aa39bfdfbf96bb529e9705f853d501fa83f1 (plain)
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
import urllib
import urllib2
import sys
import random
def post_request(url, params):
    params = urllib.urlencode(params)
    headers = {
        "Content-type": "application/x-www-form-urlencoded",
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36",
        "Accept": "text/plain"
    }
    try:
        req = urllib2.Request(url, params, headers)
        response = urllib2.urlopen(req)
        return response.read()
    except Exception as e:
        sys.stderr.write(str(e))
        raise 

class Service:
    def __init__(self):
        self._required_keys = []
        self.url = ""
    def new(self, params):
        for k in self._required_keys:
            if not k in params:
                params[k] = "";
        self.current_params = params
        return json.loads(post_request(self.url, self.current_params))

class Pb_Api_Params:
    def _weighted_choice(self, param):
       weights_total = sum(map(lambda x: x["weight"], param))
       choice = random.randint(0, weights_total)
       position = 0
       for elem in param:
          position += elem["weight"]
          if position >= choice:
              return elem["value"]
    def _default_choice(self, param):
       heaviest_idx = 0
       heaviest_weight = 0
       idx = 0
       for elem in param:
          if elem["weight"] > heaviest_weight:
              heaviest_weight = elem["weight"]
              heaviest_idx = idx;
          idx += 1
       return param[heaviest_idx]["value"]