Example #1
0
def album_rip(album_id):
	"""
	:param id: String (Album ID)
	"""
	meta = client.get_meta(type="album", id=int(album_id))
	# Check for multiple artists.
	if meta['list'][0]['album_info']['result']['multi_artist_yn'] == "Y":
		# Label V.A. if amount of artists > 2
		if meta['list'][0]['album_info']['result']['artist_disp_nm'].count(",") > 2:
			meta['list'][0]['album_info']['result']['artist_disp_nm'] = "Various Artists"
	album_directory_name = "{} - {} [{}]".format(meta['list'][0]['album_info']['result']['artist_disp_nm'],
	                                        meta['list'][0]['album_info']['result']['title'].strip(),
	                                        utils.get_date(meta['list'][0]['album_info']['result']['release_ymd']))
	# Check for availability.
	if meta['list'][0]['album_info']['result']['is_album_str_noright']:
		logger_bugs.warning('No streaming rights for {}.'.format(album_directory_name))
	else:
		logger_bugs.info("Album: {}.".format(album_directory_name))
		if config.prefs['artist_folders']:
			album_path = utils.sanitize(os.path.join(config.prefs['downloads_directory'],
			                          meta['list'][0]['album_info']['result']['artist_disp_nm'], album_directory_name))
		else:
			album_path = utils.sanitize(os.path.join(config.prefs['downloads_directory'], album_directory_name))
		utils.make_dir(album_path)
		cover_path = os.path.join(album_path, config.prefs['cover_name'])
		try:
			download_cover(meta['list'][0]['album_info']['result']['img_urls'], cover_path)
		# If we're unable to request from the url we'll set the cover_path to False
		except requests.exceptions.HTTPError:
			cover_path = False
		for track in meta['list'][0]['album_info']['result']['tracks']:
			# Check for availability.
			if not track['track_str_rights']:
				logger_bugs.warning('No streaming rights for #{} - {}.'.format(track['track_no'], track['track_title']))
			else:
				track_quality = utils.determine_quality(track=track)
				pre_path = os.path.join(album_path, "{}. .BugsPy".format(track['track_no']))
				post_path = utils.sanitize(os.path.join(album_path, "{}. {}.{}".format(track['track_no'], track['track_title'],
				                                                        track_quality)))
				if utils.exist_check(post_path):
					logger_bugs.info("{} already exists.".format(post_path))
				else:
					download_track(pre_path=pre_path, track_id=track['track_id'], track_title=track['track_title'],
					               track_number=track['track_no'])
					os.rename(pre_path, post_path)
					try:
						tag(album=meta, track=track, file_path=post_path, cover_path=cover_path)
					# TODO: Come back to this exception and implement a better solution on f_file.save() within tag()
					except error:
						logger_bugs.warning("_writeblock error, falling back to a smaller cover artwork.")
						config.prefs['cover_size'] = "500"
						cover_path = os.path.join(album_path, "fallback_cover.jpg")
						download_cover(meta['list'][0]['album_info']['result']['img_urls'], cover_path)
						tag(album=meta, track=track, file_path=post_path, cover_path=cover_path)
Example #2
0
    def _post(self, request):
        template_data = {'display': '', 'result': '', 'url': ''}

        try:
            template_data['display'] = self._post_display(request)
            template_data['result'] = sanitize(self._post_result(request))
        except Exception, e:
            raise ModuleError('Something went wrong during module update: ' +
                              e.message)
Example #3
0
    def _post(self, request):
        template_data = {
            'display': '',
            'result': '',
            'url': ''
        }

        try:
            template_data['display'] = self._post_display(request)
            template_data['result'] = sanitize(self._post_result(request))
        except Exception, e:
            raise ModuleError('Something went wrong during module update: ' + e.message)
