Skip to content

Commit 2a130fe

Browse files
committed
Fix the pypi downloadable url
Signed-off-by: jiyeong.seok <[email protected]>
1 parent 0cced58 commit 2a130fe

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/fosslight_util/_get_downloadable_url.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,35 @@ def get_downloadable_url(link):
103103

104104

105105
def get_download_location_for_pypi(link):
106-
# get the url for downloading source file in pypi.org/project/(oss_name)/(oss_version)/#files
106+
# get the url for downloading source file: https://docs.pypi.org/api/ Predictable URLs
107107
ret = False
108108
new_link = ''
109+
host = 'https://files.pythonhosted.org'
109110

110111
try:
111112
dn_loc_re = re.findall(r'pypi.org\/project\/?([^\/]*)\/?([^\/]*)', link)
112113
oss_name = dn_loc_re[0][0]
114+
oss_name = re.sub(r"[-_.]+", "-", oss_name).lower()
113115
oss_version = dn_loc_re[0][1]
114116

115-
pypi_url = 'https://pypi.org/project/' + oss_name + '/' + oss_version + '/#files'
116-
117-
content = urlopen(pypi_url).read().decode('utf8')
118-
bs_obj = BeautifulSoup(content, 'html.parser')
119-
120-
card_file_list = bs_obj.findAll('div', {'class': 'card file__card'})
121-
122-
for card_file in card_file_list:
123-
file_code = card_file.find('code').text
124-
if file_code.lower() == "source":
125-
new_link = card_file.find('a').attrs['href']
117+
new_link = f'{host}/packages/source/{oss_name[0]}/{oss_name}/{oss_name}-{oss_version}.tar.gz'
118+
try:
119+
res = urlopen(new_link)
120+
if res.getcode() == 200:
126121
ret = True
127-
break
122+
else:
123+
logger.warning(f'Cannot find the valid link for pypi (url:{new_link}')
124+
except Exception as e:
125+
oss_name = re.sub(r"[-]+", "_", oss_name).lower()
126+
new_link = f'{host}/packages/source/{oss_name[0]}/{oss_name}/{oss_name}-{oss_version}.tar.gz'
127+
res = urlopen(new_link)
128+
if res.getcode() == 200:
129+
ret = True
130+
else:
131+
logger.warning(f'Cannot find the valid link for pypi (url:{new_link}')
128132
except Exception as error:
129133
ret = False
130-
logger.warning('Cannot find the link for pypi (url:'+link+') '+str(error))
134+
logger.warning(f'Cannot find the link for pypi (url:{link}({(new_link)})) e:{str(error)}')
131135

132136
return ret, new_link
133137

0 commit comments

Comments
 (0)