add support for crates.io

This commit is contained in:
依云 2016-07-22 14:24:07 +08:00
parent b9f5a1e476
commit 375fb4f14f
3 changed files with 31 additions and 1 deletions

View File

@ -5,7 +5,7 @@ logger = logging.getLogger(__name__)
handler_precedence = ( handler_precedence = (
'github', 'gitcafe', 'aur', 'pypi', 'archpkg', 'gems', 'pacman', 'github', 'gitcafe', 'aur', 'pypi', 'archpkg', 'gems', 'pacman',
'cmd', 'bitbucket', 'gcode_hg', 'gcode_svn', 'regex', 'manual', 'vcs', 'cmd', 'bitbucket', 'gcode_hg', 'gcode_svn', 'regex', 'manual', 'vcs',
'npm', 'hackage', 'cpan', 'gitlab', 'packagist' 'cratesio', 'npm', 'hackage', 'cpan', 'gitlab', 'packagist'
) )
def get_version(name, conf, callback): def get_version(name, conf, callback):

View File

@ -0,0 +1,22 @@
import os
import json
from functools import partial
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from ..sortversion import sort_version_keys
API_URL = 'https://crates.io/api/v1/crates/%s'
def get_version(name, conf, callback):
name = conf.get('cratesio') or name
request = HTTPRequest(API_URL % name, user_agent='lilydjwg/nvchecker')
AsyncHTTPClient().fetch(
request,
callback = partial(_cratesio_done, name, callback),
)
def _cratesio_done(name, callback, res):
data = json.loads(res.body.decode('utf-8'))
version = [v['num'] for v in data['versions'] if not v['yanked']][0]
callback(name, version)

8
tests/test_cratesio.py Normal file
View File

@ -0,0 +1,8 @@
from tests.helper import ExternalVersionTestCase
class CratesIOTest(ExternalVersionTestCase):
def test_npm(self):
self.assertEqual(
self.sync_get_version("example", {"cratesio": None}),
"0.1.0")