<commit_before>#!/lounge/bin/janosh -f

local util = require("util")

local api_keys = {
  soundcloud = "2dcf4e12afd6ed5119741f888260fc85"
}


local HelpersClass = {} -- the table representing the class, which will double as the metatable for the instances
HelpersClass.__index = HelpersClass -- failed table lookups on the instances should fallback to the class table, to get methods

function HelpersClass.new()
  return setmetatable({}, HelpersClass)
end

function HelpersClass.resolve(self, url, category) 
  if category == "youtube" then
    if string.gmatch(url, "&v=") then
      url = url:gsub("&list=[a-zA-Z0-9]+", "")
    end
    print("#### URL:", url)
    return self:youtube_dl(url)
  else
    return self:youtube_dl_noformat(url)
  end
end

function HelpersClass.youtube_dl(self, url)
--    p,i,o,e = Janosh:psystem("/usr/bin/youtube-dl -f 22+bestaudio/18+bestaudio/137+bestaudio/136+bestaudio/135+bestaudio/134+bestaudio/133+bestaudio/160+bestaudio/http-380+bestaudio/http-240+bestaudio/http-144+bestaudio/hls-380-3+bestaudio/hls-380-2+bestaudio/hls-380-1+bestaudio/hls-380-0+bestaudio/hls-240-3+bestaudio/hls-240-2+bestaudio/hls-240-1+bestaudio/hls-240-0+bestaudio/hls-144-3+bestaudio/hls-144-2+bestaudio/hls-144-1+bestaudio/hls-144-0+bestaudio --encoding utf-8 -g -e '" .. url .. "'")
    p,i,o,e = Janosh:psystem("/usr/bin/youtube-dl -f 'best[ext=mp4]/best' -r 1 --encoding utf-8 -g -e '" .. url .. "'")
    items={}
    title=""
    url=""
    while true do
      title = Janosh:preadLine(o)
      url = Janosh:preadLine(o)
      if title == nil or url == nil then break end
      items[title]=url
    end
    print("YOUTUBE_DL", title, url) 
    Janosh:pclose(i)
    Janosh:pclose(o)
    Janosh:pclose(e)
    Janosh:pwait(p)
    return items
end

function HelpersClass.youtube_dl_noformat(self, url)
    p,i,o,e = Janosh:psystem("/usr/bin/youtube-dl --encoding utf-8 -g -r 1 -e '" .. url .. "'")
    items={}
    title=""
    url=""
    while true do
      title = Janosh:preadLine(o)
      url = Janosh:preadLine(o)
      if title == nil or url == nil then break end
      items[title]=url
    end
    print("YOUTUBE_DL", title, url)
    Janosh:pclose(i)
    Janosh:pclose(o)
    Janosh:pclose(e)
    Janosh:pwait(p)
    return items
end

function HelpersClass.soundcloud(self, url)
   local json = JSON:decode(
      util:http_get('http://api.soundcloud.com/resolve.json?' ..
                  'client_id=' .. api_keys.soundcloud .. '&url=' .. url))
   return json['stream_url'], json.user.username .. ' - ' .. json.title
end

function HelpersClass.jamendo(self, url)<commit_msg>use local youtube-dl<commit_after>#!/lounge/bin/janosh -f

local util = require("util")

local api_keys = {
  soundcloud = "2dcf4e12afd6ed5119741f888260fc85"
}


local HelpersClass = {} -- the table representing the class, which will double as the metatable for the instances
HelpersClass.__index = HelpersClass -- failed table lookups on the instances should fallback to the class table, to get methods

function HelpersClass.new()
  return setmetatable({}, HelpersClass)
end

function HelpersClass.resolve(self, url, category) 
  if category == "youtube" then
    if string.gmatch(url, "&v=") then
      url = url:gsub("&list=[a-zA-Z0-9]+", "")
    end
    print("#### URL:", url)
    return self:youtube_dl(url)
  else
    return self:youtube_dl_noformat(url)
  end
end

function HelpersClass.youtube_dl(self, url)
--    p,i,o,e = Janosh:psystem("/usr/bin/youtube-dl -f 22+bestaudio/18+bestaudio/137+bestaudio/136+bestaudio/135+bestaudio/134+bestaudio/133+bestaudio/160+bestaudio/http-380+bestaudio/http-240+bestaudio/http-144+bestaudio/hls-380-3+bestaudio/hls-380-2+bestaudio/hls-380-1+bestaudio/hls-380-0+bestaudio/hls-240-3+bestaudio/hls-240-2+bestaudio/hls-240-1+bestaudio/hls-240-0+bestaudio/hls-144-3+bestaudio/hls-144-2+bestaudio/hls-144-1+bestaudio/hls-144-0+bestaudio --encoding utf-8 -g -e '" .. url .. "'")
    p,i,o,e = Janosh:psystem("/usr/local/bin/youtube-dl -f 'best[ext=mp4]/best' -r 1 --encoding utf-8 -g -e '" .. url .. "'")
    items={}
    title=""
    url=""
    while true do
      title = Janosh:preadLine(o)
      url = Janosh:preadLine(o)
      if title == nil or url == nil then break end
      items[title]=url
    end
    print("YOUTUBE_DL", title, url) 
    Janosh:pclose(i)
    Janosh:pclose(o)
    Janosh:pclose(e)
    Janosh:pwait(p)
    return items
end

function HelpersClass.youtube_dl_noformat(self, url)
    p,i,o,e = Janosh:psystem("/usr/local/bin/youtube-dl --encoding utf-8 -g -r 1 -e '" .. url .. "'")
    items={}
    title=""
    url=""
    while true do
      title = Janosh:preadLine(o)
      url = Janosh:preadLine(o)
      if title == nil or url == nil then break end
      items[title]=url
    end
    print("YOUTUBE_DL", title, url)
    Janosh:pclose(i)
    Janosh:pclose(o)
    Janosh:pclose(e)
    Janosh:pwait(p)
    return items
end

function HelpersClass.soundcloud(self, url)
   local json = JSON:decode(
      util:http_get('http://api.soundcloud.com/resolve.json?' ..
                  'client_id=' .. api_keys.soundcloud .. '&url=' .. url))
   return json['stream_url'], json.user.username .. ' - ' .. json.title
end

function HelpersClass.jamendo(self, url)
{'fraction_non_alphanumeric': 0.15341888527102088, 'fraction_numerical': 0.04098831641447998, 'mean_word_length': 3.3516666666666666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3798718', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1915, 'n_words': 379}