Skip to content

Instantly share code, notes, and snippets.

@shoredata
Forked from dmarx/LinkFixerClone.py
Created July 13, 2018 20:56
Show Gist options
  • Save shoredata/dced31d82c110733280218d375302c96 to your computer and use it in GitHub Desktop.
Save shoredata/dced31d82c110733280218d375302c96 to your computer and use it in GitHub Desktop.

Revisions

  1. @dmarx dmarx revised this gist Nov 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LinkFixerClone.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import praw
    import praw # simple interface to the reddit API, also handles rate limiting of requests
    import re
    from collections import deque
    from time import sleep
  2. @dmarx dmarx revised this gist Nov 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LinkFixerClone.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    r = praw.Reddit(USERAGENT)
    r.login(USERNAME,PASSWORD) # necessary if your bot will talk to people

    cache = deque(maxlen=200)
    cache = deque(maxlen=200) # To make sure we don't duplicate effort

    r_pat = re.compile(' r/[A-Za-z0-9]+')
    u_pat = re.compile(' u/[A-Za-z0-9]+')
  3. @dmarx dmarx revised this gist May 9, 2013. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions LinkFixerClone.py
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,11 @@
    from collections import deque
    from time import sleep

    USERNAME = "Your username here"
    PASSWORD = "Your password here"
    USERNAME = "Your username here"
    PASSWORD = "Your password here"
    USERAGENT = "Your useragent string here. It should include your /u/username as a courtesy to reddit"

    r = praw.Reddit('A linkfixerbot clone (because the author was being mean and not sharing their source) by /u/shaggorama')
    r = praw.Reddit(USERAGENT)
    r.login(USERNAME,PASSWORD) # necessary if your bot will talk to people

    cache = deque(maxlen=200)
  4. @dmarx dmarx revised this gist May 9, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions LinkFixerClone.py
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@
    from collections import deque
    from time import sleep

    USERNAME = 'your username here'
    PASSOWRD = 'your password here'
    USERNAME = "Your username here"
    PASSWORD = "Your password here"

    r = praw.Reddit('A linkfixerbot clone (because the author was being mean and not sharing their source) by /u/shaggorama')
    r.login(USERNAME,PASSWORD) # necessary if your bot will talk to people
    @@ -26,7 +26,7 @@ def check_condition(comment):
    def bot_action(c, links):
    text = ''
    for link in links:
    text += "/" + link + "\n"
    text += "/" + link[1:] + "\n"
    print c.author.name, c.subreddit.display_name, c.submission.title
    print text
    c.reply(text)
  5. @dmarx dmarx revised this gist May 9, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions LinkFixerClone.py
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@

    cache = deque(maxlen=200)

    r_pat = re.compile('r/[A-Za-z0-9]+')
    u_pat = re.compile('u/[A-Za-z0-9]+')
    r_pat = re.compile(' r/[A-Za-z0-9]+')
    u_pat = re.compile(' u/[A-Za-z0-9]+')

    def check_condition(comment):
    text = comment.body
  6. @dmarx dmarx created this gist May 9, 2013.
    55 changes: 55 additions & 0 deletions LinkFixerClone.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import praw
    import re
    from collections import deque
    from time import sleep

    USERNAME = 'your username here'
    PASSOWRD = 'your password here'

    r = praw.Reddit('A linkfixerbot clone (because the author was being mean and not sharing their source) by /u/shaggorama')
    r.login(USERNAME,PASSWORD) # necessary if your bot will talk to people

    cache = deque(maxlen=200)

    r_pat = re.compile('r/[A-Za-z0-9]+')
    u_pat = re.compile('u/[A-Za-z0-9]+')

    def check_condition(comment):
    text = comment.body
    broken = set(re.findall(r_pat, text))
    broken.union( set(re.findall(u_pat, text)) )
    condition = False
    if broken:
    condition = True
    return condition, broken

    def bot_action(c, links):
    text = ''
    for link in links:
    text += "/" + link + "\n"
    print c.author.name, c.subreddit.display_name, c.submission.title
    print text
    c.reply(text)

    running = True
    while running:
    all = r.get_all_comments(limit = None, url_data = {'limit':100})
    for c in all:
    if c.id in cache:
    break
    cache.append(c.id)
    bot_condition_met, parsed = check_condition(c)
    if bot_condition_met:
    try:
    bot_action(c, parsed)

    except KeyboardInterrupt:
    running = False
    except praw.errors.APIException, e:
    print "[ERROR]:", e
    print "sleeping 30 sec"
    sleep(30)
    except Exception, e: # In reality you don't want to just catch everything like this, but this is toy code.
    print "[ERROR]:", e
    print "blindly handling error"
    continue