Advertisement
indexnotfound

Scr

Mar 21st, 2021
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # THIS IS FOR EDUCATIONIAL PURPOSE ONLY.
  4. # DON'T BLAME ME IF YOU'RE BEING BANNED.
  5. # I WARNED YOU
  6.  
  7. # to use this script, you need to install simple_pastebin_parser with
  8. #       `sudo pip3 install simple_pastebin_parser --no-deps`
  9. #
  10.  
  11. try:
  12.     import simple_pastebin_parser as spp
  13. except ModuleNotFoundError:
  14.     print("You didn't read the manual!")
  15.     print()
  16.     print("to use this script, you need to install simple_pastebin_parser with: `sudo pip3 install simple_pastebin_parser --no-deps`")
  17.     exit(1)
  18.  
  19. if "/i/" not in spp.KNOWN_PREFIXES:
  20.     spp.KNOWN_PREFIXES.append("/i/")
  21.  
  22. import re,argparse,sys,pickle,hashlib
  23.  
  24. def setunk(strang,unkstr="unknown"):
  25.     if (strang == ""):
  26.         return unkstr
  27.     return strang
  28.  
  29. def contains(strang,search):
  30.     for word in search:
  31.         if word in strang:
  32.             return True
  33.     return False
  34.  
  35. def contains_regex(strang,search):
  36.     for regex in search:
  37.         if re.match(regex,strang):
  38.             return True
  39.     return False
  40.  
  41. class pbPaste:
  42.     def __init__(self,paste):
  43.         self.title = setunk(paste.Title,"Untitled")
  44.         self.author = setunk(paste.Author,"Unknown Author")
  45.         self.date = paste.Date
  46.         self.content = paste.Content.replace("\r\n","\n")
  47.         self.hash = hashlib.sha256(self.content.encode("utf-8")).hexdigest()
  48.         self.id = paste.id
  49.  
  50. def get_one_paste():
  51.     for paste in spp.get_pastes():
  52.         return pbPaste(paste)
  53.  
  54. def main(SEARCH=[""],verbose=True,output="",save=False):
  55.     if output:
  56.         append_write = "w"
  57.         if save:
  58.             append_write = "a"
  59.         outfile = open(output,append_write,newline="\n")
  60.     if (save and output):
  61.         try:
  62.             paste_array = pickle.load(open(output+".pickle","rb"))
  63.         except FileNotFoundError:
  64.             paste_array = []
  65.     else:
  66.         paste_array = []
  67.     try:
  68.         for paste in spp.get_pastes():
  69.             current = pbPaste(paste)
  70.             skip = False
  71.             for prev_paste in paste_array:
  72.                 if current.hash == prev_paste:
  73.                     skip = True
  74.                     break
  75.             if (contains_regex(current.title,SEARCH) or contains_regex(current.content,SEARCH)) and (not skip):
  76.                 paste_array.append(current.hash)
  77.                 title_len = 27+4+len(current.title)
  78.                 outstr = ""
  79.                 outstr += "="*title_len+"\n"*2
  80.                 outstr += "  found corresponding paste: "+current.title+"\n"*2
  81.                 outstr += title_len*"-"+"\n"*2
  82.                 outstr += current.content+"\n"*2
  83.                 outstr += title_len*"="+"\n"*5
  84.                 if verbose:
  85.                     print(outstr,end="")
  86.                 if output:
  87.                     outfile.write(outstr)
  88.         if output:
  89.             outfile.close()
  90.             print("Human-readable data saved as "+output)
  91.             if save:
  92.                 pickle.dump(paste_array,open(output+".pickle","wb"))
  93.                 print("Pickles available here: "+output+".pickle")
  94.         return 0
  95.     except KeyboardInterrupt:
  96.         print("\nSearch stopped by user.")
  97.         if output:
  98.             outfile.close()
  99.             print("Human-readable data saved as "+output)
  100.             if save:
  101.                 pickle.dump(paste_array,open(output+".pickle","wb"))
  102.                 print("Pickles available here: "+output+".pickle")
  103.         return 1
  104.     except:
  105.         if output:
  106.             outfile.close()
  107.             print("Human-readable data saved as "+output)
  108.             if save:
  109.                 pickle.dump(paste_array,open(output+".pickle","wb"))
  110.                 print("Pickles available here: "+output+".pickle")
  111.         raise
  112.  
  113.  
  114. if __name__ == "__main__":
  115.     print("Started pastebin scraper")
  116.     parser = argparse.ArgumentParser()
  117.     parser.add_argument("-v", "--verbose", help="display gathered data in terminal", action="store_true")
  118.     parser.add_argument("-o", "--output", help="output file")
  119.     parser.add_argument("-r", "--regex", help="regular expression to match")
  120.     parser.add_argument("-p", "--pickle", help="adds a file to save pickled data", action="store_true")
  121.     args = parser.parse_args()
  122.     arg_regex = ""
  123.     if (args.regex):
  124.         arg_regex = args.regex
  125.     exit_value = main(output=args.output,verbose=args.verbose,SEARCH=[arg_regex],save=args.pickle)
  126.     sys.exit(exit_value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement