Source

django-simpleproxy / utils.py

Full commit
# -*- coding: utf-8 -*-
"""
===========================
utils
===========================
:create_date: 11/09/15 
"""
import re
import urlparse
import os
import time
import urllib2
import sys
import uuid

from django.core.cache import cache
from django.conf import settings
from lw.utils import http


def url_generator(request):

    fqdn = http.get_full_path(request)
    proxy_url = '%s/%s' % (fqdn, settings.SIMPLEPROXY_URL)
    pattern_http = re.compile(r"^%s|^http://" % fqdn)

    def _generate(proxy_url, url):
        clean_url = url.lstrip('http://')
        return os.path.join(proxy_url, clean_url)
#        uid = str(uuid.uuid4())
#        cache.set("%s-%s" % ('simpleproxy', uid), uid)
#        return os.path.join(proxy_url, uid, url.lstrip('http://'))

    def genarate(url, proxy=True):
        if not url:
            return url
        if pattern_http.search(url) and proxy:
            return _generate(proxy_url, url)
        return urlparse.urljoin(fqdn, url)

    return genarate


def exists_url(uid):
    cuid = u"%s-%s" % ('simpleproxy', uid)
    if uid == cache.get(cuid):
        cache.delete(cuid)
        return True
    else:
        return False


def url_read(path):
    def url_open(path, restart=10):
        if restart < 1:
            raise urllib2.HTTPError

        try:
            return urllib2.urlopen(path)
        except urllib2.URLError:
            time.sleep(0.2)
            restart -= 1
            return url_open(path, restart)

    if '://' not in path:
        path = 'http://' + path

    try:
        response = url_open(path)
    except urllib2.HTTPError:
        response = sys.exc_info()[1]

    return response.read()


def join(url1, *rest):
    if not rest:
        return url1
    url2, rest = rest[0], rest[1:]
    if url1.endswith('/'):
        if url2.startswith('/'):
            res = join(url1 + url2[1:], *rest)
        else:
            res = join(url1 + url2, *rest)
    elif url2.startswith('/'):
        res = join(url1 + url2, *rest)
    else:
        res = join(url1 + '/' + url2, *rest)
    return res