Example #4
0
    def _get(self, request):
        template_data = {
            'module': '',
            'display': '',
            'result': '',
            'url': request.GET['url']
        }

        try:
            template_data['module'] = self._get_module(request)
            template_data['display'] = self._get_display(request)
            template_data['result'] = sanitize(self._get_result(request))
        except Exception, e:
            raise ModuleError('Something went wrong during module initialization: ' + e.message)
Example #5
0
def track_rip(track_id):
    meta = client.get_meta(type="track", id=int(track_id))
    track = meta['list'][0]['track_detail']['result']['track']
    logger_bugs.info("Artist: {} | Track: {}".format(track['artist_nm'],
                                                     track['track_title']))
    track_quality = utils.determine_quality(track=track)
    album_path = os.path.join(config.prefs['downloads_directory'])
    pre_path = os.path.join(album_path,
                            "{}. .BugsPy".format(track['track_no']))
    post_path = os.path.join(
        album_path,
        utils.sanitize("{}. {}.{}".format(track['track_no'],
                                          track['track_title'],
                                          track_quality)))
    if utils.exist_check(post_path):
        logger_bugs.info("{} already exists.".format(post_path))
    else:
        download_track(pre_path=pre_path,
                       track_id=track['track_id'],
                       track_title=track['track_title'],
                       track_number=track['track_no'])
        os.rename(pre_path, post_path)
        # Fetch the album meta of the track, this is required for proper tagging.
        meta = client.get_meta(type="album", id=int(track['album_id']))
        # Download album artwork
        cover_path = os.path.join(album_path, config.prefs['cover_name'])
        try:
            download_cover(meta['list'][0]['album_info']['result']['img_urls'],
                           cover_path)
        # If we're unable to request from the url we'll set the cover_path to False
        except requests.exceptions.HTTPError:
            cover_path = False
        try:
            tag(album=meta,
                track=track,
                file_path=post_path,
                cover_path=cover_path)
        # TODO: Come back to this exception and implement a better solution on f_file.save() within tag()
        except error:
            logger_bugs.warning(
                "_writeblock error, falling back to a smaller cover artwork.")
            config.prefs['cover_size'] = "500"
            cover_path = os.path.join(album_path, "fallback_cover.jpg")
            download_cover(meta['list'][0]['album_info']['result']['img_urls'],
                           cover_path)
            tag(album=meta,
                track=track,
                file_path=post_path,
                cover_path=cover_path)
Example #6
0
    def _get(self, request):
        template_data = {
            'module': '',
            'display': '',
            'result': '',
            'url': request.GET['url']
        }

        try:
            template_data['module'] = self._get_module(request)
            template_data['display'] = self._get_display(request)
            template_data['result'] = sanitize(self._get_result(request))
        except Exception, e:
            raise ModuleError(
                'Something went wrong during module initialization: ' +
                e.message)
