Skip to content
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

[SourceForge] Add the old API back #9101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions services/sourceforge/sourceforge.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Joi from 'joi'
import dayjs from 'dayjs'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'

const schema = Joi.object({
total: nonNegativeInteger,
}).required()

const intervalMap = {
dd: {
startDate: endDate => endDate,
interval: 'day',
},
dw: {
// 6 days, since date range is inclusive,
startDate: endDate => dayjs(endDate).subtract(6, 'days'),
interval: 'week',
},
dm: {
startDate: endDate => dayjs(endDate).subtract(30, 'days'),
interval: 'month',
},
dt: {
startDate: () => dayjs(0),
},
}

export default class Sourceforge extends BaseJsonService {
static category = 'downloads'

static route = {
base: 'sourceforge',
pattern: ':interval(dt|dm|dw|dd)/:project/:folder*',
}

static examples = [
{
title: 'SourceForge Downloads',
pattern: ':interval(dt|dm|dw|dd)/:project',
namedParams: {
interval: 'dm',
project: 'sevenzip',
},
staticPreview: this.render({
downloads: 215990,
interval: 'dm',
}),
},
{
title: 'SourceForge Downloads (folder)',
pattern: ':interval(dt|dm|dw|dd)/:project/:folder',
namedParams: {
interval: 'dm',
project: 'arianne',
folder: 'stendhal',
},
staticPreview: this.render({
downloads: 550,
interval: 'dm',
}),
},
]

static defaultBadgeData = { label: 'sourceforge' }

static render({ downloads, interval }) {
return renderDownloadsBadge({
downloads,
labelOverride: 'downloads',
interval: intervalMap[interval].interval,
})
}

async fetch({ interval, project, folder }) {
const url = `https://sourceforge.net/projects/${project}/files/${
folder ? `${folder}/` : ''
}stats/json`
// get yesterday since today is incomplete
const endDate = dayjs().subtract(24, 'hours')
const startDate = intervalMap[interval].startDate(endDate)
const options = {
searchParams: {
start_date: startDate.format('YYYY-MM-DD'),
end_date: endDate.format('YYYY-MM-DD'),
},
}

return this._requestJson({
schema,
url,
options,
errorMessages: {
404: 'project not found',
},
})
}

async handle({ interval, project, folder }) {
const { total: downloads } = await this.fetch({ interval, project, folder })
return this.constructor.render({ interval, downloads })
}
}
40 changes: 40 additions & 0 deletions services/sourceforge/sourceforge.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { isMetric, isMetricOverTimePeriod } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('total downloads').get('/dt/sevenzip.json').expectBadge({
label: 'downloads',
message: isMetric,
})

t.create('total downloads (with subdirs)')
.get('/dt/smartmontools/smartmontools/7.1.json')
.expectBadge({
label: 'downloads',
message: isMetric,
})

t.create('monthly downloads').get('/dm/sevenzip.json').expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
})

t.create('weekly downloads').get('/dw/sevenzip.json').expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
})

t.create('daily downloads').get('/dd/sevenzip.json').expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
})

t.create('downloads folder').get('/dm/arianne/stendhal.json').expectBadge({
label: 'downloads',
message: isMetricOverTimePeriod,
})

t.create('invalid project').get('/dd/invalid.json').expectBadge({
label: 'sourceforge',
message: 'project not found',
})