def makeOAuth(ID,SECRET): """ Makes the .ini file for OAuth2Util. The template was taken from their GitHub readme, https://github.com/SmBe19/praw-OAuth2Util/blob/master/OAuth2Util/README.md """ global showWarnings with open(MYDIR+"/MyFiles/oauth.ini","w") as ini: ini.write('[app]\n' '# These grant the bot to every scope, only use those you want it to access.\n' 'scope = identity,account,edit,flair,history,livemanage,modconfig,modflair,modlog,modothers,modposts,modself,modwiki,mysubreddits,privatemessages,read,report,save,submit,subscribe,vote,wikiedit,wikiread\n' 'refreshable = True\n' 'app_key = '+ID+'\n' 'app_secret = '+SECRET+'\n' '\n' '[server]\n' 'server_mode = False\n' 'url = 127.0.0.1\n' 'port = 65010\n' 'redirect_path = authorize_callback\n' 'link_path = oauth\n' '\n' '# Will be filled automatically\n' '[token]\n' 'token = None\n' 'refresh_token = None\n' 'valid_until = 0') if showWarnings: warn=("No oauth.ini file was found. I have generated one " "from the information in MyAccount.cfg. OAuth will open " "your browser to ask for your permission to allow me to " "login.\n NOTE: You may want to edit the scope if I don't " "have my own dedicated account.") wrap(warn)
def makeAccount(): """ Makes the template for the account settings. Doesn't auto-populate (obviously), so it exits if it can't find the file. """ global showWarnings with open(MYDIR+'/MyAccount.cfg','w') as cfg: cfg.write("USERNAME:\nID:\nSECRET:") if showWarnings: warn = (MYDIR+'/MyAccount.cfg" was not found!\n *****This is ' 'fatal!\n Please re-enter the account credentials into ' 'that file.') wrap(warn) exit()
def firstTime(): """ Launches if the placeholder isn't found. Greets the user with a pleasant message, makes the required files, then exits """ greeting = ("Hello! My name is "+MYNAME+" - it's nice to meet you! " "I'm going to be using my folder at \""+MYDIR+"\". Before I can begin " "working, I'll need to know my Reddit account name, ID, and secret. " "Please open \""+MYDIR+"/MyAccount.cfg\" and enter my account " "information where it says to. I look forward to working with you!") wrap(greeting) global showWarnings showWarnings=False makeAccount() exit()
def checkForFiles(): """ Checks to make sure all necessary files and folders exist. """ if not path.exists(MYDIR+'/MessageArchive'): mkdir(MYDIR+'/MessageArchive') if not path.exists(MYDIR+'/MyFiles'): mkdir(MYDIR+'/MyFiles') if not path.exists(MYDIR+'/MyFiles/'+MYNAME+'.placeholder'): with open(MYDIR+'/MyFiles/'+MYNAME+'.placeholder','w') as f: f.write('This is a placeholder to signify that the bot has been loaded before. Do not delete this file.') firstTime() if not path.exists(MYDIR+"/MyAccount.cfg"): makeAccount() loadAccount() global ID,SECRET if not path.exists(MYDIR+"/MyFiles/oauth.ini"): makeOAuth(ID,SECRET) elif not open(MYDIR+"/MyFiles/oauth.ini","r").read(): makeOAuth(ID,SECRET) if not path.exists(MYDIR+"/SantaList.csv"): wrap("I do not see a \"SantaList.csv\" file in my home directory ("+MYDIR+"). " "This is fatal. Exiting...") exit()
def loadAccount(): """ Defining the account credentials for the bot to use """ with open(MYDIR+'/MyAccount.cfg','r') as cfg: cfg=cfg.read() creds=cfg.splitlines() ACCOUNT = creds[0].replace("USERNAME:"******"") global ID ID = creds[1].replace("ID:","") global SECRET SECRET = creds[2].replace("SECRET:","") ## Remove leading space if it exists for variable in ['ACCOUNT', 'ID', 'SECRET']: if eval(variable)[0] == " ": exec(variable+"="+eval(variable)[1:]) if ACCOUNT == "": warn =("I don't have my account name listed, so I can't " "sign in. Please, enter my account credentials in the " "config file \""+MYDIR+"/MyAccount.cfg\"\n *****This " "is fatal!\n Exiting...") wrap(warn) exit() if ID == ("" or "(The account ID goes here!)"): warn =("I don't have my ID listed, so I can't sign in. " "Go to https://github.com/reddit/reddit/wiki/OAuth2 " "If you do not know how to get this information.\n " "Please, enter my account credentials in the config file" " \""+MYDIR+"/MyAccount.cfg\"\n *****This is fatal!\n " "Exiting...") wrap(warn) exit() if SECRET == ("" or "(The account secret goes here!)"): warn =("I don't have my secret listed, so I can't sign in. " "Go to https://github.com/reddit/reddit/wiki/OAuth2 " "If you do not know how to get this information.\n " "Please, enter my account credentials in the config file" " \""+MYDIR+"/MyAccount.cfg\"\n *****This is fatal!\n " "Exiting...") wrap(warn) exit()
SantaList = loadArray(MYDIR+"/SantaList.csv") ACCOUNT = account() PMLink = "https://www.reddit.com/message/compose/?to="+ACCOUNT+"&subject="+URLSyntax('Closet Santa message')+"&message=" botFlair=("\n\nHappy holidays from "+MYNAME+" and the /r/ClosetSanta mods!\n*****\n" "[Send mail to your gift recipient]("+PMLink+URLSyntax('From: Santa-san\n\nMessage: ')+") or " "[Send mail to your Closet Santa]("+PMLink+URLSyntax('To: Santa-san\n\nMessage: ')+")\n\n" "[^^I'm ^^open ^^source!](https://github.com/WolfgangAxel/ClosetSantaMessagingBot)") ## Import Reddit functions import getPMs ## Begin monitoring if __name__ == '__main__': greeting = "\n"+MYNAME+", reporting for duty!\n I will be using the account /u/"+ACCOUNT print "\n" wrap(greeting) print "From Santa link: "+PMLink+URLSyntax('From: Santa-san\n\nMessage: ') print "To Santa link: "+PMLink+URLSyntax('To: Santa-san\n\nMessage: ') while True: try: begin = time() getPMs.getPMs() end = time() if int(end-begin) < BOTFREQUENCY: sleep(BOTFREQUENCY-int(end-begin)) except Exception as e: wrap("Looks like I'm having internet connection issues. I will try again in 5 minutes.\n\nError details: "+str(e.args)) sleep(300)