Skip to content

Commit

Permalink
Merge pull request #38 from sjdonado/36-tidal-integration
Browse files Browse the repository at this point in the history
36 tidal integration
  • Loading branch information
sjdonado authored Dec 8, 2024
2 parents 9fef2e7 + c5124b9 commit 3b66a5f
Show file tree
Hide file tree
Showing 27 changed files with 674 additions and 265 deletions.
7 changes: 5 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"sourceType": "module"
},
"plugins": [
"jsx"
"jsx",
"simple-import-sort"
],
"rules": {
"no-unused-vars": "off",
Expand All @@ -24,6 +25,8 @@
{
"varsIgnorePattern": "^Html$"
}
]
],
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error"
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Copy a link from your favorite streaming service, paste it into the search bar,
Adapters represent the streaming services supported by the Web App and the Raycast Extension. Each adapter allows the app to convert links from one platform to others. The table below shows which features are available for each one:

| Adapter | Inverted Search | Official API | Verified Links |
| ---------------- | --------------- | ----------------------- | -------------- |
| ---------------- | --------------- | ---------------------- | -------------- |
| Spotify | Yes | Yes | Yes |
| Tidal | Yes | Yes | Yes |
| YouTube Music | Yes | No | Yes |
| Apple Music | Yes | No | Yes |
| Deezer | Yes | Yes | Yes |
| SoundCloud | Yes | No | Yes |
| Tidal | No | No (coming soon) | No |

## Web App

Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
image: sjdonado/bit
environment:
APP_URL: http://localhost:4001
ADMIN_NAME: 'Admin'
ADMIN_API_KEY: 'E7gWaEu8JOIGKR/jTmOOgbmBCup2h48jmux2YvIzpxk='
volumes:
- sqlite_data:/app/sqlite
ports:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "idonthavespotify",
"version": "1.4.1",
"version": "1.5.0",
"scripts": {
"dev": "concurrently \"bun run build:dev\" \"bun run --watch www/bin.ts\"",
"build:dev": "vite build --mode=development --watch",
Expand Down Expand Up @@ -39,6 +39,7 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsx": "^0.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-tailwindcss": "^3.15.1",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
Expand Down
15 changes: 7 additions & 8 deletions src/adapters/apple-music.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { ENV } from '~/config/env';
import { MetadataType, Adapter } from '~/config/enum';
import { RESPONSE_COMPARE_MIN_SCORE } from '~/config/constants';

import { Adapter, MetadataType } from '~/config/enum';
import { ENV } from '~/config/env';
import { cacheSearchResultLink, getCachedSearchResultLink } from '~/services/cache';
import { SearchMetadata, SearchResultLink } from '~/services/search';
import { getResultWithBestScore } from '~/utils/compare';
import HttpClient from '~/utils/http-client';
import { logger } from '~/utils/logger';
import { getCheerioDoc } from '~/utils/scraper';
import { getResultWithBestScore } from '~/utils/compare';

import { SearchMetadata, SearchResultLink } from '~/services/search';
import { cacheSearchResultLink, getCachedSearchResultLink } from '~/services/cache';

export const APPLE_MUSIC_LINK_SELECTOR = 'a[href^="https://music.apple.com/"]';

Expand All @@ -25,7 +23,7 @@ export async function getAppleMusicLink(query: string, metadata: SearchMetadata)
const searchType = APPLE_MUSIC_SEARCH_TYPES[metadata.type];

if (!searchType) {
return;
return null;
}

// apple music does not support x-www-form-urlencoded encoding
Expand Down Expand Up @@ -60,5 +58,6 @@ export async function getAppleMusicLink(query: string, metadata: SearchMetadata)
return searchResultLink;
} catch (err) {
logger.error(`[Apple Music] (${url}) ${err} `);
return null;
}
}
45 changes: 29 additions & 16 deletions src/adapters/deezer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ENV } from '~/config/env';
import { MetadataType, Adapter } from '~/config/enum';
import { ADAPTERS_QUERY_LIMIT } from '~/config/constants';
import { compareTwoStrings } from 'string-similarity';

import { ADAPTERS_QUERY_LIMIT, RESPONSE_COMPARE_MIN_SCORE } from '~/config/constants';
import { Adapter, MetadataType } from '~/config/enum';
import { ENV } from '~/config/env';
import { cacheSearchResultLink, getCachedSearchResultLink } from '~/services/cache';
import { SearchMetadata, SearchResultLink } from '~/services/search';
import HttpClient from '~/utils/http-client';
import { logger } from '~/utils/logger';
import { responseMatchesQuery } from '~/utils/compare';

import { SearchMetadata, SearchResultLink } from '~/services/search';
import { cacheSearchResultLink, getCachedSearchResultLink } from '~/services/cache';

