from __future__ import absolute_import, unicode_literals
import pywikibot from pywikibot import pagegenerators
from pywikibot.bot import (
SingleSiteBot, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot)
from pywikibot.tools import issue_deprecation_warning
import sys import pywikibot from pywikibot import pagegenerators import msvcrt
import pyaudio import wave import os.path
import time
import re import io
global input_device_index global_input_device_index = 2 # = 2 for Reaper class recordBot():
def __init__(self): #self.newRecording = print("RecordBot Initialsed") print #main()
def getArticleList(self):
list1= with io.open(fileNameGlobal, encoding='utf8') as infile: #encode error here? ,encoding='utf-8' #infile=str(infile.encoding('utf8')) #print "infile: " + str(infile) for line in infile: #line = unicode(line) #line = line.decode('utf8') #print(line.rstrip()) #list1.append(line.rstrip()) #line0=line.decode('utf-8') altFilename = line.replace("'", "_") ##hack to get apostrophes working list1.append(altFilename) #encode error here? #print "unicode line: " + line #f.close() bot = recordBot() trimList = bot.trimArticleTitles(list1) print ("Working on " + str(len(trimList)) + " pages...") return trimList
def trimArticleTitles(self, inputList): trimList= #print len(inputList) for line in inputList: #lineUni=unicode(line, "utf-8") #mucking about with unicode line= line.encode('utf-8') #line0 = line.decode('utf-8') #line1=lineUni line1 = line.decode('utf8') #line1=str(line) #print "line 1 is: " + line1 if re.search('\(',line1)!= None:
index = line1.rfind('(') trimmed=line1 trimList.append(trimmed) #print trimmed elif re.search(',',line1) != None: index2=line1.rfind(',') trimmed2=line1 trimList.append(trimmed2) #print trimmed2 else: trimList.append(line1) #fixes error for items without , or ( #print line1 return inputList #return trimList - turned off for Wiktionary work
def newRecording(self, masterList, upto): #fileTitle FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 CHUNK = 1024 RECORD_SECONDS = 4 #input_device_index = 2 audio = pyaudio.PyAudio()
# start Recording stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK, input_device_index=global_input_device_index) #print "recording..." frames =
print() print (" RECORDING - Press SPACE to save and continue, ") print (" to skip or to quit") print (" to pause") print() #newChar="g" while 1: data = stream.read(CHUNK) frames.append(data) if msvcrt.kbhit(): newChar = msvcrt.getch().decode('utf-8') if newChar==" ": break
else: return newChar print() print (" FINISHED RECORDING") print()
fileTitle=masterList.rstrip() #added rstrip #print("title: '" + fileTitle+"'") # stop Recording stream.stop_stream() stream.close() audio.terminate() #newFileName = 'C:\\Users\\jim\\pywikitest2\\' + str(fileTitle)+ '.wav'
index2=fileNameGlobal.rfind('.txt') trimmed2=fileNameGlobal
#newFileName = trimmed2.decode('utf8') + u'\\' + fileTitle.decode('utf8')+ u'.wav' #create new folder for each file list newFileName = trimmed2 + u'\\' + fileTitle+ u'.wav' #print newFileName newDir = trimmed2 + '\\' if os.path.isdir(newDir)==False: os.mkdir(trimmed2) #print newDir num=1 if os.path.isfile(newFileName): while os.path.isfile(newFileName): num=num+1 newFileName= trimmed2 + '\\' + fileTitle + str(num) + '.wav' #print newFileName
#newFileName = 'C:\\Users\\jim\\pywikitest2\\' + str(fileTitle)+ '.wav' #print("New file NAME: " + newFileName) waveFile = wave.open(newFileName, 'wb') waveFile.setnchannels(CHANNELS) waveFile.setsampwidth(audio.get_sample_size(FORMAT)) waveFile.setframerate(RATE) waveFile.writeframes(b.join(frames)) waveFile.close
if msvcrt.kbhit(): newChar = msvcrt.getch().decode('utf-8') return newChar
return newChar def consoleDisplay(self, artList, whereUpto):
if whereUpto + 1 > len(artList): print(" END OF LIST REACHED") sys.exit() elif whereUpto +1 < len(artList): print (" UP NEXT " + str(artList)) else: print( " END ") print ("%-------------------------------------------------------%") print (">>>>>>>>>>>>> " + str(artList)) # + " <<<") print ("%-------------------------------------------------------%") print (" LAST ITEM " + str(artList)) print()
def main(*args):
local_args = pywikibot.handle_args(args) args = local_args argTextFile = args #excluding .txt suffix stripEnd = argTextFile.find("_sf_") dirOnly = argTextFile global fileNameGlobal fileNameGlobal = u'C:\\Users\\jim\\pywikitest\\' +str(dirOnly) + '\\' + str(argTextFile) +'.txt' #u at start #print "dir only: " + str(dirOnly) #print "global filename: " + str(fileNameGlobal) bot = recordBot() fullList = bot.getArticleList()
#index2=fileNameGlobal.rfind('.txt') #create new directory #trimmed2=fileNameGlobal #os.mkdir(trimmed2) length=len(fullList) print ("Get ready to rumble - press SPACE BAR to get started") print() if msvcrt.getch().decode('utf-8')==" ":
#print("z pressed")
waitingForOptions = True conUpto=0 recUpto=0 rec3=" "
for rr in range(0,length): if rec3 == "k": #skip disp = bot.consoleDisplay(fullList, conUpto) conUpto=conUpto+1 rec3 = bot.newRecording(fullList,recUpto) recUpto=recUpto+1 if rec3 == "q": sys.exit()
if rec3==" ": disp = bot.consoleDisplay(fullList, conUpto) rec3 = bot.newRecording(fullList,recUpto) recUpto=recUpto+1 conUpto=conUpto+1
if rec3 == "p": print ("PAUSED - Press to continue") print() #print " to skip or to quit" while rec3 != "c": rec3=msvcrt.getch().decode('utf-8') disp = bot.consoleDisplay(fullList, conUpto-1) rec3 = bot.newRecording(fullList,recUpto-1)
if __name__ == '__main__':
main()