Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add web server #130

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/mockingbird/src/on_message.rs
Original file line number Diff line number Diff line change
@@ -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::<UrlValidate>().unwrap();

let captures = regex.captures(&msg.content);

for url in captures {
// wait for #128 to be merged

println!("url: {}", url);
}
}
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 0 additions & 22 deletions sbin/cleanup-dl/bin/cleanup-downloads.pl

This file was deleted.

65 changes: 17 additions & 48 deletions sbin/cleanup-dl/cleanup-downloads.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion sbin/cleanup-dl/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{ perlPackages }:
with perlPackages;
buildPerlPackage {
pname = "coggie-cleanup-deemix";
pname = "coggie-cleanup";
version = "0.1.0";
outputs = ["out"];

Expand Down
100 changes: 100 additions & 0 deletions sbin/deemix-stream/deemix-min
Original file line number Diff line number Diff line change
@@ -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')