Пример #1
0
    def from_chart(cls, chart, downloaded=False):
        week = chart_calendarweek()
        artist = chart["artist"]
        title = chart["title"]
        image_url = chart["image"]["src"]
        position = chart["position"]
        last_position = chart["last_position"]

        return cls(
            artist=artist,
            title=title,
            image_url=image_url,
            position=position,
            last_position=last_position,
            week=week,
            downloaded=downloaded,
            path=cls.construct_path(week, artist, title),
        )
Пример #2
0
    def download_charts(self, category, username=None, password=None,
                        audio_only=False, notify=False):
        action = False
        session = self.Session()
        
        DB = {'hitlist' : HitlistSong,
              'dance' : DanceSong,
              'black' : BlackSong}[category]
        
        charts = list(get_charts(category))
        
        # TODO: 
        #  friday and no new charts list
        calendar_week = chart_calendarweek()
        self.log('Calendar week: {}'.format(calendar_week))
        query = session.query(DB).filter(DB.week == calendar_week)
        if not all(song == charts[song.position-1] for song in query):
            calendar_week += 1
            self.log('/{}.'.format(calendar_week))
        self.log('\n\n')
        
        path = os.path.join(self.music_dir, category, str(calendar_week))
        if not os.path.isdir(path):
            os.makedirs(path)
    
        for chart in charts:
            query = session.query(DB).filter(DB.week == calendar_week) \
                                     .filter(DB.position == chart['position'])
            try:
                song = query.one()
            except NoResultFound:
                song = DB.from_chart(chart)
                song.week = calendar_week
                song.rebuild_path()
                session.add(song)
            else:
                continue
        
        chart_queue = Queue()
        [chart_queue.put((song, 0)) for song in session.query(DB).filter(DB.downloaded == False)]
        
        while not chart_queue.empty():
            song, video_result = chart_queue.get()

            self.log('Downloading #{0} (Week {1}): {2!s}.\n'
                     .format(song.position, song.week, song))
            
            video = search_youtube(song)[video_result]
            video_url = video.get('href')
            
            try:
                video_id = parse_qs(urlparse(video_url).query)['v'][0]
            except KeyError:
                self.log('Unable to retrieve url from youtube: {}'.format(video_url))
                continue
            old = session.query(DB).filter(DB.video_id == video_id) \
                                   .filter(DB.week != song.week) \
                                   .first()

            if not old is None:
                suffix = '.mp3' if audio_only else '.flv'
                song_path = os.path.join(self.music_dir, song.path) + suffix
                old_path = os.path.join(self.music_dir, old.path) + suffix
                
                if os.path.exists(old_path):
                    self.log('File does already exist, creating hardlink.\n')
                    if not os.path.exists(song_path):
                        os.link(old_path, song_path)
                    song.video_id = video_id
                    song.downloaded = True
            else:
                try:
                    self.download(video_url, song,
                                  username=username, password=password,
                                  audio_only=audio_only)
                except CalledProcessError, e:
                    self.log(e.message)
                except DownloadError, e:
                    if e.is_gema_error:
                        chart_queue.put((song, video_result+1))
                else:
                    song.video_id = video_id
                    song.downloaded = True