def create_test_track(id=None, duration=None, tags=(), attachments=(), lyrics=None, source_filename=None): def _get_tag(tag): return get_tag(tag, create_if_missing=True) def _get_attachment(attachment): if hasattr(attachment, 'location') and hasattr(attachment, 'type'): return create_attachment(attachment) else: return DBSession.query(Attachment).filter( Attachment.location.like('%%{0}%%'.format(attachment))).one() track = Track() track.id = id if id else random_string(10) track.duration = duration if duration else random.randint(60, 360) for tag in tags: track.tags.append(_get_tag(tag)) for attachment in attachments: track.attachments.append(_get_attachment(attachment)) track.lyrics = lyrics or '' track.source_filename = source_filename DBSession.add(track) return track
def gen_id_for_track(track): """ TODO: This method is a hecky peice of crap ... I feel dirty just looking at it. The hash generation is particulaly horrible. I would suggest some tidying """ id = '' # Gen 3 letter ID from category,from,title def get_chars(s, num=2): try: return re.search(r'(\w{%s})'% num, s.replace(' ','')).group(1) except: return '' # sprinkle on a little bit of 'title' hashy goodness in the hope that we wont get colisions exclude_tags = ('status:', '#') identifyer_string = "-".join(filter(lambda tag: not any((exclude_tag in tag for exclude_tag in exclude_tags )) ,sorted(tag.full for tag in track.tags))) try: title_hash = re.search(r'([1-9][abcdef123456789])', hashlib.sha1(identifyer_string.encode('utf-8')).hexdigest().lower()).group(1) #hex(hash( except Exception: title_hash = '' _get_chars = lambda parent, num: get_chars(track.get_tag(parent), num) id = "".join(( _get_chars('category', 1), _get_chars('from', 2) or _get_chars('artist', 2) , _get_chars('title', 2), title_hash, )) # If the tags were not present; then split the title string and get the first 3 characters if not id: log.error('unable to aquire id from tags - uing random id') id = random_string(length=5) # Normaize case id = id.lower() # Check for colistions and make unique number def get_id_number(id): count = 0 def get_count(): return str(count) if count else '' while DBSession.query(Track).filter_by(id=id+get_count()).count(): count += 1 log.warn('track.id colision - this is unexpected and could lead to inconsistencys with track naming for printed lists in the future {0}, press c to continue'.format(id)) import pdb ; pdb.set_trace() return get_count() id += get_id_number(id) return id
def create_test_track(id=None, duration=None, tags=[], attachments=[], lyrics=None, source_filename=None): def _get_tag(tag): return get_tag(tag, create_if_missing=True) def _get_attachment(filename): return DBSession.query(Attachment).filter(Attachment.location.like('%%{0}%%'.format(filename))).one() track = Track() track.id = id if id else random_string(10) track.duration = duration if duration else random.randint(60,360) [track.tags .append(_get_tag (t)) for t in tags ] [track.attachments.append(_get_attachment(a)) for a in attachments] if lyrics: track.lyrics.append(lyrics) track.source_filename = source_filename return track
def inject_testdata(request): """ When demoing the system to new people it was often an annoyance to manually add multiple tracks to show the priority token system. This is a nice way to quickly add test tracks. """ message = '' if (request.params.get('cmd') == 'add_queue_item'): (random_track_id, ) = random.choice(DBSession.query(Track.id).all()) random_performer_name = random.choice(('Bob','Jane','Sally','Brutus','Rasputin','Lisa','Ryu','Ken','Alec Baldwin','Ghengis Kahn','Kraken','Lucy')) queue_track_request = Request.blank('/queue.json', POST={ 'track_id': random_track_id, 'performer_name': random_performer_name, 'session_owner': random_string(), }) response_json = request.invoke_subrequest(queue_track_request).json message = response_json['messages'].pop() return action_ok(message=message)
def inject_testdata(request): """ When demoing the system to new people it was often an annoyance to manually add multiple tracks to show the priority token system. This is a nice way to quickly add test tracks. """ message = '' if (request.params.get('cmd') == 'add_queue_item'): (random_track_id, ) = random.choice(DBSession.query(Track.id).all()) random_performer_name = random.choice(('Bob', 'Jane', 'Sally', 'Brutus', 'Rasputin', 'Lisa', 'Ryu', 'Ken', 'Alec Baldwin', 'Ghengis Kahn', 'Kraken', 'Lucy')) queue_track_request = Request.blank('/queue.json', POST={ 'track_id': random_track_id, 'performer_name': random_performer_name, 'session_owner': random_string(), }) response_json = request.invoke_subrequest(queue_track_request).json message = response_json['messages'].pop() return action_ok(message=message)
def create_test_track(id=None, duration=None, tags=(), attachments=(), lyrics=None, source_filename=None): def _get_tag(tag): return get_tag(tag, create_if_missing=True) def _get_attachment(attachment): if hasattr(attachment, 'location') and hasattr(attachment, 'type'): return create_attachment(attachment) else: return DBSession.query(Attachment).filter(Attachment.location.like('%%{0}%%'.format(attachment))).one() track = Track() track.id = id if id else random_string(10) track.duration = duration if duration else random.randint(60, 360) for tag in tags: track.tags.append(_get_tag(tag)) for attachment in attachments: track.attachments.append(_get_attachment(attachment)) track.lyrics = lyrics or '' track.source_filename = source_filename DBSession.add(track) return track
def get_random_description(words=None, word_size=None): if not words: words = random.randint(4,24) if not word_size: word_size = random.randint(2,16) return " ".join([random_string(word_size) for word in range(words)])
def random_tracks(request, DBSession, commit, num_tracks=800): log.info('Generating {0} random tracks'.format(num_tracks)) # Attachment generation ---------------------------------------------------- attachments_meta = [ ('preview1.3gp' , 'preview' ), ('preview2.flv' , 'preview' ), ('image1.jpg' , 'thumbnail'), ('image2.jpg' , 'thumbnail'), ('image3.png' , 'thumbnail'), ('processed.mpg', 'video' ), ('subtitles.ssa', 'subtitle' ), ] attachments = [] for location, type in attachments_meta: attachment = Attachment() attachment.location = location attachment.type = type DBSession.add(attachment) attachments.append(attachment) commit() # Random Tag generation ---------------------------------------------------- parent_tag = get_tag('from') for series_num in range(10): DBSession.add(Tag('Series %s'%random_string(1), parent_tag)) commit() # -------------------------------------------------------------------------- tags = DBSession.query(Tag).options(joinedload(Tag.parent)).all() alias_parent_tag = aliased(Tag) tags_category = DBSession.query(Tag).join(alias_parent_tag, Tag.parent).filter(alias_parent_tag.name=='category').all() tags_from = DBSession.query(Tag).join(alias_parent_tag, Tag.parent).filter(alias_parent_tag.name=='from').all() def get_random_tags(num_tags=None): random_tags = [] if not num_tags: num_tags = random.randint(1,5) for tag_num in range(num_tags): random_tags.append(tags[random.randint(0,len(tags)-1)]) # if we have 'from' tags and NO category tag, then add a random category from_tags = [t for t in random_tags if t.parent and t.parent.name=='from'] category_tags = [t for t in random_tags if t.parent and t.parent.name=='category'] if from_tags and not category_tags: random_tags.append(random.choice(tags_category)) if category_tags and not from_tags: random_tags.append(random.choice(tags_from)) # only have 1 'from' tag for tag in from_tags[1:]: random_tags.remove(tag) # only have one category tag for tag in category_tags[1:]: random_tags.remove(tag) return random_tags def get_random_description(words=None, word_size=None): if not words: words = random.randint(4,24) if not word_size: word_size = random.randint(2,16) return " ".join([random_string(word_size) for word in range(words)]) # Track generation --------------------------------------------------------- tag_titles = [get_tag('Test Track {0}'.format(i), 'title', create_if_missing=True) for i in range(num_tracks)] #commit() for track_number in range(num_tracks): track = Track() track.id = "track_%d" % track_number #track.description = get_random_description() track.duration = random.randint(60,360) track.tags = list(set(get_random_tags())) + [tag_titles[track_number]] track.attachments = attachments DBSession.add(track) commit()
def get_random_description(words=None, word_size=None): if not words: words = random.randint(4, 24) if not word_size: word_size = random.randint(2, 16) return " ".join([random_string(word_size) for word in range(words)])
def random_tracks(request, DBSession, commit, num_tracks=800): """ Generate 100's of random tracks """ log.info('Generating {0} random tracks'.format(num_tracks)) # Attachment generation ---------------------------------------------------- attachments_meta = [ ('preview1.3gp', 'preview'), ('preview2.flv', 'preview'), ('image1.jpg', 'image'), ('image2.jpg', 'image'), ('image3.png', 'image'), ('processed.mpg', 'video'), ('subtitles.srt', 'srt'), ] attachments = [] for location, type in attachments_meta: attachment = Attachment() attachment.location = location attachment.type = type DBSession.add(attachment) attachments.append(attachment) commit() # Random Tag generation ---------------------------------------------------- parent_tag = get_tag('from') for series_num in range(10): DBSession.add(Tag('Series %s' % random_string(1), parent_tag)) commit() # -------------------------------------------------------------------------- tags = DBSession.query(Tag).options(joinedload(Tag.parent)).all() alias_parent_tag = aliased(Tag) tags_category = DBSession.query(Tag).join( alias_parent_tag, Tag.parent).filter(alias_parent_tag.name == 'category').all() tags_from = DBSession.query(Tag).join( alias_parent_tag, Tag.parent).filter(alias_parent_tag.name == 'from').all() def get_random_tags(num_tags=None): random_tags = [] if not num_tags: num_tags = random.randint(1, 5) for tag_num in range(num_tags): random_tags.append(tags[random.randint(0, len(tags) - 1)]) # if we have 'from' tags and NO category tag, then add a random category from_tags = [ t for t in random_tags if t.parent and t.parent.name == 'from' ] category_tags = [ t for t in random_tags if t.parent and t.parent.name == 'category' ] if from_tags and not category_tags: random_tags.append(random.choice(tags_category)) if category_tags and not from_tags: random_tags.append(random.choice(tags_from)) # only have 1 'from' tag for tag in from_tags[1:]: random_tags.remove(tag) # only have one category tag for tag in category_tags[1:]: random_tags.remove(tag) return random_tags def get_random_description(words=None, word_size=None): if not words: words = random.randint(4, 24) if not word_size: word_size = random.randint(2, 16) return " ".join([random_string(word_size) for word in range(words)]) # Track generation --------------------------------------------------------- tag_titles = [ get_tag('Test Track {0}'.format(i), 'title', create_if_missing=True) for i in range(num_tracks) ] #commit() for track_number in range(num_tracks): track = Track() track.id = "track_%d" % track_number #track.description = get_random_description() track.duration = random.randint(60, 360) track.tags = list(set(get_random_tags())) + [tag_titles[track_number]] track.attachments = attachments DBSession.add(track) commit()