Skip to content

Commit

Permalink
feat: Add Bandcamp
Browse files Browse the repository at this point in the history
Implements #72
  • Loading branch information
octfx committed Oct 30, 2023
1 parent 3819ba9 commit 13c6ad4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ As of version 3.x, EmbedVideo supports embedding video content from the followin
| Site | Service Name(s) | ID Example | URL Example(s) |
|----------------------------------------------------------|---------------------------------|--------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| [Archive.org Videos](https://archive.org/details/movies) | `archiveorg` | electricsheep-flock-244-80000-6 | https://archive.org/details/electricsheep-flock-244-80000-6<br/>https://archive.org/embed/electricsheep-flock-244-80000-6 |
| [Bandcamp](https://bandcamp.com/) | `bandcamp` | 1003592798 | https://emptyhousesmusic.bandcamp.com/album/empty-houses (Click on share/embed, and copy the id after `album=`) |
| [Bilibili](https://www.bilibili.com/) | `bilibili` | BV1Hz4y1k7ae | https://player.bilibili.com/player.html?bvid=1Hz4y1k7ae&amp;page=1 |
| [DailyMotion](https://dailymotion.com/) | `dailymotion` | x1adiiw_archer-waking-up-as-h-jon-benjamin_shortfilms | http://www.dailymotion.com/video/x1adiiw_archer-waking-up-as-h-jon-benjamin_shortfilms |
| [KakaoTV](https://tv.kakao.com/) | `kakaotv` | 301157950 | https://play-tv.kakao.com/embed/player/cliplink/301157950 |
Expand Down
4 changes: 4 additions & 0 deletions includes/EmbedService/AbstractEmbedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ abstract public function getBaseUrl(): string;
* @return float|null
*/
public function getAspectRatio(): ?float {
if ( $this->width !== null && $this->height !== null ) {
return $this->width / $this->height;
}

return $this->getDefaultWidth() / $this->getDefaultHeight();
}

Expand Down
89 changes: 89 additions & 0 deletions includes/EmbedService/Bandcamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare( strict_types=1 );

namespace MediaWiki\Extension\EmbedVideo\EmbedService;

final class Bandcamp extends AbstractEmbedService {

/**
* @inheritDoc
*/
public function getDefaultHeight(): int {
return 200;
}

/**
* @inheritDoc
*/
public function getDefaultWidth(): int {
return 400;
}

/**
* @inheritDoc
*/
public function getBaseUrl(): string {
if ( empty( $this->urlArgs ) ) {
// phpcs:ignore Generic.Files.LineLength.TooLong
return '//bandcamp.com/EmbeddedPlayer/album=%1$s/size=large/bgcol=181a1b/linkcol=056cc4/artwork=small/transparent=true/';
}

return '//bandcamp.com/EmbeddedPlayer/album=%1$s/';
}

/**
* @inheritDoc
*/
public function getUrl(): string {
if ( $this->getUrlArgs() !== false ) {
unset( $this->urlArgs['autoplay'] );

$args = array_shift( $this->urlArgs );
return sprintf( '%s&%s', sprintf( $this->getBaseUrl(), $this->getId() ), $args ?? '' );
}

return parent::getUrl();
}

/**
* @inheritDoc
*/
protected function getUrlRegex(): array {
return [
'#bandcamp\.com\/EmbeddedPlayer\/album=(\d+)(?:[\/\w=]+)?#is',
];
}

/**
* @inheritDoc
*/
protected function getIdRegex(): array {
return [
'#(?:album=)?(\d+)(?:[\/\w=]+)?#is',
];
}

/**
* @inheritDoc
*/
public function getPrivacyPolicyUrl(): ?string {
return 'https://bandcamp.com/privacy';
}

/**
* @inheritDoc
*/
public function getCSPUrls(): array {
return [
'https://bandcamp.com',
];
}

/**
* @inheritDoc
*/
public function getContentType(): ?string {
return 'audio';
}
}
4 changes: 4 additions & 0 deletions includes/EmbedService/EmbedServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class EmbedServiceFactory {
*/
private static $availableServices = [
ArchiveOrg::class,
Bandcamp::class,
Bilibili::class,
Ccc::class,
DailyMotion::class,
Expand Down Expand Up @@ -61,6 +62,9 @@ public static function newFromName( string $serviceName, string $id ): AbstractE
case 'archive.org':
return new ArchiveOrg( $id );

case 'bandcamp':
return new Bandcamp( $id );

case 'bilibili':
case 'player.bilibili':
return new Bilibili( $id );
Expand Down

0 comments on commit 13c6ad4

Please sign in to comment.