Example #7
0
def load_resources_view(request):
    """ Load resources for a given list of modules

    :param request:
    :return:
    """
    if not request.method == 'GET':
        return HttpResponse({}, status=status.HTTP_400_BAD_REQUEST)

    if 'urlsToLoad' not in request.GET or 'urlsLoaded' not in request.GET:
        return HttpResponse({}, status=status.HTTP_403_FORBIDDEN)

    # URLs of the modules to load
    mod_urls_qs = sanitize(request.GET['urlsToLoad'])
    mod_urls = json.loads(mod_urls_qs)

    # URLs of the loaded modules
    mod_urls_loaded_qs = sanitize(request.GET['urlsLoaded'])
    mod_urls_loaded = json.loads(mod_urls_loaded_qs)

    # Request hack to get module resources
    request.GET = {'resources': True}

    # List of resources
    resources = {'scripts': [], 'styles': []}

    # Add all resources from requested modules
    for url in mod_urls:
        module_view = get_module_view(url)
        mod_resources = module_view(request).content

        mod_resources = sanitize(mod_resources)
        mod_resources = json.loads(mod_resources)

        # Append resource to the list
        for key in resources.keys():
            if mod_resources[key] is None:
                continue

            for resource in mod_resources[key]:
                if resource not in resources[key]:
                    resources[key].append(resource)

    # Remove possible dependencies form already loaded modules
    for url in mod_urls_loaded:
        module_view = get_module_view(url)
        mod_resources = module_view(request).content

        mod_resources = sanitize(mod_resources)
        mod_resources = json.loads(mod_resources)

        # Remove resources already loaded
        for key in resources.keys():
            if mod_resources[key] is None:
                continue

            for resource in mod_resources[key]:
                if resource in resources[key]:
                    i = resources[key].index(resource)
                    del resources[key][i]

    # Build response content
    response = {'scripts': "", 'styles': ""}

    # Aggregate scripts
    for script in resources['scripts']:
        if script.startswith('http://') or script.startswith('https://'):
            script_tag = '<script class="module" src="' + script + '"></script>'
        else:
            with open(script, 'r') as script_file:
                script_tag = '<script class="module">' + script_file.read(
                ) + '</script>'

        response['scripts'] += script_tag

    # Aggregate styles
    for style in resources['styles']:
        if style.startswith('http://') or style.startswith('https://'):
            script_tag = '<link class="module" rel="stylesheet" type="text/css" href="' + style + '"></link>'
        else:
            with open(style, 'r') as script_file:
                script_tag = '<style class="module">' + script_file.read(
                ) + '</style>'

        response['styles'] += script_tag

    # Send response
    return HttpResponse(json.dumps(response), status=status.HTTP_200_OK)
Example #8
0
def main():
    """
	Main function which will control the flow of our script when called.
	"""
    total = len(args.u)
    for n, url in enumerate(args.u, 1):
        logger_genie.info("Album {} of {}".format(n, total))
        album_id = utils.check_url(url)
        if not album_id:
            return
        meta = client.get_meta(album_id)
        album_fol = "{} - {}".format(
            parse.unquote(meta['DATA0']['DATA'][0]['ARTIST_NAME']),
            parse.unquote(meta['DATA0']['DATA'][0]['ALBUM_NAME']))
        if prefs['artist_folders']:
            album_fol_abs = os.path.join(
                os.path.dirname(__file__), prefs['download_directory'],
                parse.unquote(
                    utils.sanitize(meta['DATA0']['DATA'][0]['ARTIST_NAME'])),
                utils.sanitize(album_fol))
        else:
            album_fol_abs = os.path.join(os.path.dirname(__file__),
                                         prefs['download_directory'],
                                         utils.sanitize(album_fol))
        logger_genie.info("Album found: " + album_fol)
        utils.make_dir(album_fol_abs)
        cover_url = parse.unquote(
            meta['DATA0']['DATA'][0]['ALBUM_IMG_PATH_600'])
        download_cover(cover_url, album_fol_abs)
        f_meta = {
            "track_total": len(meta['DATA1']['DATA']),
            "album_artist":
            parse.unquote(meta['DATA0']['DATA'][0]['ARTIST_NAME']),
            "release_date": meta['DATA0']['DATA'][0]['ALBUM_RELEASE_DT'],
            "planning":
            parse.unquote(meta['DATA0']['DATA'][0]['ALBUM_PLANNER'])
        }
        f_meta['disc_total'] = meta['DATA1']['DATA'][f_meta['track_total'] -
                                                     1]['ALBUM_CD_NO']
        for track in meta['DATA1']['DATA']:
            try:
                s_meta = client.get_stream_meta(track['SONG_ID'], args.f)
            except HTTPError:
                logger_genie.warning("Could not get stream info for {}".format(
                    track['SONG_ID']))
                continue
            except exceptions.StreamMetadataError:
                continue
            cur = track['ROWNUM']
            track_title = parse.unquote(track['SONG_NAME'])
            f_meta['track_artist'] = parse.unquote(track['ARTIST_NAME'])
            ext = utils.get_ext(s_meta['FILE_EXT'])
            post_abs = os.path.join(
                album_fol_abs, "{}. {}.{}".format(cur.zfill(2),
                                                  utils.sanitize(track_title),
                                                  ext))
            if utils.exist_check(post_abs):
                continue
            if not utils.allowed_check(s_meta['STREAMING_LICENSE_YN']):
                continue
            pre_abs = os.path.join(album_fol_abs, cur + ".genie-dl")
            specs = utils.parse_specs(s_meta['FILE_EXT'], s_meta['FILE_BIT'])
            download_track(s_meta['STREAMING_MP3_URL'], track_title, pre_abs,
                           cur, f_meta['track_total'], specs)
            try:
                fix_tags(pre_abs, ext, f_meta)
                logger_genie.debug("Tags updated: {}".format(f_meta))
            except Exception as e:
                raise e
            try:
                os.rename(pre_abs, post_abs)
                logger_genie.debug("{} has been renamed".format(post_abs))
            except OSError:
                raise exceptions.TrackRenameError(
                    "Could not rename {}".format(pre_abs))
