diff --git a/crates/mockingbird/src/on_message.rs b/crates/mockingbird/src/on_message.rs new file mode 100644 index 0000000..3b3ef4a --- /dev/null +++ b/crates/mockingbird/src/on_message.rs @@ -0,0 +1,21 @@ + +fn setup_client() { + let regex = Regex::new(r"^(http|https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,3}|www\.[^\s]+\.[^\s]{2,3})$").unwrap(); + + regex.compile() +} + + +// we have to use regex because discord now supports +// link masking as markdown syntax +async fn on_message(ctx: &Context, msg: &Message) { + let regex = ctx.data.read().await.get::().unwrap(); + + let captures = regex.captures(&msg.content); + + for url in captures { + // wait for #128 to be merged + + println!("url: {}", url); + } +} \ No newline at end of file diff --git a/flake.nix b/flake.nix index e83da2c..289b45f 100644 --- a/flake.nix +++ b/flake.nix @@ -70,8 +70,8 @@ rec { packages.check-cache = cictl.check; packages.deemix-stream = deemix-stream; - packages.coggiebot-softcleanup = pkgs.callPackage ./sbin/cleanup-dl { - perlPackages = pkgs.perl534Packages; + packages.coggiebot-cleanup = pkgs.callPackage ./sbin/cleanup-dl { + # perlPackages = pkgs.perlPackages; }; packages.coggiebot-deploy = vanilla-linux; diff --git a/sbin/cleanup-dl/bin/cleanup-downloads.pl b/sbin/cleanup-dl/bin/cleanup-downloads.pl deleted file mode 100644 index 014267f..0000000 --- a/sbin/cleanup-dl/bin/cleanup-downloads.pl +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env perl -package Coggiebot::cleanupDL; -use strict; -use warnings; -use Set::Object qw(set); - -our $VERSION = 0.1; - -my $inuse=`lsof +D $1 | tr -s ' ' | cut -d ' ' -f 9-`; -my @inuse_arr=split("\n", $inuse); -my $inuse_set = Set::Object->new(); -$inuse_set->insert(@inuse_arr); - -my $old=`find $1 -ctime +30min`; -my @old_arr=split("\n", $old); -my $old_set=Set::Object->new(); -$old_set->insert(@old_arr); - -my $remove=$old_set - $inuse_set; -print(@remove); - - diff --git a/sbin/cleanup-dl/cleanup-downloads.pl b/sbin/cleanup-dl/cleanup-downloads.pl index 2f35039..6de24d7 100644 --- a/sbin/cleanup-dl/cleanup-downloads.pl +++ b/sbin/cleanup-dl/cleanup-downloads.pl @@ -4,73 +4,42 @@ package Coggiebot::cleanupDL; use strict; use warnings; -use Fcntl ':flock'; # import LOCK_* constants use Set::Object qw(set); our $VERSION = 0.1; - -my $CACHE = $ENV('COG_MCACHE') or die "COG_MCACHE not set"; -my $CACHE_LOOKUP = $ENV('COG_MLOOKUP') or die "COG_MLOOKUP not set"; -my $CACHE_EXPIRE = $ENV('COG_MEXPIRE') or die "COG_MEXPIRE not set"; -my $QUEUED = $ENV('COG_MQUEUE') or die "COG_MQUEUE not set"; +my $EXPIRE = $ENV('DEEMIX_CTIME_EXPIRE') or die "DEEMIX_CTIME_EXPIRE not set"; my $EXCEPTIONS = Set::Object->new(); +my $REMOVE = Set::Object->new(); -sub except_queued() { - # files queued for playing - open(my $fh, 'r', "$QUEUE" or die $!; - flock($fh, LOCK_EX) or die "Cannot lock playlist queue - $!\n"; - - # read the file and - # convert symlinks to filepath - while (<$fh>) { - chomp; - $EXCEPTION->insert(readlink $_); - } - close($fh) -} - -sub except_opened() { +sub except_opened { + my $dir = $_[0] or die "No directory specified"; # Find files still in use - my $inuse=`lsof +D $CACHE | sed -n '1d;p' | tr -s ' ' | cut -d ' ' -f 9- | sort -u`; + my $inuse=`lsof +D $dir | sed -n '1d;p' | tr -s ' ' | cut -d ' ' -f 9- | sort -u`; my @inuse_arr=split("\n", $inuse); - $EXCEPTION->insert(@inuse_arr); } -sub cleanup() { - # Find all files older than 20 minutes - my $old=`find $CACHE -type f -cmin +$EXPIRE`; - my @old_arr=split("\n", $old); - $old_set->insert(@old_arr); +sub cleanup { + my $dir = $_[0] or die "No directory specified"; + + my $rmraw=`find $dir -type f -cmin +$EXPIRE`; + my @rmlist=split("\n", $rmraw); + $REMOVE->insert(@rmlist); # Exclude files that are still in use from being deleted - my $remove = $old_set - $EXCEPTION; - for my $file ($remove->members()) { + my $removals = $REMOVE - $EXCEPTION; + for my $file ($removals->members()) { if (-f $file) { print "deleting: $file\n"; unlink $file; } } - - for my $file `ls $CACHE_LOOKUP` { - if (! -e readlink $file) { - print "deleting lookup: $file\n"; - unlink $file; - } - } -} - -sub init() { - `mkdir -p $CACHE $CACHE_LOOKUP`; - if (! -e $QUEUED) { - `touch $QUEUED`; - } } sub main() { - init(); - except_queued(); - except_opened(); - cleanup(); + my $dir = $ARGV[0] or die "No directory specified"; + + except_opened($dir); + cleanup($dir); } diff --git a/sbin/cleanup-dl/default.nix b/sbin/cleanup-dl/default.nix index ed113bf..6df06a7 100644 --- a/sbin/cleanup-dl/default.nix +++ b/sbin/cleanup-dl/default.nix @@ -2,7 +2,7 @@ { perlPackages }: with perlPackages; buildPerlPackage { - pname = "coggie-cleanup-deemix"; + pname = "coggie-cleanup"; version = "0.1.0"; outputs = ["out"]; diff --git a/sbin/deemix-stream/deemix-min b/sbin/deemix-stream/deemix-min new file mode 100755 index 0000000..eda3808 --- /dev/null +++ b/sbin/deemix-stream/deemix-min @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +from deemix_stream.__init__ import SpotifyStreamer + +import click +import requests +import sys +import json + +from pathlib import Path +from requests import get +from deezer import Deezer +from deezer import TrackFormats +from deemix.types.Track import Track +from deemix import generateDownloadObject, parseLink +from deemix.settings import DEFAULTS as DEFAULT_SETTINGS, load as loadSettings +from deemix.downloader import getPreferredBitrate, formatsName, streamTrack +from deemix.errors import DownloadFailed, MD5NotFound, \ + DownloadCanceled, PreferredBitrateNotFound, \ + TrackNot360, AlbumDoesntExists, \ + DownloadError, DownloadEmpty + +from deezer.errors import WrongLicense, WrongGeolocation + +from deemix.utils import USER_AGENT_HEADER +from deemix.utils.crypto import generateBlowfishKey, decryptChunk + + +from deemix.utils import getBitrateNumberFromText, formatListener +import deemix.utils.localpaths as localpaths +from deemix.downloader import Downloader +from deemix.itemgen import GenerationError + + +try: + from deemix.plugins.spotify import Spotify +except ImportError: + Spotify = None + +class SpotifyMin(Spotify): + def __init__(self, configFolder, clientID, clientSecret): + super().__init__(configFolder) + self.credentials = {'clientId': clientID, 'clientSecret': clientSecret} + +def downloadLinks(dz, url, plugins=None, settings=None): + downloadObjects = [] + bitrate = settings.get("maxBitrate", TrackFormats.MP3_320) + + try: + downloadObject = generateDownloadObject( + dz, url, bitrate, plugins=plugins + ) + except GenerationError as e: + print(f"{e.link}: {e.message}") + return + + if isinstance(downloadObject, list): + downloadObjects += downloadObject + else: + downloadObjects.append(downloadObject) + + for obj in downloadObjects: + if obj.__type__ == "Convertable": + obj = plugins[obj.plugin].convert(dz, obj, settings) + Downloader(dz, obj, settings).start() + +@click.command() +@click.option('-p', '--path', type=str, default=None, help='ARL token to use') +@click.option('-a', '--arl', type=str, default=None, help='ARL token to use') +@click.option('-s', '--spt-id', type=str, help='Path to the config folder') +@click.option('-ss', '--spt-secret', type=str, help='Path to the config folder') +@click.option('-sc', '--spt-cache', type=str, help='Path to the config folder') +@click.option('-f', '--flac', is_flag=True, help='Path to the config folder') +@click.argument('url', nargs=-1, required=True) +def download(url, path, arl, spt_id, spt_secret, spt_cache, flac): + dz = Deezer() + assert arl, 'You must provide an ARL token' + assert dz.login_via_arl(arl.strip()), 'Invalid ARL' + + settings = DEFAULT_SETTINGS + + if flac: settings["maxBitrate"] = str(TrackFormats.FLAC) + + if path is not None: + if path == '': path = '.' + path = Path(path) + settings['downloadLocation'] = str(path) + + plugins = { + "spotify": SpotifyStreamer(spt_id, spt_secret, spt_cache) + } + plugins["spotify"].setup() + + + print(url, url.__class__) + downloadLinks(dz, url[0], plugins, settings) + + click.echo("All done!") + +if __name__ == '__main__': + download(auto_envvar_prefix='DEEMIX') \ No newline at end of file