summaryrefslogtreecommitdiff
path: root/ricky/utils.py
blob: 04e20747d20ebf716f412faa4923a4a5e221903d (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
50
51
52
53
54
55
56
57
58
59
import os
import sys
import urllib
import urllib2
import simplejson as json
from ricky.config import OFFLINE, PB_DATA_URL


def data_from_url(url):
    """
    retrieves image params from db using the url
    """
    newfile = os.path.split(url)[-1]
    if OFFLINE:
        sys.path.append("./photoblaster")
        from photoblaster.db.models.imcmd import ImCmd
        result = ImCmd.search(newfile=newfile).first()
        try:
            return {
                "module": result.tag.split(":")[0],
                "params": json.loads(result.dataobj)
            }
        except AttributeError:
            sys.stderr.write("No usable data found in db\n")
            return None
    else:
        print http_request("%s?newfile=%s" % (PB_DATA_URL, newfile))
        raise NotImplementedError("Not yet implemented\n")


def http_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:
        if params:
            req = urllib2.Request(url, params, headers)
        else:
            req = urllib2.Request(url, params, headers)
        response = urllib2.urlopen(req)
        return response.read()
    except ValueError:
        sys.stderr.write(
            "Bad Post params or Url sent to photoblaster"
            "api.\n"
        )
    except urllib2.URLError:
        sys.stderr.write(
            "Could not complete post request to the given url:\n" +
            ("URL: %s\n" % url) +
            ("PARAMS: %s\n" % params)
        )