import six


def urlpathjoin(*parts):
    """Join multiple paths into a single url

    >>> urlpathjoin('https://storage.scrapinghub.com:8002/', 'jobs', '1/2/3')
    'https://storage.scrapinghub.com:8002/jobs/1/2/3'
    >>> urlpathjoin('https://storage.scrapinghub.com:8002', 'jobs', '1/2/3', None)
    'https://storage.scrapinghub.com:8002/jobs/1/2/3'
    >>> urlpathjoin(_, 'state')
    'https://storage.scrapinghub.com:8002/jobs/1/2/3/state'
    >>> urlpathjoin(_, None)
    'https://storage.scrapinghub.com:8002/jobs/1/2/3/state'
    >>> urlpathjoin(78)
    '78'
    >>> urlpathjoin('78')
    '78'
    >>> urlpathjoin('s', 78)
    's/78'
    >>> urlpathjoin('s', 78, 'foo')
    's/78/foo'
    >>> urlpathjoin('s/78/foo')
    's/78/foo'
    >>> urlpathjoin((111, 'jobs'), 33)
    '111/jobs/33'
    >>> urlpathjoin('http://localhost:8003/', ('jobs', '1111111'), '2/1')
    'http://localhost:8003/jobs/1111111/2/1'

    """
    url = None
    for p in parts:
        if p is None:
            continue
        elif isinstance(p, tuple):
            p = urlpathjoin(*p)
        elif not isinstance(p, six.text_type):
            p = six.text_type(p)

        url = p if url is None else u'{0}/{1}'.format(url.rstrip(u'/'), p)

    return url