mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at offline-detection 152 lines 3.9 kB view raw
1import {Platform} from 'react-native' 2 3export type EmbedPlayerParams = 4 | {type: 'youtube_video'; videoId: string; playerUri: string} 5 | {type: 'twitch_live'; channelId: string; playerUri: string} 6 | {type: 'spotify_album'; albumId: string; playerUri: string} 7 | { 8 type: 'spotify_playlist' 9 playlistId: string 10 playerUri: string 11 } 12 | {type: 'spotify_song'; songId: string; playerUri: string} 13 | {type: 'soundcloud_track'; user: string; track: string; playerUri: string} 14 | {type: 'soundcloud_set'; user: string; set: string; playerUri: string} 15 16export function parseEmbedPlayerFromUrl( 17 url: string, 18): EmbedPlayerParams | undefined { 19 let urlp 20 try { 21 urlp = new URL(url) 22 } catch (e) { 23 return undefined 24 } 25 26 // youtube 27 if (urlp.hostname === 'youtu.be') { 28 const videoId = urlp.pathname.split('/')[1] 29 if (videoId) { 30 return { 31 type: 'youtube_video', 32 videoId, 33 playerUri: `https://www.youtube.com/embed/${videoId}?autoplay=1`, 34 } 35 } 36 } 37 if (urlp.hostname === 'www.youtube.com' || urlp.hostname === 'youtube.com') { 38 const [_, page, shortVideoId] = urlp.pathname.split('/') 39 const videoId = 40 page === 'shorts' ? shortVideoId : (urlp.searchParams.get('v') as string) 41 42 if (videoId) { 43 return { 44 type: 'youtube_video', 45 videoId, 46 playerUri: `https://www.youtube.com/embed/${videoId}?autoplay=1`, 47 } 48 } 49 } 50 51 // twitch 52 if (urlp.hostname === 'twitch.tv' || urlp.hostname === 'www.twitch.tv') { 53 const parent = 54 Platform.OS === 'web' ? window.location.hostname : 'localhost' 55 56 const parts = urlp.pathname.split('/') 57 if (parts.length === 2 && parts[1]) { 58 return { 59 type: 'twitch_live', 60 channelId: parts[1], 61 playerUri: `https://player.twitch.tv/?volume=0.5&!muted&autoplay&channel=${parts[1]}&parent=${parent}`, 62 } 63 } 64 } 65 66 // spotify 67 if (urlp.hostname === 'open.spotify.com') { 68 const [_, type, id] = urlp.pathname.split('/') 69 if (type && id) { 70 if (type === 'playlist') { 71 return { 72 type: 'spotify_playlist', 73 playlistId: id, 74 playerUri: `https://open.spotify.com/embed/playlist/${id}`, 75 } 76 } 77 if (type === 'album') { 78 return { 79 type: 'spotify_album', 80 albumId: id, 81 playerUri: `https://open.spotify.com/embed/album/${id}`, 82 } 83 } 84 if (type === 'track') { 85 return { 86 type: 'spotify_song', 87 songId: id, 88 playerUri: `https://open.spotify.com/embed/track/${id}`, 89 } 90 } 91 } 92 } 93 94 // soundcloud 95 if ( 96 urlp.hostname === 'soundcloud.com' || 97 urlp.hostname === 'www.soundcloud.com' 98 ) { 99 const [_, user, trackOrSets, set] = urlp.pathname.split('/') 100 101 if (user && trackOrSets) { 102 if (trackOrSets === 'sets' && set) { 103 return { 104 type: 'soundcloud_set', 105 user, 106 set: set, 107 playerUri: `https://w.soundcloud.com/player/?url=${url}&auto_play=true&visual=false&hide_related=true`, 108 } 109 } 110 111 return { 112 type: 'soundcloud_track', 113 user, 114 track: trackOrSets, 115 playerUri: `https://w.soundcloud.com/player/?url=${url}&auto_play=true&visual=false&hide_related=true`, 116 } 117 } 118 } 119} 120 121export function getPlayerHeight({ 122 type, 123 width, 124 hasThumb, 125}: { 126 type: EmbedPlayerParams['type'] 127 width: number 128 hasThumb: boolean 129}) { 130 if (!hasThumb) return (width / 16) * 9 131 132 switch (type) { 133 case 'youtube_video': 134 case 'twitch_live': 135 return (width / 16) * 9 136 case 'spotify_album': 137 return 380 138 case 'spotify_playlist': 139 return 360 140 case 'spotify_song': 141 if (width <= 300) { 142 return 180 143 } 144 return 232 145 case 'soundcloud_track': 146 return 165 147 case 'soundcloud_set': 148 return 360 149 default: 150 return width 151 } 152}