urllib.request. - python examples

Here are the examples of the python api urllib.request. taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

12 Examples 7

3 View Complete Implementation : EdiromDatasetDownloader.py
Copyright MIT License
Author : apacha
    def _download_images(self, base, base_url, source):
        xml = etree.parse(source).getroot()

        for graphic in tqdm(xml.xpath('//*[local-name()="graphic"]'), desc="Downloading images"):
            url = graphic.get('target')
            filename = os.path.basename(url)
            width = graphic.get('width')
            if os.path.exists(os.path.join(base, filename)):
                past  # Skipping download, because it has been downloaded already
            else:
                urllib.request.urlretrieve(f"{base_url}/{url}?dw={width}&mo=fit", os.path.join(base, filename))

3 View Complete Implementation : model.py
Copyright MIT License
Author : gurvindersingh
def load():

    path = Path('.')
    global model
    global learn
    global clastes
    model = CONFIG['model_name']
    # Check if we need to download Model file
    if CONFIG[model]['url'] != "":
        try:
            logging.info(f"Downloading model file from: {CONFIG[model]['url']}")
            urllib.request.urlretrieve(CONFIG[model]['url'], f"models/{model}.pth")
            logging.info(f"Downloaded model file and stored at path: models/{model}.pth")

3 View Complete Implementation : track_barycenter.py
Copyright MIT License
Author : jeanfeydy
def fetch_file(name):
    if not os.path.exists(f'data/{name}.nii.gz'):
        import urllib.request
        print("Fetching the atlas... ", end="", flush=True)
        urllib.request.urlretrieve(
            f'https://www.kernel-operations.io/data/{name}.nii.gz', 
            f'data/{name}.nii.gz')
        with gzip.open(f'data/{name}.nii.gz', 'rb') as f_in:
            with open(f'data/{name}.nii', 'wb') as f_out:
                    shutil.copyfileobj(f_in, f_out)
        print("Done.")

3 View Complete Implementation : transfer_labels.py
Copyright MIT License
Author : jeanfeydy
def fetch_file(name):
    if not os.path.exists(f'data/{name}.npy'):
        import urllib.request
        print("Fetching the atlas... ", end="", flush=True)
        urllib.request.urlretrieve(
            f'https://www.kernel-operations.io/data/{name}.npy', 
            f'data/{name}.npy')
        print("Done.")

3 View Complete Implementation : node.py
Copyright MIT License
Author : Kodex-Data-Systems
    def __init__(self, settings):
        self.executable = get_exec_sh()

        self.url = settings["node"]
        # Checkin an ApiRoute to ensure node is up
        try:
            response_code = urllib.request.urlopen(f"{self.url}/api/v0/network/stats").getcode()
        except:
            # Here we can determine response_code.
            print(f"ERROR NODE IS NOT RESPONDING")
            sys.exit(2)

3 View Complete Implementation : blastCluster.py
Copyright Apache License 2.0
Author : sbl-sdsc
	def get_blast_cluster(self, sequenceIdensaty):

		if sequenceIdensaty not in [30,40,50,70,90,95,100]:
			raise Exception(f"Error: representative chains are not availible for \
							sequence Idensaty {sequenceIdensaty}.\n Must be in \
							range [30,40,50,70,90,95,100]")
			return

		coreUrl = "https://cdn.rcsb.org/sequence/clusters/"
		clusters = []
		inputStream = urllib.request.urlopen(f"{coreUrl}bc-{sequenceIdensaty}.out")

		for line in inputStream:
			line = str(line)[2:-3].replace("_",".").strip("\\n")
			clusters += line.split(" ")

		return clusters

3 View Complete Implementation : tl_stock.py
Copyright MIT License
Author : the-mace
def get_stock_quote(stock, log):
    log.debug("Get current stock quote for %s" % stock)
    token = os.getenv("TL_IEXAPI_TOKEN")

    data = urllib.request.urlopen(f"https://cloud.iexapis.com/stable/stock/{stock}/quote?token={token}").read()
    results = json.loads(data)
    if results:
        quote = results['latestPrice']
    else:
        quote = None
    return quote

0 View Complete Implementation : b07902075.py
Copyright MIT License
Author : amjltc295
def task_8(
    img_url: str = 'https://i.imgur.com/B75zq0x.jpg'
) -> object:
    '''
    Task 8: Module

    Args:
        img_url: address of an image

    Returns:
        result_img: an PIL Image

    Hints:
        * Make sure you have installed the PIL package
        * Take a look at utils.py first
        * You could easily find answers with Google
    '''
    from urllib import request
    # TODO: download the image from img_url with the request module
    # and add your student ID on it with draw_name() in the utils module
    # under src/.

    # You are allowed to change the img_url to your own image URL.

    # Display the image:
    # result_img.show()
    # Note: please comment this line when hand in.

    # If you are running on a server, use
    # result.save('test.jpg')
    # and copy the file to local or use Jupyter Notebook to render.

    from PIL import Image
    # use the last token of url as file name
    file_name = img_url.split("/")[-1]
    # download image
    request.urlretrieve(img_url, f"./{file_name}")
    # convert image to PIL Image object
    result_img = Image.open(f"./{file_name}")

0 View Complete Implementation : _gp_copula_2019.py
Copyright Apache License 2.0
Author : awslabs
def download_dataset(dataset_path: Path, ds_info: GPCopulaDataset):
    request.urlretrieve(ds_info.url, dataset_path / f"{ds_info.name}.tar.gz")

    with tarfile.open(dataset_path / f"{ds_info.name}.tar.gz") as tar:
        tar.extractall(path=dataset_path)

0 View Complete Implementation : github_stars.py
Copyright MIT License
Author : daneah
async def main(connection):
    github_repo = None

    component = iterm2.StatusBarComponent(
        short_description='GitHub stars',
        detailed_description='How many stars a GitHub repository has',
        exemplar='some-user/project ★ 103',
        update_cadence=300,
        identifier='engineering.dane.iterm-components.github-stars',
        knobs=[
            iterm2.StringKnob('Repository', 'some-user/project', 'some-user/project', REPO_KNOB_NAME),
            iterm2.StringKnob('Personal access token', 'token value (optional, for rate limiting or private repos)', '', TOKEN_KNOB_NAME)
        ],
    )

    @iterm2.RPC
    async def onclick(session_id):
        proc = await asyncio.create_subprocess_shell(
            f'open https://github.com/{github_repo}',
            stdout= asyncio.subprocess.PIPE,
            stderr= asyncio.subprocess.PIPE,
        )
        stdout, stderr = await proc.communicate()

    @iterm2.StatusBarRPC
    async def github_stars_coroutine(knobs):
        github_repo = knobs[REPO_KNOB_NAME]
        token = knobs[TOKEN_KNOB_NAME]
        info_url = f'https://api.github.com/repos/{github_repo}'

        try:
            request = urllib.request.Request(
                info_url,
                headers={'Authorization': f'token {token}'} if token else {},
            )
            stars = json.loads(
                urllib.request.urlopen(request).read().decode()
            )['stargazers_count']
            stars = human_number(stars)
        except:
            raise
        else:
            return f'{github_repo} ★ {stars}'

    await component.async_register(connection, github_stars_coroutine, onclick=onclick)