def get_dms(service: str, artist_id: str): # pagination might be added at some point if we need it, but considering how few dms most artists end up having, we probably won't # base = request.args.to_dict() # base.pop('o', None) # base["service"] = service # base["artist_id"] = artist_id # offset = int(request.args.get('o') or 0) # query = request.args.get('q') # limit = limit_int(int(request.args.get('limit') or 25), 50) artist = get_artist(service, artist_id) if artist is None: return redirect(url_for('artists.list')) dms = get_artist_dms(service, artist_id) props = ArtistDMsProps( id=artist_id, service=service, session=session, artist=artist, display_data=make_artist_display_data(artist), dms=dms, ) response = make_response(render_template( 'artist/dms.html', props=props, ), 200) response.headers['Cache-Control'] = 's-maxage=60' return response
def get_dms(): base = request.args.to_dict() base.pop('o', None) offset = parse_int(request.args.get('o'), 0) # noqa F811 query = request.args.get('q') limit = limit_int(int(request.args.get('limit') or 25), 50) dms = None total_count = None if query is None: (dms, total_count) = get_dm_page(offset, limit) else: (dms, total_count) = do_dm_search(query, offset, limit) artists = [get_artist(dm.service, dm.user) for dm in dms] props = DMsProps( count=total_count, limit=limit, dms=dms, artists=artists ) response = make_response(render_template( 'all_dms.html', props=props, base=base, ), 200) response.headers['Cache-Control'] = 's-maxage=60' return response
def get(service: str, artist_id: str): # cursor = get_cursor() base = request.args.to_dict() base.pop('o', None) base["service"] = service base["artist_id"] = artist_id offset = parse_int(request.args.get('o'), 0) query = request.args.get('q') limit = limit_int(int(request.args.get('limit') or 25), 50) favorited = False account = load_account() if account is not None: favorited = is_artist_favorited(account['id'], service, artist_id) (posts, total_count) = ([], 0) if not query or len(query) < 3: (posts, total_count) = get_artist_post_page(artist_id, service, offset, limit) else: (posts, total_count) = do_artist_post_search(artist_id, service, query, offset, limit) artist = get_artist(service, artist_id) if artist is None: return redirect(url_for('artists.list')) display_data = make_artist_display_data(artist) dm_count = count_user_dms(service, artist_id) (result_previews, result_attachments, result_flagged, result_after_kitsune, result_is_image) = get_render_data_for_posts(posts) props = ArtistPageProps(id=artist_id, service=service, session=session, name=artist['name'], count=total_count, limit=limit, favorited=favorited, artist=artist, display_data=display_data, dm_count=dm_count) response = make_response( render_template('user.html', props=props, base=base, results=posts, result_previews=result_previews, result_attachments=result_attachments, result_flagged=result_flagged, result_after_kitsune=result_after_kitsune, result_is_image=result_is_image), 200) response.headers['Cache-Control'] = 's-maxage=60' return response
def create_account(username: str, password: str, favorites: Optional[List[Dict]] = None) -> bool: account_id = None password_hash = bcrypt.hashpw(get_base_password_hash(password), bcrypt.gensalt()).decode('utf-8') account_create_lock.acquire() try: if is_username_taken(username): return False scrub = Cleaner(tags=[]) cursor = get_cursor() query = """ INSERT INTO account (username, password_hash) VALUES (%s, %s) RETURNING id """ cursor.execute(query, ( scrub.clean(username), password_hash, )) account_id = cursor.fetchone()['id'] if (account_id == 1): cursor = get_cursor() query = """ UPDATE account SET role = 'administrator' WHERE id = 1 """ cursor.execute(query) finally: account_create_lock.release() if favorites is not None: for favorite in favorites: artist = get_artist(favorite['service'], favorite['artist_id']) if artist is None: continue add_favorite_artist(account_id, favorite['service'], favorite['artist_id']) return True
def get_favorite_artists(account_id, reload=False): redis = get_conn() key = 'favorite_artists:' + str(account_id) favorites = redis.get(key) if favorites is None or reload: cursor = get_cursor() query = "select id, service, artist_id from account_artist_favorite where account_id = %s" cursor.execute(query, (account_id, )) favorites = cursor.fetchall() redis.set(key, serialize_dict_list(favorites)) else: favorites = deserialize_dict_list(favorites) artists = [] for favorite in favorites: artist = get_artist(favorite['service'], favorite['artist_id']) last_updated = get_artist_last_updated(favorite['service'], favorite['artist_id']) if artist is not None: artist['faved_seq'] = favorite['id'] artist['updated'] = last_updated artists.append(artist) return artists
def get(service, artist_id, post_id): # cursor = get_cursor() props = { 'currentPage': 'posts', 'service': service if service else 'patreon' } post = get_post(post_id, artist_id, service) if post is None: response = redirect( url_for('artists.get', service=service, artist_id=artist_id)) return response comments = get_post_comments(post_id, service) favorited = False account = load_account() if account is not None: favorited = is_post_favorited(account['id'], service, artist_id, post_id) artist = get_artist(service, artist_id) previews = [] attachments = [] if len(post['file']): if re.search("\.(gif|jpe?g|jpe|png|webp)$", post['file']['path'], re.IGNORECASE): # noqa w605 previews.append({ 'type': 'thumbnail', 'name': post['file'].get('name'), 'path': post['file']['path'].replace('https://kemono.party', '') }) else: attachments.append({ 'path': post['file']['path'].replace('https://kemono.party', ''), 'name': post['file'].get('name') }) if len(post['embed']): previews.append({ 'type': 'embed', 'url': post['embed']['url'], 'subject': post['embed']['subject'], 'description': post['embed']['description'] }) for attachment in post['attachments']: if re.search("\.(gif|jpe?g|jpe|png|webp)$", attachment['path'], re.IGNORECASE): # noqa w605 previews.append({ 'type': 'thumbnail', 'name': attachment['name'], 'path': attachment['path'].replace('https://kemono.party', '') }) else: file_extension = PurePath(attachment['path']).suffix # filename without extension stem = PurePath(attachment['path']).stem attachments.append({ 'path': attachment['path'], 'name': attachment.get('name'), 'extension': file_extension, 'stem': stem }) scrub = Cleaner(tags=[ 'a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'strong', 'ul', 'img', 'br', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div', 'span', 'ul', 'ol', 'li' ], attributes={ 'a': ['href', 'title'], 'abbr': ['title'], 'acronym': ['title'], 'img': ['src'] }, strip=True) post['content'] = scrub.clean(post['content']) props['artist'] = artist props['flagged'] = is_post_flagged(service, artist_id, post_id) props['favorited'] = favorited props['after_kitsune'] = post['added'] > datetime.datetime( 2020, 12, 22, 0, 0, 0, 0) response = make_response( render_template( 'post.html', props=props, post=post, comments=comments, result_previews=previews, result_attachments=attachments, ), 200) response.headers['Cache-Control'] = 's-maxage=60' return response