def update_or_create_from_melon(self, album_id): url = 'https://www.melon.com/album/detail.htm' params = { 'albumId': album_id, } response = requests.get(url, params) soup = BeautifulSoup(response.text, 'lxml') info = soup.select_one('div.section_info') entry = info.select_one('div.entry') src = info.select_one('div.thumb img').get('src') title = entry.select_one('div.info > .song_name').contents[2].strip() img_cover = re.search(r'(.*?)/melon/quality.*', src).group(1) # if re.findall('http.*?\.jpg', url_img_cover): # url_img_cover = re.findall('http.*?\.jpg', url_img_cover)[0] # else: # url_img_cover = "http://cdnimg.melon.co.kr/resource/image/web/default/noAlbum_500_160727.jpg" meta_dict = get_dict_from_dl(entry.select_one('div.meta dl')) # response = requests.get(url_img_cover) # binary_data = response.content # temp_file = BytesIO() # temp_file.write(binary_data) # # temp_file.seek(0) try: release_date = datetime.strptime(meta_dict['발매일'], '%Y.%m.%d') except ValueError: try: release_date = datetime.strptime(meta_dict['발매일'], '%Y.%m') except ValueError: try: release_date = datetime.strptime(meta_dict['발매일'], '%Y') except ValueError: release_date = None album, album_created = Album.objects.update_or_create( melon_id=album_id, defaults={ 'title': title, 'release_date': release_date, }) # file_name = Path(url_img_cover).name # file_name, temp_file = download(img_cover, album_id) temp_file = download(img_cover) file_name = '{album_id}.{ext}'.format( album_id=album_id, ext=get_buffer_ext(temp_file), ) album.img_cover.save(file_name, File(temp_file)) return album, album_created
def get_detail(self): url = 'https://www.melon.com/album/detail.htm' params = { 'albumId': self.album_id, } response = requests.get(url, params) soup = BeautifulSoup(response.text) info = soup.select_one('div.section_info') entry = info.select_one('div.entry') src = info.select_one('div.thumb img').get('src') title = entry.select_one('div.info > .song_name').contents[2].strip() url_img_cover = re.search(r'(.*?)/melon/quality.*', src).group(1) meta_dict = get_dict_from_dl(entry.select_one('div.meta dl')) self.title = title self.url_img_cover = url_img_cover self.meta_dict = meta_dict
def get_detail(self): url = 'https://www.melon.com/album/detail.htm' params = { 'albumId': self.album_id, } response = requests.get(url, params) source = response.text soup = BeautifulSoup(source, 'lxml') wrap_info = soup.select_one('div.wrap_info') entry = wrap_info.select_one('div.entry') title = entry.select_one( '.info > .song_name').strong.next_sibling.strip() meta = entry.select_one('.meta > dl.list') album_meta_dict = get_dict_from_dl(meta, first_text=True) release_date = album_meta_dict.get('발매일') img_url = wrap_info.select_one('div.thumb > a > img').get('src') self.title = title self.url_img_cover = img_url self.release_date = release_date