interface DeezerSearchResponse {
total: number;
Expand All @@ -33,7 +32,7 @@ export async function getDeezerLink(query: string, metadata: SearchMetadata) {
const searchType = DEEZER_SEARCH_TYPES[metadata.type];

if (!searchType) {
return;
return null;
}

const params = new URLSearchParams({
Expand All @@ -57,18 +56,32 @@ export async function getDeezerLink(query: string, metadata: SearchMetadata) {
throw new Error(`No results found: ${JSON.stringify(response)}`);
}

const [{ title, name, link }] = response.data;
let bestMatch: SearchResultLink | null = null;
let highestScore = 0;

for (const item of response.data) {
const title = item.title || item.name || '';
const score = compareTwoStrings(title.toLowerCase(), query.toLowerCase());

const searchResultLink = {
type: Adapter.Deezer,
url: link,
isVerified: responseMatchesQuery(title ?? name ?? '', query),
} as SearchResultLink;
if (score > highestScore) {
highestScore = score;
bestMatch = {
type: Adapter.Deezer,
url: item.link,
isVerified: score > RESPONSE_COMPARE_MIN_SCORE,
};
}
}

if (!bestMatch) {
throw new Error('No valid matches found.');
}

await cacheSearchResultLink(url, searchResultLink);
await cacheSearchResultLink(url, bestMatch);

return searchResultLink;
return bestMatch;
} catch (error) {
logger.error(`[Deezer] (${url}) ${error}`);
return null;
}
}
21 changes: 8 additions & 13 deletions src/adapters/sound-cloud.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { ENV } from '~/config/env';
import { MetadataType, Adapter } from '~/config/enum';
import { RESPONSE_COMPARE_MIN_SCORE } from '~/config/constants';

import { Adapter, MetadataType } from '~/config/enum';
import { ENV } from '~/config/env';
import { cacheSearchResultLink, getCachedSearchResultLink } from '~/services/cache';
import { SearchMetadata, SearchResultLink } from '~/services/search';
import { getResultWithBestScore } from '~/utils/compare';
import HttpClient from '~/utils/http-client';
import { logger } from '~/utils/logger';
import { getCheerioDoc } from '~/utils/scraper';
import { getResultWithBestScore } from '~/utils/compare';

import { SearchMetadata, SearchResultLink } from '~/services/search';
import { cacheSearchResultLink, getCachedSearchResultLink } from '~/services/cache';

export async function getSoundCloudLink(query: string, metadata: SearchMetadata) {
if (metadata.type === MetadataType.Show) {
return;
return null;
}

const params = new URLSearchParams({
Expand Down Expand Up @@ -40,20 +38,17 @@ export async function getSoundCloudLink(query: string, metadata: SearchMetadata)

const { href, score } = getResultWithBestScore(noscriptDoc, listElements, query);

if (score <= RESPONSE_COMPARE_MIN_SCORE) {
return;
}

const searchResultLink = {
type: Adapter.SoundCloud,
url: `${ENV.adapters.soundCloud.baseUrl}${href}`,
isVerified: true,
isVerified: score > RESPONSE_COMPARE_MIN_SCORE,
} as SearchResultLink;

await cacheSearchResultLink(url, searchResultLink);

return searchResultLink;
} catch (err) {
logger.error(`[SoundCloud] (${url}) ${err}`);
return null;
}
}
80 changes: 38 additions & 42 deletions src/adapters/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { ADAPTERS_QUERY_LIMIT } from '~/config/constants';
import { Adapter, MetadataType } from '~/config/enum';
import { ENV } from '~/config/env';

import {
ADAPTERS_QUERY_LIMIT,
SPOTIFY_LINK_DESKTOP_REGEX,
SPOTIFY_LINK_MOBILE_REGEX,
} from '~/config/constants';
import { MetadataType, Adapter } from '~/config/enum';

import HttpClient from '~/utils/http-client';
import { logger } from '~/utils/logger';
import { responseMatchesQuery } from '~/utils/compare';

import { SearchMetadata, SearchResultLink } from '~/services/search';
import {
cacheSearchResultLink,
cacheSpotifyAccessToken,
getCachedSearchResultLink,
getCachedSpotifyAccessToken,
} from '~/services/cache';
import { SearchMetadata, SearchResultLink } from '~/services/search';
import { getOrUpdateAccessToken } from '~/utils/access-token';
import { responseMatchesQuery } from '~/utils/compare';
import HttpClient from '~/utils/http-client';
import { logger } from '~/utils/logger';

interface SpotifyAuthResponse {
access_token: string;
Expand Down Expand Up @@ -52,7 +46,7 @@ export async function getSpotifyLink(query: string, metadata: SearchMetadata) {
const searchType = SPOTIFY_SEARCH_TYPES[metadata.type];

if (!searchType) {
return;
return null;
}

const params = new URLSearchParams({
Expand Down Expand Up @@ -96,37 +90,39 @@ export async function getSpotifyLink(query: string, metadata: SearchMetadata) {
return searchResultLink;
} catch (error) {
logger.error(`[Spotify] (${url}) ${error}`);
return null;
}
}

export async function getOrUpdateSpotifyAccessToken() {
const cache = await getCachedSpotifyAccessToken();

if (cache) {
return cache;
}

const data = new URLSearchParams({
grant_type: 'client_credentials',
});

const response = await HttpClient.post<SpotifyAuthResponse>(
ENV.adapters.spotify.authUrl,
data,
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic ' +
Buffer.from(
ENV.adapters.spotify.clientId + ':' + ENV.adapters.spotify.clientSecret
).toString('base64'),
},
}
return getOrUpdateAccessToken(
getCachedSpotifyAccessToken,
async () => {
const data = new URLSearchParams({
grant_type: 'client_credentials',
});

const response = await HttpClient.post<SpotifyAuthResponse>(
ENV.adapters.spotify.authUrl,
data,
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization:
'Basic ' +
Buffer.from(
ENV.adapters.spotify.clientId + ':' + ENV.adapters.spotify.clientSecret
).toString('base64'),
},
}
);

return {
accessToken: response.access_token,
expiresIn: response.expires_in,
};
},
cacheSpotifyAccessToken
);

await cacheSpotifyAccessToken(response.access_token, response.expires_in);

return response.access_token;
}
Loading

0 comments on commit 3b66a5f

Please sign in to comment.