예제 #1
0
def getPostByUrl(urlin):
    submission = reddit.submission(url=urlin)

    comments = []
    amountReplies = settings.reddit_replies_per_comment
    try:
        for commentThread in range(0, settings.reddit_comments_per_post, 1):
            try:
                threadcomments = ()
                thread = submission.comments[commentThread]
                text = thread.body
                author = thread.author.name
                ups = thread.ups
                if author is None:
                    author = "deleted"
                threadcomments = threadcomments + ((author, text, ups), )
                prevreply = thread
                for i in range(0, amountReplies, 1):
                    try:
                        reply = list(prevreply.replies)[0]
                        try:
                            text = reply.body
                            author = reply.author.name
                            ups = reply.ups
                            threadcomments = threadcomments + (
                                (author, text, ups), )
                        except AttributeError:
                            continue
                        prevreply = reply
                    except IndexError:
                        continue
                comments.append(threadcomments)
            except (IndexError, AttributeError):
                pass
    except Exception:
        print("Error parsing script, skipping")

    time_created = (datetime.datetime.fromtimestamp(
        submission.created_utc).replace(
            tzinfo=timezone.utc).strftime("%Y-%m-%d %H:%M:%S"))
    now = datetime.datetime.now()
    time_gathered = now.strftime('%Y-%m-%d %H:%M:%S')

    all_scripts = database.getScriptIds()
    try:
        index = [scriptid[1] for scriptid in all_scripts].index(submission.id)
        return None
    except Exception:
        pass

    newSub = Submission(submission.subreddit_id, submission.id,
                        submission.subreddit.display_name,
                        submission.permalink, submission.url, submission.title,
                        submission.author.name, submission.ups,
                        submission.downs, submission.num_comments, comments,
                        time_created, time_gathered, submission.visited, False)
    return newSub
def getInfo(subredditname, amount):
    all_scripts = database.getScriptIds()
    subs = []
    subreddit = reddit.subreddit(subredditname)
    hot_subreddit = subreddit.hot(limit=amount)
    for x, submission in enumerate(hot_subreddit):
        alreadyIn = False
        print("%s/%s" % (x + 1, amount))
        if submission.num_comments < 1000:
            print("Too little comments (%s)" % submission.num_comments)
            continue
        if submission.id in [scriptid[1] for scriptid in all_scripts]:
            index = [scriptid[1]
                     for scriptid in all_scripts].index(submission.id)
            if [scriptstatus[2]
                    for scriptstatus in all_scripts][index] == "RAW":
                print("Script already in database, updating old script")
                alreadyIn = True
            else:
                print("Script already complete, skipping")
                continue
        comments = []
        amountReplies = 4
        try:
            for commentThread in range(0, 30, 1):
                try:
                    threadcomments = ()
                    thread = submission.comments[commentThread]
                    text = thread.body
                    author = thread.author.name
                    ups = thread.ups
                    if author is None:
                        author = "deleted"
                    threadcomments = threadcomments + ((author, text, ups), )
                    prevreply = thread
                    for i in range(0, amountReplies, 1):
                        try:
                            reply = list(prevreply.replies)[0]
                            try:
                                text = reply.body
                                author = reply.author.name
                                ups = reply.ups
                                threadcomments = threadcomments + (
                                    (author, text, ups), )
                            except AttributeError:
                                continue
                            prevreply = reply
                        except IndexError:
                            continue
                    comments.append(threadcomments)
                except (IndexError, AttributeError):
                    pass
        except Exception:
            print("Error parsing script, skipping")
            continue

        if not alreadyIn:
            print("Submission good to add")
        now = datetime.datetime.now()
        author = str(submission.author)
        time_created = (datetime.datetime.fromtimestamp(
            submission.created_utc).replace(
                tzinfo=timezone.utc).strftime("%Y-%m-%d %H:%M:%S"))
        time_gathered = now.strftime('%Y-%m-%d %H:%M:%S')
        newSub = Submission(submission.subreddit_id, submission.id,
                            subredditname, submission.permalink,
                            submission.url, submission.title, author,
                            submission.ups, submission.downs,
                            submission.num_comments, comments, time_created,
                            time_gathered, submission.visited, alreadyIn)
        subs.append(newSub)
    return subs
