2121from urllib .parse import urlparse
2222
2323import requests
24- from packageurl .contrib .purl2url import get_download_url
24+ from packageurl .contrib import purl2url
25+
26+ from fetchcode .utils import _http_exists
2527
2628
2729class Response :
@@ -90,18 +92,19 @@ def fetch_ftp(url, location):
9092 return resp
9193
9294
93- def fetch_purl (purl , location = None ):
95+ def resolve_purl (purl ):
9496 """
95- Return a `Response` object built from fetching the content at a PURL based `purl` URL string
96- saving the content in a file at `location`
97+ Resolve a Package URL (PURL) to a download URL.
98+
99+ This function attempts to resolve the PURL using both the purl2url library and
100+ the fetchcode.download_urls module. It returns the first valid download URL found.
97101 """
98102 from fetchcode .download_urls import download_url as get_download_url_from_fetchcode
99103
100- for resolver in (get_download_url , get_download_url_from_fetchcode ):
104+ for resolver in (purl2url . get_download_url , get_download_url_from_fetchcode ):
101105 url = resolver (purl )
102- if url :
103- return fetch (url = url )
104- return
106+ if url and _http_exists (url ):
107+ return url
105108
106109
107110def fetch (url ):
@@ -111,13 +114,16 @@ def fetch(url):
111114 """
112115 url_parts = urlparse (url )
113116 scheme = url_parts .scheme
114- location = None
115117
116- if scheme != "pkg" :
117- temp = tempfile .NamedTemporaryFile (delete = False )
118- location = temp .name
118+ if scheme == "pkg" :
119+ url = resolve_purl (url )
120+ url_parts = urlparse (url )
121+ scheme = url_parts .scheme
122+
123+ temp = tempfile .NamedTemporaryFile (delete = False )
124+ location = temp .name
119125
120- fetchers = {"ftp" : fetch_ftp , "http" : fetch_http , "https" : fetch_http , "pkg" : fetch_purl }
126+ fetchers = {"ftp" : fetch_ftp , "http" : fetch_http , "https" : fetch_http }
121127
122128 if scheme in fetchers :
123129 return fetchers .get (scheme )(url , location )
0 commit comments