import os

import praw
import json
import requests
from config import username, password, subreddit

# Why is this even a function

def wikilog(r, subreddit, wikipage, error):
    r.edit_wiki_page(subreddit, wikipage, error, error)

if __name__ == "__main__":
    print "Starting setup.py..."
    print "Attempting to log in..."
    r = praw.Reddit("Sidebar livestream updater for /r/{} by /u/andygmb ".format(subreddit))
    try:
        r.login(username=username, password=password, disable_warning=True)
        print "Success!\n"
    except praw.errors.InvalidUserPass:
        print "Make sure you have your bot's details in config.py"
    sub = r.get_subreddit(subreddit)
    path_to_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "default_wiki_config.json")
    with open(path_to_file, "r") as configjson:
        try:
            print "\nAttempting to load default_wiki_config.json..."
            config = json.load(configjson)
            prettyconfig = "    "  + json.dumps(config, indent=4).replace("\n", "\n    ")
            print "Success!\n"
        except ValueError:
            print "Invalid JSON in local file: default_wiki_config.json"
            wikilog(r, sub, "twitchbot_error_log", "Invalid JSON in local file: default_wiki_config.json")
            raise

    try:
        print "Attempting to add default_wiki_config.json to https://www.reddit.com/r/{}/twitchbot_config ...".format(subreddit)
        r.edit_wiki_page(sub, "twitchbot_config", prettyconfig, "Initial setup from setup.py")
        r.get_wiki_page(sub, "twitchbot_config").edit_settings(permlevel=2, listed=True)
        print "Success!\n".format(subreddit)
    except requests.exceptions.HTTPError:
        print "Couldn't access https://www.reddit.com/r/{}/wiki/{}, reddit may be down.".format(subreddit, "twitchbot_config")
        wikilog(r, sub, config["wikipages"]["error_log"], "Couldn't access wiki page, reddit may be down.")
        raise

    for wikipage in config["wikipages"].values():
        try:
            if not "config/" in wikipage:
                try:
                    page = r.get_wiki_page(sub, wikipage)
                    content = page.content_md
                except:
                    page = r.get_wiki_page(sub, wikipage)
                    content = ""
                print "Attempting to set up https://www.reddit.com/r/{}/wiki/{} ...".format(subreddit, wikipage)
                r.edit_wiki_page(sub, wikipage, content.encode("utf8"), "Initial setup from setup.py")
                page.edit_settings(permlevel=2, listed=True)
                print "Success!\n".format(wikipage)
        except requests.exceptions.HTTPError:
            print "Couldn't access https://www.reddit.com/r/{}/wiki/{}, reddit may be down.".format(subreddit, wikipage)
            wikilog(r, sub, wikipage, "Couldn't access wiki page, reddit may be down.")
            raise

    if config["accept_messages"].lower() in ["true", "yes", "y"]:
        print "Setup.py finished!\n\n"
        print "By default the bot will respond to PMs and update https://wwww.reddit.com/r/{}/wiki/{} with any twitch.tv links users send it.".format(subreddit, config["wikipages"]["stream_list"])
        print "Place the following link in your sidebar for people to message the bot twitch.tv streams:"
        print "http://www.reddit.com/message/compose?to={username}&subject=Twitch.tv+request+%2Fr%2F{subreddit}&message=http%3A%2F%2Fwww.twitch.tv%2F{username}".format(username=username, subreddit=subreddit)
        print '\nIf you do not want the bot to update the list of streams through PM, please edit https://wwww.reddit.com/r/{}/wiki/twitchbot_config and set "accept_messages" to "False".'.format(subreddit)
    else:
        print "Setup.py finished!\n\n"

@@ -2005,37 +2005,45 @@
 lues():%0A        
+    
 try:%0A
+    
             if n
@@ -2068,16 +2068,53 @@
 kipage:%0A
+                    content = %22%22%0A    
         
@@ -2138,32 +2138,36 @@
                 
+    
 page = r.get_wik
@@ -2180,32 +2180,36 @@
 (sub, wikipage)%0A
+    
                 
@@ -2254,22 +2254,47 @@
         
+    
 except
+ praw.errors.NotFound
 :%0A      
@@ -2311,79 +2311,21 @@
     
-page = r.get_wiki_page(sub, wikipage)%0A                    content = %22%22%0A
+    pass%0A    
     
@@ -2425,32 +2425,36 @@
 ddit, wikipage)%0A
+    
                 
@@ -2556,20 +2556,50 @@
         
+    r.get_wiki_page(sub, wiki
 page
+)
 .edit_se
@@ -2639,32 +2639,36 @@
                 
+    
 print %22Success!%5C
@@ -2691,24 +2691,28 @@
 ge)%0A        
+   
+ 
 except reque
@@ -2729,32 +2729,36 @@
 ions.HTTPError:%0A
+    
             prin
@@ -2866,32 +2866,36 @@
 ge)%0A            
+  
+  
 wikilog(r, sub, 
@@ -2962,31 +2962,43 @@
             
+    
 raise%0A
-%0A
+    %0A    
     if confi
@@ -3051,32 +3051,36 @@
 , %22y%22%5D:%0A        
+    
 print %22Setup.py 
@@ -3090,24 +3090,28 @@
 ished!%5Cn%5Cn%22%0A
+    
         prin
@@ -3299,24 +3299,28 @@
 %22%5D)%0A        
+    
 print %22Place
@@ -3409,24 +3409,28 @@
 s:%22%0A        
+   
+ 
 print %22http:
@@ -3621,24 +3621,28 @@
 it)%0A        
+    
 print '%5CnIf 
@@ -3835,14 +3835,22 @@
     
+  
+  
 else:%0A
+    
     
@@ -3864,28 +3864,29 @@
 nt %22Setup.py finished!%5Cn%5Cn%22%0A
+%0A

Fix issue with setup.py not handling praw.errors.NotFound correctly