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

feat: add Twitch support #329

Merged
merged 4 commits into from
Mar 30, 2024
Merged
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
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"@keyvhq/redis": "~2.1.0",
"@kikobeats/time-span": "~1.0.4",
"@microlink/mql": "~0.13.4",
"@microlink/ping-url": "~1.4.12",
"@microlink/ping-url": "~1.4.14",
"@microlink/ua": "~1.2.3",
"async-memoize-one": "~1.1.7",
"browserless": "~10.4.0",
Expand All @@ -107,7 +107,7 @@
"frequency-counter": "~1.0.1",
"got": "~11.8.6",
"helmet": "~7.1.0",
"html-get": "~2.16.1",
"html-get": "~2.16.3",
"http-compression": "~1.0.19",
"https-tls": "~1.0.16",
"ioredis": "~5.3.2",
Expand All @@ -121,16 +121,15 @@
"p-cancelable": "2.1.1",
"p-reflect": "~2.1.0",
"p-timeout": "~4.1.0",
"puppeteer": "~22.6.0",
"qsm": "~2.1.2",
"puppeteer": "~22.6.1",
"rate-limiter-flexible": "~5.0.0",
"router-http": "~1.0.7",
"send-http": "~1.0.6",
"serve-static": "~1.15.0",
"srcset": "~4.0.0",
"tangerine": "~1.5.4",
"top-crawler-agents": "~1.0.26",
"top-user-agents": "~2.1.12",
"top-user-agents": "~2.1.17",
"unique-random-array": "~2.0.0",
"url-regex": "~5.0.0"
},
Expand Down
8 changes: 8 additions & 0 deletions public/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ It resolves user avatar against **twitter.com**.

e.g., https://unavatar.io/twitter/kikobeats

### Twitch

Type: `username`

It resolves user avatar against **twitch.tv**.

e.g., https://unavatar.io/twitter/midudev

### YouTube

Type: `username`
Expand Down
20 changes: 5 additions & 15 deletions src/providers/deviantart.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@ const cheerio = require('cheerio')

const getHTML = require('../util/html-get')

const REGEX_PROFILE_URL =
/^https?:\/\/(?:www\.)?deviantart\.com\/([\w-]+)(?:\/.+)?$/

module.exports = PCancelable.fn(async function deviantart ({ input }, onCancel) {
module.exports = PCancelable.fn(async function deviantart (
{ input },
onCancel
) {
const promise = getHTML(`https://www.deviantart.com/${input}`)
onCancel(() => promise.onCancel())
const { html } = await promise
const $ = cheerio.load(html)
const canonUsername = $('head link[rel=canonical]')
.attr('href')
.replace(REGEX_PROFILE_URL, '$1')
const els = $('img.avatar,a[data-hook=user_link][data-icon]').filter(
(i, el) => {
const thisUsername =
$(el).attr('data-username') || $(el).attr('title') || ''
return canonUsername.toLowerCase() === thisUsername.toLowerCase()
}
)
return els.attr('data-icon') || els.attr('src')
return $('meta[property="og:image"]').attr('content')
})

module.exports.supported = {
Expand Down
11 changes: 1 addition & 10 deletions src/providers/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@

const PCancelable = require('p-cancelable')
const cheerio = require('cheerio')
const qsm = require('qsm')

const getHTML = require('../util/html-get')

const { AVATAR_SIZE } = require('../constant')

module.exports = PCancelable.fn(async function gitlab ({ input }, onCancel) {
const promise = getHTML(`https://gitlab.com/${input}`)
onCancel(() => promise.onCancel())
const { html } = await promise
const $ = cheerio.load(html)

let avatarUrl = $('.avatar-holder > a > img').attr('src')
if (avatarUrl) avatarUrl = `https://gitlab.com${avatarUrl}`

return qsm.exist(avatarUrl, 'width')
? qsm.add(avatarUrl, { width: AVATAR_SIZE })
: avatarUrl
return $('meta[property="og:image"]').attr('content')
})

module.exports.supported = {
Expand Down
3 changes: 2 additions & 1 deletion src/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const providers = {
soundcloud: require('./soundcloud'),
substack: require('./substack'),
telegram: require('./telegram'),
tiktok: require('./tiktok'),
// tiktok: require('./tiktok'),
twitch: require('./twitch'),
twitter: require('./twitter'),
youtube: require('./youtube')
}
Expand Down
10 changes: 5 additions & 5 deletions src/providers/soundcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const cheerio = require('cheerio')

const getHTML = require('../util/html-get')

module.exports = PCancelable.fn(async function soundcloud ({ input }, onCancel) {
module.exports = PCancelable.fn(async function soundcloud (
{ input },
onCancel
) {
const promise = getHTML(`https://soundcloud.com/${input}`)
onCancel(() => promise.onCancel())
const { html } = await promise
const $ = cheerio.load(html, { xmlMode: true })
const name = $(`a[itemprop=url][href="/${input}" i]`)
return $(`img[itemprop=image][alt="${name.text().trim()}’s avatar" i]`).attr(
'src'
)
return $('meta[property="og:image"]').attr('content')
})

module.exports.supported = {
Expand Down
20 changes: 20 additions & 0 deletions src/providers/twitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const PCancelable = require('p-cancelable')
const cheerio = require('cheerio')

const getHTML = require('../util/html-get')

module.exports = PCancelable.fn(async function instagram ({ input }, onCancel) {
const promise = getHTML(`https://www.twitch.tv/${input}`)
onCancel(() => promise.onCancel())
const { html } = await promise
const $ = cheerio.load(html)
return $('meta[property="og:image"]').attr('content')
})

module.exports.supported = {
email: false,
username: true,
domain: false
}
15 changes: 11 additions & 4 deletions test/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test('soundcloud', async t => {
t.true(body.url.includes('images.weserv.nl'))
})
//
;(isCI ? test.skip : test)('deviantart', async t => {
test('deviantart', async t => {
const serverUrl = await runServer(t)
const { body } = await got('deviantart/spyed?json', {
prefixUrl: serverUrl
Expand Down Expand Up @@ -94,8 +94,7 @@ test('telegram', async t => {
})
t.true(body.url.includes('images.weserv.nl'))
})

test('substack', async t => {
;(isCI ? test.skip : test)('substack', async t => {
const serverUrl = await runServer(t)
const { body } = await got('substack/bankless?json', {
prefixUrl: serverUrl
Expand All @@ -119,6 +118,14 @@ test('substack', async t => {
t.true(body.url.includes('images.weserv.nl'))
})

test('twitch', async t => {
const serverUrl = await runServer(t)
const { body } = await got('twitch/midudev?json', {
prefixUrl: serverUrl
})
t.true(body.url.includes('images.weserv.nl'))
})

test('microlink', async t => {
const serverUrl = await runServer(t)
const { body } = await got('microlink/teslahunt.io?json', {
Expand All @@ -135,7 +142,7 @@ test('readcv', async t => {
t.true(body.url.includes('images.weserv.nl'))
})
//
;(isCI ? test.skip : test)('tiktok', async t => {
test.skip('tiktok', async t => {
const serverUrl = await runServer(t)
const { body } = await got('tiktok/carlosazaustre?json', {
prefixUrl: serverUrl
Expand Down