summaryrefslogtreecommitdiff
path: root/ricky/im.py
blob: 715d825b73b171647dedbe3615799dc4604adffc (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
import urllib
import urllib2
import sys
import random
import simplejson as json
import urllib2

class Im:
    def __init__(self):
        self._required_keys = []
        self.url = ""
    def post_request(self, 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
    def call(self, params):
        if not(params.is_ready()):
          raise Exception("Im Params Not Ready")
        return json.loads(self.post_request(self.url, params.as_dict()))