def dump_commands_to_file(commands, filename): filelist = [] for element in commands: cmd = ('[command]\nname=%s\nchannel=%s\nbody=%s\n' % (element, ','.join(commands[element]['chan']), commands[element]['body'])) filelist.append(cmd) try: filehandle.put_list(filename, filelist) except IOError: print('Error writing commands file.')
def manson_game(nick): try: mansonList = filehandle.get_list('text/manson') except IOError: raise IOError("Could not open file for manson_game") nickExists = False mansonRand = random.randint(1,10) # True/False, whether or not Mansons will be added or removed mansonAddDec = random.randint(0,1) for entry in mansonList: currentNick = entry.split(':')[0] if nick == currentNick: try: nickPosition = mansonList.index(entry) except: raise IndexError("Error getting index from manson list") nickExists = True if mansonAddDec == 0: mansonCount = 0 - mansonRand else: mansonCount = mansonRand if nickExists: value = mansonList[nickPosition].split(':')[1] value = int(value) mansonCount = value + mansonCount mansonList[nickPosition] = "%s:%d" % (nick, mansonCount) else: try: mansonList.append("%s:%d" % (nick, mansonCount)) except: raise IndexError("Error appending new manson entry") mansonList = sorted(mansonList, key=str.lower) try: filehandle.put_list('text/manson', mansonList) except IOError: raise IOError("Error writing manson list") if mansonAddDec == 0: return ("%s lost %d Mansons. Your total number of Mansons is now %d" % (nick, mansonRand, mansonCount)) elif mansonAddDec == 1: return ("%s gained %d Mansons. Your total number of Mansons is now %d" % (nick, mansonRand, mansonCount))
def last_played(irc, args): total_search = True opts = get_long_opts(args) song = txtfunctions.acronym_replace(' '.join(opts['other'])) if ((opts['range'] != None and opts['range'] != [-1,-1]) or opts['country'] != None or opts['tour'] != None): total_search = False if song.lower() in [s.lower() for s in filehandle.get_list('text/setlist')]: gig = filehandle.get_list('text/gig') if date_in_range(gig, opts['range']): irc.msg ('%s was last played at: %s' % (song, gig[0])) return if total_search: database = filehandle.get_list('text/archive.db') for entry in database: if entry.startswith('lp!'): db_song = entry.split('!')[1].split('::')[0] if song.lower() == db_song: db_file = entry.split('!')[1].split('::')[1] setlist = filehandle.get_list(db_file) irc.msg('%s was last played at: %s' % (song, setlist[0])) return tours = sorted(os.listdir('GigArchive')) for tour in reversed(tours): if opts['tour'] is not None: if opts['tour'] != tour_name(tour): continue gigs = sorted(os.listdir('GigArchive/%s' % tour)) for filename in reversed(gigs): if (not date_in_range(filename, opts['range']) or (opts['country'] is not None and opts['country'] != country_code(filename))): continue gig_file = 'GigArchive/%s/%s' % (tour, filename) setlist = filehandle.get_list(gig_file) if song.lower() in [s.lower() for s in setlist]: irc.msg('%s was last played at: %s' % (song, setlist[0])) if total_search: if len(database) >= 100: database.pop(0) database.append('lp!%s::%s' % (song.lower(), gig_file)) filehandle.put_list('text/archive.db', database) return irc.msg('I do not seem to have information on that song for the given options.')
def delete_song(argument): compared = False deleteCount = 0 songs = argument.split(', ') songCount = len(songs) print('Deleting %d songs...' % (songCount)) try: setlist = filehandle.get_list('text/setlist') except IOError: raise IOError("Error getting set for delete_song") for song in songs: compared = False song = acronym_replace(song) sys.stdout.write('Comparing song: %s' % (song)) try: songIndex = setlist.index(song) compared = True except: None if compared: try: setlist.remove(song) deleteCount = deleteCount + 1 except: print("Error deleting song") sys.stdout.write('\tDELETED') print('\n') print('Deleted %d/%d songs' % (deleteCount, songCount)) try: filehandle.put_list('text/setlist', setlist) except IOError: raise IOError("Error writing set for delete_song") if (deleteCount == 0): outputMsg = 'Could not delete any songs.' else: outputMsg = 'Deleted %d out of %d songs.' % (deleteCount, songCount) return outputMsg
def song_pop(): try: undoList = filehandle.get_list('text/setlist') except IOError: raise IOError("Error opening file for song_undo") if len(undoList) != 0: del undoList[len(undoList) - 1] else: raise IndexError("Empty setlist") try: filehandle.put_list('text/setlist', undoList) except IOError: raise IOError("Error rewriting list for song_undo") return
def song_count(irc, args): total_search = True opts = get_long_opts(args) if ((opts['range'] != None and opts['range'] != [-1,-1]) or opts['country'] != None or opts['tour'] != None): total_search = False song = txtfunctions.acronym_replace(' '.join(opts['other'])) database = filehandle.get_list('text/archive.db') if total_search: for entry in database: if entry.startswith('c!'): db_song = entry.split('!')[1].split('=')[0] if db_song == song.lower(): count = int(entry.split('!')[1].split('=')[1]) irc.msg("%s has been played %d times." % (song, count)) return count = 0 tours = os.listdir('GigArchive') for tour in tours: if opts['tour'] == tour_name(tour): tours = [tour] break for tour in tours: gigs = os.listdir('GigArchive/%s' % tour) for filename in gigs: if not total_search: if (not date_in_range(filename, opts['range']) or (opts['country'] is not None and opts['country'] != country_code(filename))): continue setlist = filehandle.get_list('GigArchive/%s/%s' % (tour, filename)) if song.lower() in [s.lower() for s in setlist]: count = count + 1 if total_search: if len(database) >= 100: database.pop(0) database.append('c!%s=%d' % (song.lower(), count)) filehandle.put_list('text/archive.db', database) irc.msg("%s has been played %d times." % (song, count))
def redo(): global INDEX global UNDOLIST if INDEX >= len(UNDOLIST) - 1: return False setlist = UNDOLIST[INDEX + 1] try: filehandle.put_list(SETFILE, setlist) except IOError as i: print(i) return False INDEX += 1 return True
def undo(): global INDEX global UNDOLIST if INDEX == 0: return False setlist = UNDOLIST[INDEX - 1] try: filehandle.put_list(SETFILE, setlist) except IOError as i: print(i) return False INDEX -= 1 return True
def print_set_by_date(irc, date): date = date.replace('/', '-') try: qyear = year_value(date) except ValueError: irc.msg('Error: Invalid date format.') return database = filehandle.get_list('text/archive.db') for entry in database: if '::' not in entry: continue db_date = entry.split('::')[0] if date == db_date: db_file = entry.split('::')[1].strip() setlist = filehandle.get_list(db_file) irc.msg(setlist[0] + ':') setprint = txtfunctions.create_set_string(setlist[1:]) irc.msg(setprint) return tours = sorted(os.listdir('GigArchive')) for tour in tours: if tour_in_range(tour, qyear): gigs = sorted(os.listdir('GigArchive/%s' % tour)) for gig in gigs: if gig_date(gig) == date: gig_file = 'GigArchive/%s/%s' % (tour, gig) try: setlist = filehandle.get_list(gig_file) except IOError: irc.msg('Error opening gig file.') return irc.msg(setlist[0] + ':') setprint = txtfunctions.create_set_string(setlist[1:]) irc.msg(setprint) if len(database) >= 100: database.pop(0) database.append('%s::%s' % (date, gig_file)) filehandle.put_list('text/archive.db', database) return irc.msg('Could not find the gig from that date.')
def replace_song(argument): compared = False songs = argument.split(', ') length = len(songs) if length != 2: print("Invalid replacement arguments") outputMsg = "ERROR: Invalid number of arguments." return outputMsg else: try: setlist = filehandle.get_list('text/setlist') except IOError: raise IOError("Error getting setlist for replace_song") compareSong = songs[0] compareSong = acronym_replace(compareSong) replaceSong = songs[1] replaceSong = acronym_replace(replaceSong) sys.stdout.write('Replacing %s with %s' % (compareSong, replaceSong)) try: songIndex = setlist.index(compareSong) setlist[songIndex] = replaceSong compared = True except: print('Replace Error: Song not found') outputMsg = "ERROR: Could not find the song to replace." return outputMsg if compared: try: filehandle.put_list('text/setlist', setlist) except IOError: raise IOError("Error writing set for replace_song") sys.stdout.write('\t[DONE]\n') outputMsg = "Song has been replaced" return outputMsg
def insert_song(argument): compared = False songs = argument.split(', ') length = len(songs) if length != 2: print("Invalid replacement arguments") return "ERROR: Invalid number of arguments." else: try: setlist = filehandle.get_list('text/setlist') except IOError: raise IOError("Error getting setlist for insert_song") insertSong = songs[0] insertSong = acronym_replace(insertSong) compareSong = songs[1] compareSong = acronym_replace(compareSong) sys.stdout.write('Inserting %s before %s' % (insertSong, compareSong)) try: songIndex = setlist.index(compareSong) setlist.insert(songIndex, insertSong) compared = True except: print('Compare Error: Song not found') return "ERROR: Could not find the song to insert before." if compared: try: filehandle.put_list('text/setlist', setlist) except IOError: raise IOError("Error writing set for insert_song") sys.stdout.write('\t[DONE]\n') outputMsg = ("Inserted %s before %s" % (insertSong, compareSong)) return outputMsg
def set_previous(): setlist = filehandle.get_list('text/setlist') gig = filehandle.get_list('text/gig') tour = filehandle.get_list('text/tour') print('GIG: %s' % (gig)) if len(gig) == 0: print('SetPrevious Error: GIG not found') outputString = 'ERROR: No gig set.' return outputString if len(setlist) == 0: print('SetPrevious Error: Empty setlist') outputString = 'ERROR: Setlist is empty.' return outputString gig = gig[0] tour = tour[0].strip() sys.stdout.write('FORMATTING GIG NAME...') try: gigParse = gig.split('/') if len(gigParse) > 1: i = 1 gigFileName = gigParse[0] while i < len(gigParse): gigFileName = '%s-%s' % (gigFileName, gigParse[i]) i += 1 else: gigFileName = gigParse[0] except: raise RuntimeError("Error formatting gig") sys.stdout.write('\t[DONE]\n') gigFileName = gigFileName.split('\x0A')[0] gigFileName = gigFileName.split('\r')[0] print('GIG FILE NAME: %s' % (gigFileName)) sys.stdout.write('Writing previous setlist...') archivePath = "GigArchive/" + tour + "/" + gigFileName gig = [gig] try: filehandle.put_list('text/previous', gig) filehandle.put_list(archivePath, gig) except IOError: raise IOError("Error writing setlists for set_previous") for entry in setlist: try: filehandle.list_append('text/previous', entry) filehandle.list_append(archivePath, entry) except IOError: raise IOError("Error appending song for set_previous") sys.stdout.write('\t[DONE]\nUpdating database...') database = filehandle.get_list('text/archive.db') for entry in database: if entry.startswith('c!'): song = entry.split('!')[1].split('=')[0] if song in [s.lower() for s in setlist]: count = int(entry.split('!')[1].split('=')[1]) count = count + 1 database[database.index(entry)] = 'c!%s=%d' % (song, count) elif entry.startswith('lp!'): song = entry.split('!')[1].split('::')[0] if song in [s.lower() for s in setlist]: database[database.index(entry)] = 'lp!%s::%s' % (song, archivePath) filehandle.put_list('text/archive.db', database) sys.stdout.write('\t[DONE]\nTweeting setlist...') twit.tweet_setlist() sys.stdout.write('\t[DONE]\n') outputString = "Current setlist copied over as previous set." return outputString