Skip to content

Commit

Permalink
Collect pandoc urls from github api
Browse files Browse the repository at this point in the history
  • Loading branch information
nstrmx committed Aug 25, 2024
1 parent 7111c16 commit fe9e4ad
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions pypandoc/pandoc_download.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

import json
import logging
import os
import os.path
Expand Down Expand Up @@ -42,36 +42,29 @@ def _get_pandoc_urls(version="latest"):
:return: str version: actual pandoc version. (e.g. "latest" will be resolved to the actual one)
"""
# url to pandoc download page
url = "https://github.com/jgm/pandoc/releases/" + \
("tag/" if version != "latest" else "") + version
url = "https://api.github.com/repos/jgm/pandoc/releases/" + \
("tags/" if version != "latest" else "") + version
# try to open the url
try:
response = urlopen(url)
version_url_frags = response.url.split("/")
version = version_url_frags[-1]
except urllib.error.HTTPError as e:
raise RuntimeError("Invalid pandoc version {}.".format(version))
return
# read the HTML content
response = urlopen(f"https://github.com/jgm/pandoc/releases/expanded_assets/{version}")
content = response.read()
# regex for the binaries
uname = platform.uname()[4]
processor_architecture = "arm" if uname.startswith("arm") or uname.startswith("aarch") else "amd"
regex = re.compile(fr"/jgm/pandoc/releases/download/.*(?:{processor_architecture}|x86|mac).*\.(?:msi|deb|pkg)")
# a list of urls to the binaries
pandoc_urls_list = regex.findall(content.decode("utf-8"))
# read the json response
data = json.loads(response.read())
# actual pandoc version
version = pandoc_urls_list[0].split('/')[5]
version = data["tag_name"]
# dict that lookup the platform from binary extension
ext2platform = {
'msi': 'win32',
'deb': 'linux',
'pkg': 'darwin'
}
# parse pandoc_urls from list to dict
# py26 don't like dict comprehension. Use this one instead when py26 support is dropped
pandoc_urls = {ext2platform[url_frag[-3:]]: (f"https://github.com{url_frag}") for url_frag in pandoc_urls_list}
}
# collect pandoc urls from json content
pandoc_urls = dict()
for asset in data["assets"]:
pf = ext2platform.get(asset["name"][-3:])
if pf:
pandoc_urls[pf] = asset["browser_download_url"]
return pandoc_urls, version


Expand Down

0 comments on commit fe9e4ad

Please sign in to comment.