aboutsummaryrefslogtreecommitdiffstats
path: root/dlpcvp.py
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2018-06-19 18:11:33 +0200
committerMatěj Cepl <mcepl@cepl.eu>2018-06-19 18:11:33 +0200
commitf1ed1220a287e3321a4efaeb32a3613ef4d7942d (patch)
tree73479d5173ec51bf78666c78b0954a6c8da82bf6 /dlpcvp.py
downloaddlp_check_version_PyPI-f1ed1220a287e3321a4efaeb32a3613ef4d7942d.tar.gz
Get version of a package from PyPI.
Diffstat (limited to 'dlpcvp.py')
-rwxr-xr-xdlpcvp.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/dlpcvp.py b/dlpcvp.py
new file mode 100755
index 0000000..189ee2a
--- /dev/null
+++ b/dlpcvp.py
@@ -0,0 +1,24 @@
+#!/usr/bin/python3
+
+import json
+import sqlite3
+import urllib.request
+
+# PyPI API documentation https://warehouse.readthedocs.io/api-reference/
+PyPI_base = "https://pypi.org/pypi/{}/json"
+
+def get_version_from_pypi(name, etag=None):
+ req = urllib.request.Request(url=PyPI_base.format(name))
+
+ if etag is not None:
+ req.add_header('ETag', etag)
+
+ with urllib.request.urlopen(req) as resp:
+ if resp.getcode() == 200:
+ data = json.load(resp)
+ info_dict = data['info']
+ return info_dict['name'], info_dict['version'], resp.info()['ETag']
+ else:
+ IOError(resp.info())
+
+print(get_version_from_pypi('m2crypto'))