def clientTick(client):
    print("Server tick thread started for client")
    HEADERSIZE = 10
    while True:
        if client.disconnect:
            print("%s SERVER user %s disconnected" %
                  (datetime.datetime.now(), repr(client.username)))
            break
        full_msg = b''
        new_msg = True
        while True:
            try:
                client_connection = client.connection
                buf = client_connection.recv(2048)
                if new_msg:
                    try:
                        msglen = int(buf[:HEADERSIZE])
                    except ValueError:
                        print("client disconnect error")
                        # happens when client disconnects
                        break
                    new_msg = False

                full_msg += buf
            except ConnectionResetError:
                print("%s SERVER user %s connecton reset error" %
                      (datetime.datetime.now(), repr(client.username)))
                break
            download_size = len(full_msg) - HEADERSIZE
            if download_size == msglen:
                if download_size > 100000:
                    print("%s SERVER received large message (%s)" %
                          (datetime.datetime.now(),
                           str(download_size / 1000000) + "MB"))
                try:
                    incomingdata = pickle.loads(full_msg[HEADERSIZE:])
                except EOFError:
                    print("%s SERVER user %s disconnected" %
                          (datetime.datetime.now(), repr(client.username)))
                    break
                new_msg = True
                full_msg = b""

                if not client.authorized:
                    if "login-attempt" == incomingdata[0]:
                        print("%s SERVER user %s login attempt" %
                              (datetime.datetime.now(), repr(incomingdata[1])))
                        username = incomingdata[1]
                        password = incomingdata[2]
                        login = (database.login(username, password))
                        online_users = database.getOnlineUsers()
                        if username in online_users:
                            print("%s SERVER user %s already logged in" %
                                  (datetime.datetime.now(),
                                   repr(incomingdata[1])))
                            sendToClient(client_connection,
                                         ("login-success", False, None))
                        else:
                            if login:
                                key = generateKey()
                                client.key = key
                                client.username = username
                                sendToClient(client_connection,
                                             ("login-success", True, key))
                                client.authorized = True
                                print("%s SERVER user %s logged in" %
                                      (datetime.datetime.now(),
                                       repr(incomingdata[1])))
                                database.updateUserStatus(username, "ONLINE")
                            else:
                                sendToClient(client_connection,
                                             ("login-success", False, None))
                                print("%s SERVER user %s wrong password" %
                                      (datetime.datetime.now(),
                                       repr(incomingdata[1])))

                else:
                    if "request-scripts" == incomingdata[1]:
                        print("%s SERVER user %s request scripts" %
                              (datetime.datetime.now(), repr(client.username)))
                        if incomingdata[0] == client.key:
                            print("%s SERVER sending scripts to user %s" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))
                            amount = incomingdata[2]
                            filter = incomingdata[3]
                            if filter == "ups":
                                data = database.getScripts(amount, "ups")
                                sendToClient(client_connection,
                                             ("scripts-return", data,
                                              settings.music_types))
                            elif filter == "latest posts":
                                data = database.getScripts(
                                    amount, "timecreated")
                                sendToClient(client_connection,
                                             ("scripts-return", data,
                                              settings.music_types))
                            elif filter == "recently added":
                                data = database.getScripts(
                                    amount, "timegathered")
                                sendToClient(client_connection,
                                             ("scripts-return", data,
                                              settings.music_types))
                            elif filter == "comments":
                                data = database.getScripts(
                                    amount, "num_comments")
                                sendToClient(client_connection,
                                             ("scripts-return", data,
                                              settings.music_types))

                            pass
                        else:
                            print("%s SERVER user %s key does not match up" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))
                    elif "edit-script" == incomingdata[1]:
                        scriptno = incomingdata[2]
                        print("%s SERVER user %s request to edit script %s" %
                              (datetime.datetime.now(), repr(
                                  client.username), scriptno))
                        if incomingdata[0] == client.key:
                            script_status = database.getScriptStatus(scriptno)
                            if script_status == "RAW":
                                print(
                                    "%s SERVER allowing user %s to edit script %s"
                                    % (datetime.datetime.now(),
                                       repr(client.username), scriptno))
                                client.editingScript = scriptno
                                database.updateScriptStatus(
                                    "EDITING", client.username, scriptno)
                                sendToClient(
                                    client.connection,
                                    ('edit-script-success', True, scriptno))
                                sendToAllClients(
                                    ('script-status-update', scriptno,
                                     "EDITING", client.username))
                                print(
                                    "%s SERVER sending all clients (%s) status update for %s"
                                    %
                                    (datetime.datetime.now(),
                                     len(getAllClientConnections()), scriptno))
                            elif script_status == "EDITING":
                                print(
                                    "%s SERVER refusing user %s to edit script %s"
                                    % (datetime.datetime.now(),
                                       repr(client.username), scriptno))
                                sendToClient(
                                    client.connection,
                                    ('edit-script-success', False, scriptno))
                        else:
                            print("%s SERVER user %s key does not match up" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))
                    elif "upload-video" == incomingdata[1]:
                        if incomingdata[0] == client.key:
                            scriptno = incomingdata[2]
                            video_generator_payload = incomingdata[3]
                            script_status = database.getScriptStatus(scriptno)
                            if script_status == "EDITING":
                                if scriptno == client.editingScript:
                                    print(
                                        "%s SERVER allowing user %s to upload script number %s"
                                        % (datetime.datetime.now(),
                                           repr(client.username), scriptno))
                                    if database.uploadVid(
                                            video_generator_payload, scriptno):
                                        database.updateScriptStatus(
                                            "COMPLETE", client.username,
                                            scriptno)
                                        sendToClient(client_connection,
                                                     ('script-upload-success',
                                                      True, scriptno))
                                        client.scriptsComplete.append(scriptno)
                                        client.editingScript = None
                                    else:
                                        sendToClient(client_connection,
                                                     ('script-upload-success',
                                                      False, scriptno))

                                    sendToAllClients(
                                        ('script-status-update', scriptno,
                                         "COMPLETE", client.username))

                                else:
                                    print(
                                        "%s SERVER user %s script number %s does not match what client is editing %s"
                                        % (datetime.datetime.now(),
                                           repr(client.username), scriptno,
                                           client.editingScript))

                            else:
                                print("%s SERVER user %s script status is %s" %
                                      (datetime.datetime.now(),
                                       repr(client.username), script_status))

                        else:
                            print("%s SERVER user %s key does not match up" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))

                    elif "quit-editing" == incomingdata[1]:
                        if incomingdata[0] == client.key:
                            scriptno = incomingdata[2]
                            if client.editingScript == scriptno:
                                database.updateScriptStatus(
                                    "RAW", None, scriptno)
                                print("%s SERVER user %s quit editing %s" %
                                      (datetime.datetime.now(),
                                       repr(client.username), scriptno))
                                sendToAllClients(('script-status-update',
                                                  scriptno, "RAW", None))

                                client.editingScript = None
                            else:
                                print(
                                    "%s SERVER user %s not editing script %s" %
                                    (datetime.datetime.now(),
                                     repr(client.username), scriptno))
                        else:
                            print("%s SERVER user %s key does not match up" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))

                    elif "flag-scripts" == incomingdata[1]:
                        if incomingdata[0] == client.key:
                            scriptno = incomingdata[2]
                            flagtype = incomingdata[3]
                            database.updateScriptStatus(
                                flagtype, client.username, scriptno)
                            print(
                                "%s SERVER user %s flagging script %s as %s" %
                                (datetime.datetime.now(), repr(
                                    client.username), scriptno, flagtype))
                            sendToAllClients(('script-status-update', scriptno,
                                              flagtype, client.username))
                            client.editingScript = None
                        else:
                            print("%s SERVER user %s key does not match up" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))

                    elif "add-script" == incomingdata[1]:
                        if incomingdata[0] == client.key:
                            url = incomingdata[2]
                            try:
                                post = reddit.getPostByUrl(url)

                                if post is not None:

                                    all_scripts = database.getScriptIds()

                                    scriptIds = [
                                        scriptid[1] for scriptid in all_scripts
                                    ]
                                    print("Got script ids")
                                    if post.submission_id in scriptIds:
                                        print("Found script with same id")
                                        database.updateScriptStatusById(
                                            "RAW", client.username,
                                            post.submission_id)
                                        print("Set it to raw.")
                                        database.updateSubmission(post)
                                        print("Updated submission")
                                        print(
                                            "%s SERVER user %s reset script %s"
                                            % (datetime.datetime.now(),
                                               repr(client.username),
                                               post.submission_id))
                                        sendToClient(client_connection,
                                                     ('add-script-success',
                                                      True, "Reset script"))
                                    else:
                                        print(
                                            "%s SERVER user %s added script %s"
                                            % (datetime.datetime.now(),
                                               repr(client.username),
                                               post.submission_id))
                                        database.addSubmission(post)
                                        sendToClient(
                                            client_connection,
                                            ('add-script-success', True,
                                             "Successfully added script"))

                                else:
                                    print(
                                        "%s SERVER user %s attempted to add script that already exists"
                                        % (datetime.datetime.now(),
                                           repr(client.username)))
                                    sendToClient(client_connection,
                                                 ('add-script-success', False,
                                                  "Error occured with url."))
                            except Exception as e:
                                print(
                                    "%s SERVER user %s error attempting to add script %s"
                                    % (datetime.datetime.now(),
                                       repr(client.username), url))
                                print(e)
                                sendToClient(client_connection, (
                                    'add-script-success', False,
                                    "An error occured trying to add the script"
                                ))

                        else:
                            print("%s SERVER user %s key does not match up" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))

                    elif "PING" == incomingdata[1]:
                        if incomingdata[0] == client.key:
                            client.lastPing = datetime.datetime.now()
                            print("%s SERVER sending PONG to %s" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))
                            sendToClient(client.connection, ('PONG', ))
                        else:
                            print("%s SERVER user %s key does not match up" %
                                  (datetime.datetime.now(),
                                   repr(client.username)))

                    if (datetime.datetime.now().minute -
                            client.lastPing.minute) > 2:
                        print(
                            "%s SERVER no PING from %s in 2 minutes. Disconnecting"
                            % (datetime.datetime.now(), repr(client.username)))
                        client.disconnect = True

        print("%s SERVER Thread shutting down" % datetime.datetime.now())
        client.disconnect = True
        break