-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
import { $fetch } from 'ofetch' | ||
import { parseString } from '@fast-csv/parse' | ||
import type { Provider, Sponsorship } from '../types' | ||
|
||
export const LiberapayProvider: Provider = { | ||
name: 'liberapay', | ||
fetchSponsors(config) { | ||
return fetchLiberapaySponsors(config.liberapay?.login) | ||
}, | ||
} | ||
|
||
interface LiberapayRow { | ||
pledge_date: string | ||
patron_id: string | ||
patron_username: string | ||
patron_public_name: string | ||
donation_currency: string | ||
weekly_amount: string | ||
patron_avatar_url: string | ||
} | ||
|
||
interface ExchangeRate { | ||
code: string | ||
alphaCode: string | ||
numericCode: string | ||
name: string | ||
rate: number | ||
date: string | ||
inverseRate: number | ||
} | ||
|
||
interface ExchangeRates { | ||
[key: string]: ExchangeRate | ||
} | ||
|
||
export async function fetchLiberapaySponsors(login?: string): Promise<Sponsorship[]> { | ||
if (!login) | ||
throw new Error('Liberapay login is required') | ||
|
||
// Fetch and parse public CSV data | ||
const csvUrl = `https://liberapay.com/${login}/patrons/public.csv` | ||
const csvResponse = await $fetch<string>(csvUrl) | ||
const rows: LiberapayRow[] = [] | ||
await new Promise((resolve) => { | ||
parseString(csvResponse, { | ||
headers: true, | ||
ignoreEmpty: true, | ||
trim: true, | ||
}) | ||
.on('data', (row) => rows.push(row)) | ||
.on('end', resolve) | ||
}) | ||
|
||
// Only fetch exchange rates if we have patrons with non-USD currencies | ||
const exchangeRates = rows.some(r => r.donation_currency !== 'USD') | ||
? await $fetch<ExchangeRates>('https://www.floatrates.com/daily/usd.json') | ||
: {} | ||
|
||
return rows.map(row => ({ | ||
sponsor: { | ||
type: 'User', | ||
login: row.patron_username, | ||
name: row.patron_public_name || row.patron_username, | ||
avatarUrl: row.patron_avatar_url, | ||
linkUrl: `https://liberapay.com/${row.patron_username}`, | ||
}, | ||
monthlyDollars: getMonthlyDollarAmount(parseFloat(row.weekly_amount), row.donation_currency, exchangeRates), | ||
privacyLevel: 'PUBLIC', | ||
createdAt: new Date(row.pledge_date).toISOString(), | ||
provider: 'liberapay', | ||
})) | ||
} | ||
|
||
function getMonthlyDollarAmount(weeklyAmount: number, currency: string, exchangeRates: ExchangeRates): number { | ||
const weeksPerMonth = 4.345 | ||
const monthlyAmount = weeklyAmount * weeksPerMonth | ||
|
||
if (currency === 'USD') | ||
return monthlyAmount | ||
|
||
// Optionally exchange to USD | ||
const currencyLower = currency.toLowerCase() | ||
const inverseRate = exchangeRates[currencyLower]?.inverseRate ?? 1 | ||
return monthlyAmount * inverseRate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters