Advertisement
erktheerk

NSALeaksBot

Jul 1st, 2014
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.14 KB | None | 0 0
  1. #/u/GoldenSights
  2. import praw # simple interface to the reddit API, also handles rate limiting of requests
  3. import time
  4. import datetime
  5. import sqlite3
  6.  
  7. '''USER CONFIGURATION'''
  8.  
  9. USERNAME  = "NSAleaksBot"
  10. #This is the bot's Username. In order to send mail, he must have some amount of Karma.
  11. PASSWORD  = ""
  12. #This is the bot's Password.
  13. USERAGENT = "/r/NSALeaks content bot Maintained by /u/erktherk"
  14. #This is a short description of what the bot does. For example "/u/GoldenSights' Newsletter bot"
  15. SUBREDDIT = "all"
  16. #"Snowden+news+restoretheforth+worldnews+technology+privacy+news+wikileaks+politics+politic+worldpolitics+firstlook+inthenews"
  17. #This is the sub or list of subs to scan for new posts. For a single sub, use "sub1". For multiple subs, use "sub1+sub2+sub3+...". For all use "all"
  18. KEYWORDS = [" NSA", "NSA " "Snowden", "Greenwald", "Edward Snowden", "Glenn Greenwald"]
  19. #Any comment containing these words will be saved.
  20. KEYDOMAINS = [""]
  21.  
  22. SUBDUMP = True
  23. #Do you want the bot to dump into a subreddit as posts? Use True or False (Use capitals! No quotations!)
  24. DSUB = "NSALeaksBot"
  25. #If SUBDUMP is set to True, you will need to choose a subreddit to submit to.
  26. ALLOWSELF = False
  27. #Do you want the bot to dump selfposts? Use True or False (Use capitals! No quotations!)
  28. DISTINGUISHPOST = False
  29. #Do you want the bot to mod-distinguish the post?
  30. DISTINGUISHCOMMENT = False
  31. #Do you want the bot to mod-distinguish the comment on the dump?
  32. TITLE = ""
  33. #This is the title of the submission to your dump
  34. #Leave blank to keep the original post's title
  35. #Otherwise, use these injectors to create dynamic title:
  36. #_subreddit_ = subreddit of original post
  37. #_author_ = OP of original post
  38. #_ptitle_ = Title of original post
  39.  
  40. NSUB = "NSALeaksBot+NSALeaks+NSA"
  41. #This is the subreddit that will undergo the Other Discussions test.
  42. DELAY = 10
  43. #How many SECONDS old must a post be on your sub before it receives an Other Discussion box?
  44. DISTINGUISHN = False
  45. #Do you want the bot to mod-distinguish the comment?
  46. HEADER = "**Other Discussions on reddit:**\n\nSubreddit | Author | Post | Comments | Time\n:- | - | - | -: | -:\n"
  47. #This will be at the top of the Other Discussions comment. This formatting will prepare the box.
  48.  
  49.  
  50. MAXPOSTS = 100
  51. #This is how many posts you want to retrieve all at once. PRAW can download 100 at a time.
  52. WAIT = 20
  53. #This is how many seconds you will wait between cycles. The bot is completely inactive during this time.
  54.  
  55. EDITPASTMAX = 172800
  56. #5184000
  57. #How far back do you want to go when editing posts, in seconds?
  58. #A value of 172800 will only edit comments that are less than 2 days old
  59.  
  60. DOMAINBLACKLIST = ['dailycaller.com']
  61.  
  62.  
  63. '''All done!'''
  64.  
  65.  
  66.  
  67.  
  68. WAITS = str(WAIT)
  69. try:
  70.     import bot #This is a file in my python library which contains my Bot's username and password. I can push code to Git without showing credentials
  71.     USERNAME = bot.uG
  72.     PASSWORD = bot.pG
  73.     USERAGENT = bot.aG
  74. except ImportError:
  75.     pass
  76.  
  77. sql = sqlite3.connect('sql.db')
  78. print('Loaded SQL Database')
  79. cur = sql.cursor()
  80.  
  81. cur.execute('CREATE TABLE IF NOT EXISTS oldposts(ID TEXT)')
  82. cur.execute('CREATE TABLE IF NOT EXISTS oldlinks(ID TEXT)')
  83. print('Loaded Completed table')
  84.  
  85. sql.commit()
  86.  
  87. print('Logging in')
  88. r = praw.Reddit(USERAGENT)
  89. r.login(USERNAME, PASSWORD)
  90.  
  91.  
  92. def getTime(bool):
  93.     timeNow = datetime.datetime.now(datetime.timezone.utc)
  94.     timeUnix = timeNow.timestamp()
  95.     if bool == False:
  96.         return timeNow
  97.     else:
  98.         return timeUnix
  99.  
  100. def scanSub():
  101.     print('Searching '+ SUBREDDIT + '.')
  102.     subreddit = r.get_subreddit(SUBREDDIT)
  103.     posts = subreddit.get_new(limit=MAXPOSTS)
  104.     for post in posts:
  105.         pid = post.id
  106.         plink = post.permalink
  107.         if not post.is_self or ALLOWSELF == True:
  108.             ptitle = post.title
  109.             purl = post.url
  110.             cur.execute('SELECT * FROM oldposts WHERE ID=?', [pid])
  111.             if not cur.fetchone():
  112.                 cur.execute('SELECT * FROM oldlinks WHERE ID=?', [purl])
  113.                 if not cur.fetchone():
  114.                     if (KEYWORDS == [] or any(key.lower() in ptitle.lower() for key in KEYWORDS)) and (KEYDOMAINS == [] or any(key.lower() in purl.lower() for key in KEYDOMAINS)):
  115.                         try:
  116.                             pauthor = post.author.name
  117.                             psub = post.subreddit.display_name
  118.                             print(pid + ', ' + pauthor)
  119.                             if TITLE == "":
  120.                                 newtitle = ptitle
  121.                             else:
  122.                                 newtitle = TITLE
  123.                                 newtitle = newtitle.replace('_ptitle_', ptitle)
  124.                                 newtitle = newtitle.replace('_author_', pauthor)
  125.                                 newtitle = newtitle.replace('_subreddit_', psub)
  126.  
  127.                             if len(newtitle) > 300:
  128.                                 newtitle = newtitle[:297] + '...'
  129.  
  130.                             if SUBDUMP == True:
  131.                                 print('\tDumping to ' + DSUB)
  132.                                 try:
  133.                                     create = r.submit(DSUB, newtitle, url=purl, captcha = None)
  134.                                 except praw.errors.AlreadySubmitted:
  135.                                     print('Error: Already Submitted. Skipping...')
  136.                                 print('\tCreated post ' + create.id)
  137.                                 if DISTINGUISHPOST == True:
  138.                                     print('\tDistinguishing post')
  139.                                     create.distinguish()
  140.                         except AttributeError:
  141.                             print(pid + ': Author deleted. Ignoring')
  142.                     cur.execute('INSERT INTO oldposts VALUES(?)', [pid])
  143.                     cur.execute('INSERT INTO oldlinks VALUES(?)', [purl])
  144.                 else:
  145.                     print(pid + ': Already linked somewhere else')  
  146.         else:
  147.             print(pid + ': Ignoring selfpost')
  148.             cur.execute('INSERT INTO oldposts VALUES(?)', [pid])
  149.        
  150.         sql.commit()
  151.  
  152. def discussions():
  153.     print('\nScanning ' + NSUB)
  154.     subreddit = r.get_subreddit(NSUB)
  155.     posts = subreddit.get_new(limit=MAXPOSTS)
  156.     for post in posts:
  157.         result = []
  158.         pid = post.id
  159.         cur.execute('SELECT * FROM oldposts WHERE ID=?', [pid])
  160.         if not cur.fetchone():
  161.             ptime = post.created_utc
  162.             curtime = getTime(True)
  163.             if curtime - ptime > DELAY:
  164.                 generatebox(post, result)
  165.                 if len(result) > 0:
  166.                     final = HEADER + '\n'.join(result)
  167.                     if len(final) < 10000:
  168.                         print('\tCreating comment')
  169.                         post.add_comment(final)
  170.                         cur.execute('INSERT INTO oldposts VALUES(?)', [pid])
  171.                 else:
  172.                     print('\tNo results!')
  173.                 if post.is_self:
  174.                     cur.execute('INSERT INTO oldposts VALUES(?)', [pid])
  175.             else:
  176.                 print(pid + ': Too young')
  177.         sql.commit()
  178.  
  179. def editpast():
  180.     print('\nUpdating previous comments')
  181.     user = r.get_redditor(USERNAME)
  182.     comments = user.get_comments(limit=MAXPOSTS)
  183.     for comment in comments:
  184.         result = []
  185.         ctime = comment.created_utc
  186.         curtime = getTime(True)
  187.         if curtime - ctime > EDITPASTMAX:
  188.             print('\tReached end')
  189.             break
  190.         else:
  191.             print(comment.id)
  192.             post = comment.submission
  193.             generatebox(post, result)
  194.             if len(result) > 0:
  195.                 final = HEADER + '\n'.join(result)
  196.                 if len(final) < 10000:
  197.                     if final != comment.body:
  198.                         print('\tUpdating')
  199.                         comment.edit(final)
  200.                     else:
  201.                         print('\tDoes not need updating')
  202.             else:
  203.                 print('\tNone!')
  204.  
  205.  
  206. def generatebox(post, result):
  207.     purl = post.url
  208.     print(post.id + ': Trying OD box')
  209.     if not any(domain.lower() in purl.lower() for domain in DOMAINBLACKLIST):
  210.         search = r.search('url:"' + purl + '"', sort='new', limit=None)
  211.         for item in search:
  212.             if item.id != post.id:
  213.                 timestamp = item.created_utc
  214.                 timestamp = datetime.datetime.utcfromtimestamp(int(timestamp)).strftime("%A %B %d, %Y %H:%M UTC")
  215.                 try:
  216.                     iauthor = item.author.name
  217.                 except AttributeError:
  218.                     pauthor = '[deleted]'
  219.                 subreddit = '/r/' + item.subreddit.display_name
  220.                 ilink = item.permalink
  221.                 ilink = ilink.replace('http://www', 'http://np')
  222.                 result.append(subreddit + ' | ' + iauthor + ' | [post](' + ilink + ') | ' + str(item.num_comments) + ' | ' + timestamp)
  223.  
  224.  
  225.  
  226.  
  227. while True:
  228.     try:
  229.         scanSub()
  230.         discussions()
  231.         editpast()
  232.     except Exception as e:
  233.         print('An error has occured:', str(e))
  234.     print('Running again in ' + WAITS + ' seconds \n')
  235.     sql.commit()
  236.     time.sleep(WAIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement