Forráskód Böngészése

Merge branch 'master' into windows_compatible

# Conflicts:
#	helper/sites/download.py
Deben Oldert 6 éve
szülő
commit
0cd73b1360

+ 0 - 0
helper/__init__.py


+ 1 - 1
console.py → helper/console.py

@@ -1,7 +1,7 @@
 import sys
 
 import colorama as color
-from settings import Settings
+from helper.settings import Settings
 
 OUTPUT = 0
 DBG_ERROR = 1

+ 12 - 0
helper/settings.py

@@ -0,0 +1,12 @@
+class Settings:
+    # Directory to permanently save the file (ENV: MD_SAVEDIR)
+    SaveDir = None
+
+    # Directory to temporary download the file (ENV: MD_TMP)
+    tmpDir = '/tmp'
+
+    # Minimal debug level (ENV: MD_LOGGING)
+    Debuglvl = None
+
+    # Minimal bitrate to auto download the file (ENV: MD_QUALITY)
+    MinQuality = 300

+ 0 - 0
helper/sites/__init__.py


+ 21 - 22
sites/helper/download.py → helper/sites/download.py

@@ -1,10 +1,9 @@
-import console
-import os
-import sites.helper.tagging as tagging
-from sites.helper.item import Empty
+from helper import console
+import helper.tagging as tagging
+from helper.sites.item import Empty
 from .request import RawRequest
 from pyquery import PyQuery
-from settings import Settings
+from helper.settings import Settings
 
 import math
 import urllib.parse
@@ -51,44 +50,44 @@ def download(item, type='GET', parameters=None, headers=None, cookies=None, stre
 
                 console.output('Bitrate: {0}kbps'.format(int(item.bitrate)))
 
-            ans = console.ask_input('Continue downloading? [y/n]')
+            if item.duration_seconds is not None and int(item.bitrate) < Settings.MinQuality:
+                ans = console.ask_input('Continue downloading? [y/n]')
 
-            if ans != 'y':
-                return
+                if ans != 'y':
+                    return
 
     savefileprogress(name, full_name, file, item)
 
 
 def savefileprogress(name, full_name, file, item):
-    os.path.abspath(full_name)
-
     console.output('Saving to: {0}'.format(full_name), level=console.DBG_INFO)
     with open(full_name, 'wb') as f:
         progress = 0
         if item.bytes is not None:
             bar = console.ProgressBar(total=item.bytes)
 
-            for chunk in file.iter_content(chunk_size=4096):
-                progress += len(chunk)
-                f.write(chunk)
-                bar.report_progress(progress=progress)
+            for chunk in file.iter_content(chunk_size=1024):
+                if chunk:
+                    progress += len(chunk)
+                    f.write(chunk)
+                    bar.report_progress(progress=progress)
+            bar.destroy()
 
         else:
-            bar = console.ProgressBar()
             f.write(file.content)
 
-        bar.destroy()
+    tags = tagging.search_tags(item)
 
-        tags = tagging.search_tags(item)
+    if tags is not None:
+        item.link_tag_item(tags)
 
-        if tags is not None:
-            item.link_tag_item(tags)
+        tagging.download_artwork(tags)
 
-            tagging.download_artwork(tags)
+        tagging.write_tags_to_file(full_name, item)
 
-            tagging.write_tags_to_file(f, item)
+        name = urllib.parse.unquote('{x.artist} - {x.title}'.format(x=tags))
 
-    full_save_name = os.path.abspath('{0}/{1}'.format(Settings.SaveDir, name))
+    full_save_name = '{0}/{1}'.format(Settings.SaveDir, name)
     os.rename(full_name, full_save_name)
     console.output('Moved to {0}'.format(full_save_name), level=console.DBG_INFO)
 

+ 1 - 1
sites/helper/item.py → helper/sites/item.py

@@ -1,4 +1,4 @@
-import console
+from helper import console
 import re
 
 

+ 0 - 0
sites/helper/query.py → helper/sites/query.py


+ 1 - 1
sites/helper/request.py → helper/sites/request.py

@@ -1,7 +1,7 @@
 import requests
 import urllib.parse
 
-import console
+from helper import console
 
 
 class Request:

+ 1 - 1
sites/helper/structure.py → helper/sites/structure.py

@@ -1,6 +1,6 @@
 from pyquery import PyQuery
 
-from sites.helper.item import Item, Empty
+from helper.sites.item import Item
 
 
 class Structure:

+ 7 - 7
sites/helper/tagging.py → helper/tagging.py

@@ -1,9 +1,9 @@
-import console
-from mutagen.id3 import ID3, ID3NoHeaderError
-from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC, APIC
+from helper import console
+from mutagen.id3 import ID3NoHeaderError
+from mutagen.id3 import ID3, TIT2, TALB, TPE1, COMM, TCON, APIC
 
-from sites.helper.request import RawRequest
-from sites.tags import available
+from helper.sites.request import RawRequest
+from tags import available
 
 
 def search_tags(item):
@@ -47,7 +47,7 @@ def search_tags(item):
 
 def write_tags_to_file(file, item):
     try:
-        mp3 = ID3(file.name)
+        mp3 = ID3(file)
     except ID3NoHeaderError:
         mp3 = ID3()
 
@@ -65,7 +65,7 @@ def write_tags_to_file(file, item):
                        desc='Cover',
                        data=tagitem.cover_image)
 
-    mp3.save(file.name)
+    mp3.save(file)
 
 
 def download_artwork(tag):

+ 0 - 0
helper/tags/__init__.py


+ 2 - 2
sites/tags/structure.py → helper/tags/structure.py

@@ -1,7 +1,7 @@
-from sites.helper.structure import Structure
+from helper.sites.structure import Structure
 from pyquery import PyQuery
 
-from sites.tags.tagitem import TagItem
+from helper.tags.tagitem import TagItem
 
 
 class TagStructure(Structure):

+ 0 - 0
sites/tags/tagitem.py → helper/tags/tagitem.py


+ 16 - 5
main.py

@@ -1,7 +1,7 @@
 import sites as sites
-import console as console
+from helper import console as console
 import os.path
-from settings import Settings
+from helper.settings import Settings
 
 
 def init():
@@ -18,6 +18,8 @@ def init():
 
     directory(os.getenv('MD_SAVEDIR', '~/Downloads'))
 
+    quality(os.getenv('MD_QUALITY', '300'))
+
 
 def main():
     init()
@@ -43,9 +45,9 @@ def main():
 def choose_site():
     global selected_site
     _index = console.option_picker('Available sites',
-                                          list(map(lambda x: x.url, sites.available)),
-                                          selected_site,
-                                          True)
+                                   list(map(lambda x: x.url, sites.available)),
+                                   selected_site,
+                                   True)
     if _index is not None:
         selected_site = _index
     return True
@@ -134,6 +136,14 @@ def directory(dir=None):
     return True
 
 
+def quality(quality=None):
+    if quality is None:
+        quality = console.ask_input('Enter minimal quality for auto download')
+    Settings.MinQuality = int(quality)
+    console.output('Minimal quality for auto downloading is set to {0}'.format(Settings.MinQuality), level=console.DBG_INFO)
+    return True
+
+
 def last(items=None):
     global search_cache
     if items is not None:
@@ -158,6 +168,7 @@ commands = [
     ('save', directory, 'Change the download directory'),
     ('search', search, 'Search the selected site'),
     ('sites', choose_site, 'Pick the site to search'),
+    ('quality', quality, 'Set the minimal quality to download without confirmation'),
     ('quit', quit, 'Quit the program')
 ]
 

+ 0 - 7
settings.py

@@ -1,7 +0,0 @@
-import console as console
-
-
-class Settings:
-    SaveDir = None
-    tmpDir = '/tmp'
-    Debuglvl = None

+ 2 - 1
sites/__init__.py

@@ -2,6 +2,7 @@ from .czne import Czne
 from .default import DefaultSite
 from .musicteam import MusicTeam
 from .lalamus import Lalamus
+from .uzimusic import Uzimusic
 
 
-available = [DefaultSite(), Czne(), MusicTeam(), Lalamus()]
+available = [DefaultSite(), Czne(), MusicTeam(), Lalamus(), Uzimusic()]

+ 1 - 1
sites/czne.py

@@ -1,5 +1,5 @@
 from sites.default import DefaultSite
-from sites.helper.query import Query
+from helper.sites.query import Query
 
 
 class Czne(DefaultSite):

+ 8 - 12
sites/default.py

@@ -1,10 +1,8 @@
-import console
-from sites.helper.request import Request
-from sites.helper.query import Query
-from sites.helper.structure import Structure
-from sites.helper.item import Empty
-
-import sites.helper.download as Download
+from helper.sites import download
+from helper.sites.request import Request
+from helper.sites.query import Query
+from helper.sites.structure import Structure
+from helper.sites.item import Empty
 
 
 class DefaultSite:
@@ -52,10 +50,8 @@ class DefaultSite:
         # item.original_url = item.site.format_url(item.original_url)
 
         if 'zippyshare' in item.original_url:
-            Download.zippyaudio(item)
+            download.zippyaudio(item)
         elif 'krakenfiles' in item.original_url:
-            Download.krakenfiles(item)
-        elif 'lalamus' in item.original_url:
-            Download.download(item)
+            download.krakenfiles(item)
         else:
-            console.output('Don\'t know what to do with link: {}.'.format(item.download_url), level=console.DBG_ERROR)
+            download.download(item)

+ 1 - 1
sites/lalamus.py

@@ -1,5 +1,5 @@
 from sites.default import DefaultSite
-from sites.helper.query import Query
+from helper.sites.query import Query
 
 
 class Lalamus(DefaultSite):

+ 1 - 1
sites/musicid.py

@@ -1,5 +1,5 @@
 from sites.default import DefaultSite
-from sites.helper.query import Query
+from helper.sites.query import Query
 import requests
 
 # TODO - Make compatible, add artist path

+ 1 - 1
sites/musicteam.py

@@ -1,5 +1,5 @@
 from sites.default import DefaultSite
-from sites.helper.query import Query
+from helper.sites.query import Query
 import requests
 
 

+ 29 - 0
sites/uzimusic.py

@@ -0,0 +1,29 @@
+from sites.default import DefaultSite
+from helper.sites.query import Query
+
+
+class Uzimusic(DefaultSite):
+    def __init__(self):
+        super().__init__()
+        self.url = 'https://uzimusic.ru'
+        self.query = Query(self, 'POST') \
+            .add_parameter('do', 'search') \
+            .add_parameter('subaction', 'search') \
+            .add_parameter('search_start', '0') \
+            .add_parameter('full_search', '0') \
+            .add_parameter('result_from', '1') \
+            .add_parameter('story', '{0}')
+        self.structure\
+            .set_container_path('div#dle-content') \
+            .set_item_path('div.songs-list-item') \
+            .set_title_path('div.song-name') \
+            .set_artist_path('div.song-artist') \
+            .set_duration_path('span.song-time') \
+            .set_url_path('span.song-play')
+        self.request\
+            .add_header('Referer', self.url + '/') \
+            .add_header('Origin', self.url) \
+            .add_header('Content-Type', 'application/x-www-form-urlencoded')
+
+    def format_url(self, field):
+        return self.url + (field.attr['data-audio'].format(title_to_eng=field.attr['data-audio-title'], HTTP_HOST=''))

+ 0 - 0
sites/tags/__init__.py → tags/__init__.py


+ 2 - 2
sites/tags/beatport.py → tags/beatport.py

@@ -1,5 +1,5 @@
-from sites.tags.default import DefaultTagSite
-from sites.helper.query import Query
+from tags.default import DefaultTagSite
+from helper.sites.query import Query
 from pyquery import PyQuery
 
 

+ 3 - 3
sites/tags/default.py → tags/default.py

@@ -1,6 +1,6 @@
-from sites.tags.structure import TagStructure
-from sites.helper.request import Request
-from sites.helper.query import Query
+from helper.tags.structure import TagStructure
+from helper.sites.request import Request
+from helper.sites.query import Query
 
 
 class DefaultTagSite: