Explanation how to use sub mirroring script; script in comments

6  2019-09-17 by ChineseGuitars

7 comments
import string
import time
import datetime
import praw
import psaw

acc = praw.Reddit(
        client_id="7_cXnJtUBwJvrQ",
        client_secret="v-q4pmUcVt75IL1388Agtk5SYaQ",
        username="ChineseGuitars",
        user_agent="whatever",
        password="gayguitarist69"
        )

api = psaw.PushshiftAPI()


def nuke():
    leeren = True
    while leeren:  
        c = list(acc.user.me().submissions.new(limit=None))+list(acc.user.me().comments.new(limit=None))
        if len(c) == 0:
            leeren = False
        for a in c:
            a.delete()

def nuke_p(p):
    leeren = True
    while leeren:  
        c = [a for a in (acc.user.me().submissions.new(limit=None)) if a.subreddit == p] + [a for a in(acc.user.me().comments.new(limit=None)) if a.subreddit == p]
        if len(c) == 0:
            print(len(c))
            leeren = False
        for a in c:
            a.delete()

class onaReplicator():
    def __init__(self, *args):
        self.printed = dict()
        self.printedc = dict()
        self.sprinted = set()
        self.cprinted = set()        
        self.subs_to_do = list(args)[1:]
        self.mirror = list(args)[0]
        self.submissions = self.get_allsubs()
        self.comments = self.get_allcoms()
        self.submit()
        self.submit_comments()

    def mirror(self, mirrored, mirror):
        pass

    def get_sub(self, subname):
        return list(api.search_submissions(subreddit=subname))
    def get_coms(self, subname):
        return list(api.search_comments(subreddit=subname))  

    def get_allsubs(self):
        submissions = []
        for a in self.subs_to_do:
            for a in self.get_sub(a):
                if a not in submissions:
                    submissions.append(a)
        submissions.sort(key=lambda x:x.created, reverse=False)
        return submissions

    def get_allcoms(self):
        comments = []
        for a in self.subs_to_do:
            for b in self.get_coms(a):
                comments.append(b)
        comments.sort(key=lambda x:x.created, reverse=True)
        comments.reverse()
        return comments

    def get_date(self, t):
        ts = t.created
        return str(datetime.datetime.utcfromtimestamp(ts))

    def submit(self):
        print("submitting posts")
        for a in self.submissions[:]:
            try:
                title = a.title + self.get_sig(a)
                if len(title) > 300:
                    title = title[:299]
                if not a.selftext == "":
                    acc.subreddit(self.mirror).submit(title, a.selftext)
                else:
                    if a.is_video:
                        acc.subreddit(self.mirror).submit_video(title, a.selftext)
                    else: 
                        acc.subreddit(self.mirror).submit(title, url=a.url)
                self.printed[a.id]=self.lastsub()
                self.sprinted.add(a.id)
            except Exception as e:
                print(e)
    def lastcom(self):
        return next(acc.user.me().comments.new(limit=1)).id
    def lastsub(self):
        return next(acc.user.me().submissions.new(limit=1)).id


    def submit_comments(self, mc = 20):      #NEEDS IMPROVEMENT!  
        for a in range(mc):
            print('comments left to do: ' + str(len(self.comments)))
            for a in self.comments:
                try:
                    mi = a.parent_id[3:]
                    if mi in self.sprinted:
                        acc.submission(self.printed[mi]).reply(self.get_sig(a) + "\n\n" + a.body)
                        self.printedc[a.id] = self.lastcom()
                        self.cprinted.add(a.id)
                        self.comments.remove(a)
                    if mi in self.cprinted:
                        acc.comment(self.printedc[mi]).reply(self.get_sig(a) + "\n\n" + a.body)
                        self.printedc[a.id] = self.lastcom()
                        self.cprinted.add(a.id)
                        self.comments.remove(a)

                except Exception as e:
                    print("Comment went wrong" + str(e))





    def get_sig(self, t):
        return " | By: "+t.author+" Score: "+str(t.score)+" Date: "+self.get_date(t) + " (UTC) Sub: "+t.subreddit+" | "

def listen():
    for m in acc.inbox.messages():
        if m.subject == "mirror" and m.new:
            handle(m)

def handle(m):
    message = ""    
    b = m.body.lower().split(" ")                         
    if m.author in list(acc.subreddit(b[0]).moderator()):
            try:
                for a in b[1:]:                     
                    next(api.search_submissions(subreddit=a))
                message = ("will copy: "+str(b[1:])+" to "+b[0])
                acc.redditor(str(m.author)).message('Your Request', message)             
                s = onaReplicator(b[0], b[1:])
                message="All done."           

            except Exception as e:
                print("Something went wrong: " + str(e))
    else:
        message = ('You not a mod at '+b[0])

    acc.redditor(str(m.author)).message('Your Request', message)
    print("handled")
    m.delete()  

if __name__=='__main__':
    pick = True
    choice = None
    while pick:
        print(choice)
        choice = input("1) Do you want to fill up a sub of your own?\n\n2)Do you want to listen for other users' requests to fill their subs?\n\n3) Nuke?\n\n")

        if choice in ["1", "2", "3"]:
            pick = False


    if choice == "1":
        mirror = input("The name of your sub to fill: \n>")
        print("\n")
        mirrored = input("The name of the sub to copy: \n>")
        s = onaReplicator(mirror, mirrored)
    elif choice == "2":
        while True:
            print("\nlistening...")
            listen()
    else:

        nuking = input("Type in name of the sub (or just press enter to delete EVERYTHING): \n>")
        if nuking == "":
            c2 = input("Sure everything should be deleted? [y/n]")
            if c2.lower() == "y":
                nuke()
        else:

            nuke_p(nuking)

Forgot to mention that you need to install 2 third party libraries (praw and psaw) Run the command line and type: pip install praw pip install psaw

fyi i believe you need both python3 and pip3, so make sure you use pip3 install. And python3 to run or you need an alias.

You are absolutely right about python3. Forgot to mention that.

I must be retarded.

If you failed to get it running, you can PM me about it. This post is a pretty low-effort tutorial. Not your fault.

Da fuck