def search_themes(request, input_text): template = loader.get_template('search/search_themes.html') themes = Theme.search(input_text) themes_info = [] for theme in themes: theme_info = {} theme_info['theme'] = theme theme_info['articles_title'] = map(lambda at: at.article.title, theme.articletheme_set.all()[:2]) themes_info.append(theme_info) context = RequestContext(request, { 'themes': themes_info, }) return HttpResponse(template.render(context))
def import_db(article_info): def normalize_array_str(info): return info if isinstance(info, (str, unicode)) else ''.join(info) def normalize_authors(authors): normalized_authors = [] for author in authors: author_info = {} if isinstance(author, (str, unicode, list)): author_info['author'] = normalize_array_str(author) elif isinstance(author, dict): for k, v in author.iteritems(): author_info[k] = normalize_array_str(v) if author_info: normalized_authors.append(author_info) return normalized_authors if article_info['abstract']: semantic_extractor = SemanticExtractor(article_info['abstract']) extracted_keywords_map = semantic_extractor.get_keywords() extracted_language_acr = semantic_extractor.get_language() else: extracted_keywords_map = {} extracted_language_acr = None article_title = normalize_array_str(article_info['title']) article_authors = normalize_authors(article_info['authors']) article = Article.add_article(article_title, article_authors, language_acr=extracted_language_acr) for reference in article_info.get('references', []): r_title = normalize_array_str(reference['title']) r_authors = normalize_authors(reference['authors']) Reference.add_reference(article, r_title, r_authors) for ck in article_info.get('keywords'): if isinstance(ck, dict): v = ck.get('keyword', []) keyword = normalize_array_str(v) Theme.add_themes(article, keyword, 'article') synonyms_list = ck.get('synonyms', []) for synonym in synonyms_list: Theme.add_theme_synonym(keyword, normalize_array_str(synonym)) elif isinstance(ck, (str, list)): keyword = normalize_array_str(ck) Theme.add_themes(article, keyword, 'article') for extraction_type, keyword_list in extracted_keywords_map.iteritems(): for keyword in keyword_list: Theme.add_themes(article, keyword, extraction_type) return article
def theme(request): if request.method == 'POST': if request.POST.get('movie'): name = request.POST['movie'] type = 'movie' elif request.POST.get('drama'): name = request.POST['drama'] type = 'drama' elif request.POST.get('musical'): name = request.POST['musical'] type = 'musical' Theme.objects.filter(type=type).delete() #1 parts = urlparse( 'https://www.music-flo.com/api/search/v2/search?searchType=ALBUM&sortType=ACCURACY×tamp=1597825544349') qs = dict(parse_qsl(parts.query)) qs['keyword'] = name + 'ost' parts = parts._replace(query=urlencode(qs)) new_url = urlunparse(parts) u = urlopen(new_url) data = u.read() j = json.loads(data) obj = j['data']['list'][0]['list'] if len(obj) == 1: album_id = obj[0]['id'] else: for i in range(len(obj)) : if obj[i]['representationArtist']['name'] == 'Various Artists': album_id = obj[i]['id'] urls = str('https://www.music-flo.com/api/meta/v1/album/' + str(album_id) + '/track?timestamp=1597824172509') u_d = urlopen(urls) data_d = u_d.read() j_d = json.loads(data_d) obj = j_d['data']['list'] for i in range(len(obj)): song = obj[i]['name'] if 'name' in obj[i].keys() else '노래 제목을 알 수 없습니다.' album = obj[i]['album']['title'] if ('album' in obj[i].keys()) & ('title' in obj[i]['album'].keys()) else '앨범명을 알 수 없습니다.' artist = obj[i]['representationArtist']['name'] if ('representationArtist' in obj[i].keys()) & ('name' in obj[i]['representationArtist'].keys()) else '아티스트를 알 수 없습니다.' id = obj[i]['id'] if 'id' in obj[i].keys() else '' urls = str('https://www.music-flo.com/api/meta/v1/track/' + str(id) + '?timestamp=1597824526912') u_d = urlopen(urls) data_d = u_d.read() j_d = json.loads(data_d) obj_d = j_d['data'] if 'data' in j_d.keys(): obj_d = j_d['data'] lyrics = obj_d['lyrics'] if 'lyrics' in obj_d.keys() else '가사정보가 없습니다.' try: release = datetime.datetime.strptime(obj_d['album']['releaseYmd'], '%Y%m%d').strftime('%Y.%m.%d') if ('album' in obj_d.keys()) & ('releaseYmd' in obj_d['album'].keys()) else '앨범 발매연도를 알 수 없습니다.' except: release = obj_d['album']['releaseYmd'] genre = obj_d['album']['genreStyle'] if ('album' in obj_d.keys()) & ('genreStyle' in obj_d['album'].keys()) else '장르정보가 없습니다.' else: lyrics = '가사정보가 없습니다.' release = '앨범 발매연도를 알 수 없습니다.' genre = '장르정보가 없습니다.' tmp = Theme(song=song, album=album, artist=artist, lyrics=lyrics, release=release, genre=genre, type=type) tmp.save() contents = Theme.objects.filter(type=type) return render(request, 'theme.html', {'contents': contents, 'keyword': name})