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

[greasyfork] Add Greasy Fork service badges #8080

Merged
merged 17 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
24 changes: 24 additions & 0 deletions services/greasyfork/greasyfork-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Joi from 'joi'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'

const schema = Joi.object({
daily_installs: nonNegativeInteger,
total_installs: nonNegativeInteger,
good_ratings: nonNegativeInteger,
ok_ratings: nonNegativeInteger,
bad_ratings: nonNegativeInteger,
version: Joi.string().required(),
license: Joi.string().allow(null).required(),
}).required()

export default class BaseGreasyForkService extends BaseJsonService {
static defaultBadgeData = { label: 'greasy fork' }

async fetch({ scriptId }) {
return this._requestJson({
schema,
url: `https://greasyfork.org/scripts/${scriptId}.json`,
})
}
}
58 changes: 58 additions & 0 deletions services/greasyfork/greasyfork-downloads.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { renderDownloadsBadge } from '../downloads.js'
import BaseGreasyForkService from './greasyfork-base.js'

class BaseGreasyForkInstalls extends BaseGreasyForkService {
static category = 'downloads'

static defaultBadgeData = { label: 'installs' }
}

class GreasyForkDailyInstalls extends BaseGreasyForkInstalls {
static route = { base: 'greasyfork/dd', pattern: ':scriptId' }

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: this.render({ downloads: 17 }),
},
]

static _cacheLength = 21600

static render({ downloads }) {
return renderDownloadsBadge({ downloads, interval: 'day' })
}

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
return this.constructor.render({
downloads: data.daily_installs,
})
}
}

class GreasyForkTotalInstalls extends BaseGreasyForkInstalls {
static route = { base: 'greasyfork/dt', pattern: ':scriptId' }

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: this.render({ downloads: 3420 }),
},
]

static _cacheLength = 21600
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved

static render({ downloads }) {
return renderDownloadsBadge({ downloads })
}

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
return this.constructor.render({ downloads: data.total_installs })
}
}

export { GreasyForkDailyInstalls, GreasyForkTotalInstalls }
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions services/greasyfork/greasyfork-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ServiceTester } from '../tester.js'
import { isMetric, isMetricOverTimePeriod } from '../test-validators.js'

export const t = new ServiceTester({
id: 'GreasyForkInstalls',
title: 'GreasyFork Installs',
pathPrefix: '/greasyfork',
})
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved

t.create('Daily Installs')
.get('/dd/407466.json')
.expectBadge({ label: 'installs', message: isMetricOverTimePeriod })

t.create('Daily Installs (not found)')
.get('/dd/000000.json')
.expectBadge({ label: 'installs', message: 'not found' })

t.create('Total Installs')
.get('/dt/407466.json')
.expectBadge({ label: 'installs', message: isMetric })

t.create('Total Installs (not found)')
.get('/dt/000000.json')
.expectBadge({ label: 'installs', message: 'not found' })
42 changes: 42 additions & 0 deletions services/greasyfork/greasyfork-license.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { renderLicenseBadge } from '../licenses.js'
import { InvalidResponse } from '../index.js'
import BaseGreasyForkService from './greasyfork-base.js'

class GreasyForkLicense extends BaseGreasyForkService {
static category = 'license'

static route = {
base: 'greasyfork/l',
pattern: ':scriptId',
}

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: this.render({ licenses: ['MIT'] }),
},
]

static defaultBadgeData = { label: 'license' }

static render({ licenses }) {
return renderLicenseBadge({ licenses })
}
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
if (data.license === null) {
throw new InvalidResponse({
prettyMessage: 'version not found',
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved
})
}
// remove suffix " License" from data.license
const license = data.license.replace(/ License$/, '')
return this.constructor.render({
licenses: [license],
})
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved
}
}

export { GreasyForkLicense }
16 changes: 16 additions & 0 deletions services/greasyfork/greasyfork-license.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ServiceTester } from '../tester.js'

export const t = new ServiceTester({
id: 'GreasyForkLicense',
title: 'Greasy Fork License',
pathPrefix: '/greasyfork',
})
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved

t.create('License (valid)').get('/l/407466.json').expectBadge({
label: 'license',
message: 'MIT',
})

t.create('License (not found)')
.get('/l/000000.json')
.expectBadge({ label: 'license', message: 'not found' })
132 changes: 132 additions & 0 deletions services/greasyfork/greasyfork-rating.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { floorCount as floorCountColor } from '../color-formatters.js'
DenverCoder1 marked this conversation as resolved.
Show resolved Hide resolved
import { metric } from '../text-formatters.js'
import BaseGreasyForkService from './greasyfork-base.js'

