aboutsummaryrefslogtreecommitdiffstats
path: root/dlpcvp.py
blob: 189ee2ab09e449244816e4adc445a288c527c7ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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'))