2020-07-10 17:38:30 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import logging
|
|
|
|
import requests
|
|
|
|
import wx
|
|
|
|
import urllib.parse
|
|
|
|
import config
|
2020-07-12 20:19:20 -05:00
|
|
|
from requests.auth import HTTPBasicAuth
|
2020-07-10 17:38:30 -05:00
|
|
|
from update.utils import seconds_to_string
|
|
|
|
from .import base
|
|
|
|
|
|
|
|
log = logging.getLogger("services.vk")
|
|
|
|
|
2020-07-12 20:19:20 -05:00
|
|
|
### toDo: hide secrets
|
2020-07-12 18:21:46 -05:00
|
|
|
url = "https://musicdl.manuelcortez.net"
|
2020-07-10 17:38:30 -05:00
|
|
|
application_name = "music_dl"
|
|
|
|
access_token = "e2237f17af545a4ba0bf6cb0b1a662e6"
|
|
|
|
|
|
|
|
class interface(base.baseInterface):
|
2020-07-12 20:19:20 -05:00
|
|
|
""" Class downloader for VK audios. """
|
2020-07-10 17:38:30 -05:00
|
|
|
name = "vk"
|
2020-07-12 20:31:15 -05:00
|
|
|
if config.app != None: # Workaround for cx_freeze 6.2 in python 3.7.
|
|
|
|
enabled = config.app["services"]["vk"].get("enabled")
|
|
|
|
else:
|
|
|
|
enabled = False
|
2020-07-10 17:38:30 -05:00
|
|
|
|
|
|
|
#util functions.
|
|
|
|
def get_auth(self):
|
|
|
|
# Authentication object
|
|
|
|
self.auth=HTTPBasicAuth(application_name, access_token)
|
|
|
|
|
|
|
|
def get(self, endpoint, *args, **kwargs):
|
|
|
|
response = requests.get(url+endpoint, auth=self.auth, *args, **kwargs)
|
|
|
|
return response
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(interface, self).__init__()
|
|
|
|
self.get_auth()
|
|
|
|
|
|
|
|
def get_file_format(self):
|
2020-07-12 20:19:20 -05:00
|
|
|
# Only mp3 audio is supported in VK so return it without further checks.
|
2020-07-10 17:38:30 -05:00
|
|
|
return "mp3"
|
|
|
|
|
|
|
|
def transcoder_enabled(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def search(self, text):
|
|
|
|
if text == "" or text == None:
|
|
|
|
raise ValueError("Text must be passed and should not be blank.")
|
|
|
|
log.debug("Retrieving data from vk...")
|
|
|
|
self.results = []
|
|
|
|
results = self.get("/vk/search", params=dict(text=text, maxresults=config.app["services"]["vk"]["max_results"]))
|
|
|
|
if results.status_code != 200:
|
|
|
|
return
|
|
|
|
results = results.json()
|
|
|
|
for search_result in results:
|
|
|
|
s = base.song(self)
|
|
|
|
s.title = search_result["title"]
|
|
|
|
s.artist = search_result["artist"]
|
|
|
|
s.duration = seconds_to_string(search_result["duration"])
|
|
|
|
s.url = search_result["url"]
|
|
|
|
s.info = search_result
|
|
|
|
self.results.append(s)
|
|
|
|
|
|
|
|
def get_download_url(self, file_url):
|
2020-07-12 18:21:46 -05:00
|
|
|
return "{url}/vk/download/?url={url2}".format(url=url, url2=file_url)
|
2020-07-10 17:38:30 -05:00
|
|
|
|
|
|
|
def format_track(self, item):
|
|
|
|
return "{title}. {artist}. {duration}".format(title=item.title, duration=item.duration, artist=item.artist)
|
|
|
|
|
|
|
|
class settings(base.baseSettings):
|
|
|
|
name = _("VK")
|
|
|
|
config_section = "vk"
|
|
|
|
|
|
|
|
def __init__(self, parent):
|
|
|
|
super(settings, self).__init__(parent=parent)
|
|
|
|
sizer = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
self.enabled = wx.CheckBox(self, wx.NewId(), _("Enable this service"))
|
|
|
|
self.enabled.Bind(wx.EVT_CHECKBOX, self.on_enabled)
|
|
|
|
self.map.append(("enabled", self.enabled))
|
|
|
|
sizer.Add(self.enabled, 0, wx.ALL, 5)
|
|
|
|
max_results_label = wx.StaticText(self, wx.NewId(), _("Max results per page"))
|
|
|
|
self.max_results = wx.SpinCtrl(self, wx.NewId())
|
|
|
|
self.max_results.SetRange(1, 300)
|
|
|
|
max_results_sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
max_results_sizer.Add(max_results_label, 0, wx.ALL, 5)
|
|
|
|
max_results_sizer.Add(self.max_results, 0, wx.ALL, 5)
|
|
|
|
self.map.append(("max_results", self.max_results))
|
|
|
|
self.SetSizer(sizer)
|
|
|
|
|
|
|
|
def on_enabled(self, *args, **kwargs):
|
|
|
|
for i in self.map:
|
|
|
|
if i[1] != self.enabled:
|
|
|
|
if self.enabled.GetValue() == True:
|
|
|
|
i[1].Enable(True)
|
|
|
|
else:
|
|
|
|
i[1].Enable(False)
|