Skip to content

Commit 3bac701

Browse files
committed
Collect pandoc urls from github api
1 parent 7111c16 commit 3bac701

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

pypandoc/pandoc_download.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
2+
import json
33
import logging
44
import os
55
import os.path
@@ -14,8 +14,10 @@
1414
import urllib
1515
try:
1616
from urllib.request import urlopen
17+
from urllib.parse import urlparse
1718
except ImportError:
1819
from urllib import urlopen
20+
from urlparse import urlparse
1921

2022
from .handler import logger, _check_log_handler
2123

@@ -42,36 +44,38 @@ def _get_pandoc_urls(version="latest"):
4244
:return: str version: actual pandoc version. (e.g. "latest" will be resolved to the actual one)
4345
"""
4446
# url to pandoc download page
45-
url = "https://github.com/jgm/pandoc/releases/" + \
46-
("tag/" if version != "latest" else "") + version
47+
url = "https://api.github.com/repos/jgm/pandoc/releases/" + \
48+
("tags/" if version != "latest" else "") + version
4749
# try to open the url
4850
try:
4951
response = urlopen(url)
50-
version_url_frags = response.url.split("/")
51-
version = version_url_frags[-1]
5252
except urllib.error.HTTPError as e:
5353
raise RuntimeError("Invalid pandoc version {}.".format(version))
54-
return
55-
# read the HTML content
56-
response = urlopen(f"https://github.com/jgm/pandoc/releases/expanded_assets/{version}")
57-
content = response.read()
54+
# read json response
55+
data = json.loads(response.read())
5856
# regex for the binaries
5957
uname = platform.uname()[4]
60-
processor_architecture = "arm" if uname.startswith("arm") or uname.startswith("aarch") else "amd"
61-
regex = re.compile(fr"/jgm/pandoc/releases/download/.*(?:{processor_architecture}|x86|mac).*\.(?:msi|deb|pkg)")
62-
# a list of urls to the binaries
63-
pandoc_urls_list = regex.findall(content.decode("utf-8"))
58+
processor_architecture = (
59+
"arm" if uname.startswith("arm") or uname.startswith("aarch") else "amd"
60+
)
61+
regex = re.compile(
62+
rf"/jgm/pandoc/releases/download/.*(?:{processor_architecture}|x86|mac)"
63+
r".*\.(?:msi|deb|pkg)"
64+
)
6465
# actual pandoc version
65-
version = pandoc_urls_list[0].split('/')[5]
66+
version = data["tag_name"]
6667
# dict that lookup the platform from binary extension
6768
ext2platform = {
6869
'msi': 'win32',
6970
'deb': 'linux',
7071
'pkg': 'darwin'
71-
}
72-
# parse pandoc_urls from list to dict
73-
# py26 don't like dict comprehension. Use this one instead when py26 support is dropped
74-
pandoc_urls = {ext2platform[url_frag[-3:]]: (f"https://github.com{url_frag}") for url_frag in pandoc_urls_list}
72+
}
73+
# collect pandoc urls from json content
74+
pandoc_urls = dict()
75+
for asset in data["assets"]:
76+
download_url = asset["browser_download_url"]
77+
if regex.match(urlparse(download_url).path):
78+
pandoc_urls[ext2platform.get(asset["name"][-3:])] = download_url
7579
return pandoc_urls, version
7680

7781

0 commit comments

Comments
 (0)