class BaseGreasyForkRating extends BaseGreasyForkService {
static category = 'rating'

static defaultBadgeData = { label: 'rating' }
}

class GreasyForkRatingCount extends BaseGreasyForkRating {
static route = {
base: 'greasyfork/rating-count',
pattern: ':scriptId',
}

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: this.render({ ratingCount: 22 }),
},
]

static render({ ratingCount }) {
return {
message: `${metric(ratingCount)} total`,
color: floorCountColor(ratingCount, 5, 50, 500),
}
}

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
return this.constructor.render({
ratingCount: data.good_ratings + data.ok_ratings + data.bad_ratings,
})
}
}

class GreasyForkGoodRatingCount extends BaseGreasyForkRating {
static route = {
base: 'greasyfork/good-rating-count',
pattern: ':scriptId',
}

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: this.render({ ratingCount: 17 }),
},
]

static render({ ratingCount }) {
return {
message: `${metric(ratingCount)} good`,
color: 'green',
}
}

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
return this.constructor.render({
ratingCount: data.good_ratings,
})
}
}

class GreasyForkOkRatingCount extends BaseGreasyForkRating {
static route = {
base: 'greasyfork/ok-rating-count',
pattern: ':scriptId',
}

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: this.render({ ratingCount: 2 }),
},
]

static render({ ratingCount }) {
return {
message: `${metric(ratingCount)} ok`,
color: 'yellow',
}
}

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
return this.constructor.render({
ratingCount: data.ok_ratings,
})
}
}

class GreasyForkBadRatingCount extends BaseGreasyForkRating {
static route = {
base: 'greasyfork/bad-rating-count',
pattern: ':scriptId',
}

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: this.render({ ratingCount: 3 }),
},
]

static render({ ratingCount }) {
return {
message: `${metric(ratingCount)} bad`,
color: 'red',
}
}

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
return this.constructor.render({
ratingCount: data.bad_ratings,
})
}
}

export {
GreasyForkRatingCount,
GreasyForkGoodRatingCount,
GreasyForkOkRatingCount,
GreasyForkBadRatingCount,
}
40 changes: 40 additions & 0 deletions services/greasyfork/greasyfork-rating.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Joi from 'joi'
import { ServiceTester } from '../tester.js'

export const t = new ServiceTester({
id: 'GreasyForkRating',
title: 'Greasy Fork Rating',
pathPrefix: '/greasyfork',
})

t.create('Rating Count')
.get('/rating-count/407466.json')
.expectBadge({
label: 'rating',
message: Joi.string().regex(/^\d+?\stotal$/),
})

t.create('Good Rating Count')
.get('/good-rating-count/407466.json')
.expectBadge({
label: 'rating',
message: Joi.string().regex(/^\d+?\sgood$/),
})

t.create('Ok Rating Count')
.get('/ok-rating-count/407466.json')
.expectBadge({
label: 'rating',
message: Joi.string().regex(/^\d+?\sok$/),
})

t.create('Bad Rating Count')
.get('/bad-rating-count/407466.json')
.expectBadge({
label: 'rating',
message: Joi.string().regex(/^\d+?\sbad$/),
})

t.create('Rating Count (not found)')
.get('/rating-count/000000.json')
.expectBadge({ label: 'rating', message: 'not found' })
20 changes: 20 additions & 0 deletions services/greasyfork/greasyfork-version.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { renderVersionBadge } from '../version.js'
import BaseGreasyForkService from './greasyfork-base.js'

export default class GreasyForkVersion extends BaseGreasyForkService {
static category = 'version'
static route = { base: 'greasyfork/v', pattern: ':scriptId' }

static examples = [
{
title: 'Greasy Fork',
namedParams: { scriptId: '407466' },
staticPreview: renderVersionBadge({ version: '3.9.3' }),
},
]

async handle({ scriptId }) {
const data = await this.fetch({ scriptId })
return renderVersionBadge({ version: data.version })
}
}
12 changes: 12 additions & 0 deletions services/greasyfork/greasyfork-version.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isVPlusDottedVersionAtLeastOne } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('Version').get('/407466.json').expectBadge({
label: 'greasy fork',
message: isVPlusDottedVersionAtLeastOne,
})

t.create('Version (not found)')
.get('/000000.json')
.expectBadge({ label: 'greasy fork', message: 'not found' })