-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add python package total downloads from [pepy] badge #9564
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Joi from 'joi' | ||
import { nonNegativeInteger } from '../validators.js' | ||
import { BaseJsonService, pathParams } from '../index.js' | ||
import { renderDownloadsBadge } from '../downloads.js' | ||
|
||
const schema = Joi.object({ | ||
total_downloads: nonNegativeInteger, | ||
}).required() | ||
|
||
const description = ` | ||
Python package total downloads from [Pepy](https://www.pepy.tech/). | ||
|
||
Download stats from pepy count package downloads from PyPI and known mirrors.` | ||
|
||
export default class PepyDownloads extends BaseJsonService { | ||
static category = 'downloads' | ||
|
||
static route = { | ||
base: 'pepy', | ||
pattern: 'dt/:packageName', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've only added total for now, but in principle we could make this |
||
} | ||
|
||
static openApi = { | ||
'/pepy/dt/{packageName}': { | ||
get: { | ||
summary: 'Pepy Total Downlods', | ||
description, | ||
parameters: pathParams({ | ||
name: 'packageName', | ||
example: 'django', | ||
}), | ||
}, | ||
}, | ||
} | ||
|
||
static _cacheLength = 21600 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pepy only updates once per day so we can put quite a long cache on this |
||
|
||
static defaultBadgeData = { label: 'downloads' } | ||
|
||
async fetch({ packageName }) { | ||
return this._requestJson({ | ||
url: `https://api.pepy.tech/api/v2/projects/${packageName}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in psincraian/pepy#573 we might need to supply an API key here in future but for now, we'll start off using anonymous access. |
||
schema, | ||
}) | ||
} | ||
|
||
async handle({ packageName }) { | ||
const data = await this.fetch({ packageName }) | ||
return renderDownloadsBadge({ | ||
downloads: data.total_downloads, | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createServiceTester } from '../tester.js' | ||
import { isMetric } from '../test-validators.js' | ||
export const t = await createServiceTester() | ||
|
||
t.create('downloads (valid)') | ||
.get('/dt/django.json') | ||
.expectBadge({ label: 'downloads', message: isMetric }) | ||
|
||
t.create('downloads (not found)') | ||
.get('/dt/not-a-package.json') | ||
.expectBadge({ label: 'downloads', message: 'not found' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted in #4319 (comment) I've decided to add this as "python package total downloads from pepy", not "PyPI total downloads" due to inclusion of downloads from known mirrors in these stats.