def load_resources_view(request):
    """ Load resources for a given list of modules

    :param request:
    :return:
    """
    if not request.method == 'GET':
        return HttpResponse({}, status=status.HTTP_400_BAD_REQUEST)

    if 'urlsToLoad' not in request.GET or 'urlsLoaded' not in request.GET:
        return HttpResponse({}, status=status.HTTP_403_FORBIDDEN)

    # URLs of the modules to load
    mod_urls_qs = sanitize(request.GET['urlsToLoad'])
    mod_urls = json.loads(mod_urls_qs)

    # URLs of the loaded modules
    mod_urls_loaded_qs = sanitize(request.GET['urlsLoaded'])
    mod_urls_loaded = json.loads(mod_urls_loaded_qs)

    # Request hack to get module resources
    request.GET = {
        'resources': True
    }

    # List of resources
    resources = {
        'scripts': [],
        'styles': []
    }

    # loaded_resources = {
    #     'scripts': [],
    #     'styles': []
    # }

    # Add all resources from requested modules
    for url in mod_urls:
        module_view = get_module_view(url)
        mod_resources = module_view(request).content

        mod_resources = sanitize(mod_resources)
        mod_resources = json.loads(mod_resources)

        # Append resource to the list
        for key in resources.keys():
            if mod_resources[key] is None:
                continue

            for resource in mod_resources[key]:
                if resource not in resources[key]:
                    resources[key].append(resource)

    # Remove possible dependencies form already loaded modules
    for url in mod_urls_loaded:
        module_view = get_module_view(url)
        mod_resources = module_view(request).content

        mod_resources = sanitize(mod_resources)
        mod_resources = json.loads(mod_resources)

        # Remove resources already loaded
        for key in resources.keys():
            if mod_resources[key] is None:
                continue

            for resource in mod_resources[key]:
                if resource in resources[key]:
                    i = resources[key].index(resource)
                    del resources[key][i]


    # Build response content
    response = {
        'scripts': "",
        'styles': ""
    }

    # Aggregate scripts
    for script in resources['scripts']:
        if script.startswith('http://') or script.startswith('https://'):
            script_tag = '<script src="' + script + '"></script>'
        else:
            with open(script, 'r') as script_file:
                script_tag = '<script>' + script_file.read() + '</script>'

        response['scripts'] += script_tag

    # Aggregate styles
    for style in resources['styles']:
        if style.startswith('http://') or style.startswith('https://'):
            script_tag = '<link rel="stylesheet" type="text/css" href="' + style + '"></link>'
        else:
            with open(style, 'r') as script_file:
                script_tag = '<style>' + script_file.read() + '</style>'

        response['styles'] += script_tag

    # Send response
    return HttpResponse(json.dumps(response), status=status.HTTP_200_OK)