Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import urllib
  4. import sys
  5. import re
  6. import time
  7. from yattag import Doc, indent
  8. import xml.etree.ElementTree
  9. import os
  10. import urllib.request as urllib2
  11. import random
  12. import string
  13. import adventure
  14. import io
  15.  
  16. url='https://boards.4chan.org/qa/thread/527093'
  17. print(url, '\n')
  18. tmpfile = io.open('finalarchive.txt', 'w+')
  19. tmpfile.write('WELCOME TO COLOSSAL CAVES!! WOULD YOU LIKE INSTRUCTIONS?\nMade by Josh with the the library of github/brandon-rhodes')
  20. tmpfile.close()
  21.  
  22. def gettoken(data):
  23. soup=BeautifulSoup(data, 'html.parser')
  24. out1=''
  25. for element in soup.find_all(name=True, value=True, type=True):
  26. if element['name'] == 'csrf_token_login':
  27. out1=element['value']
  28. continue
  29. return(out1)
  30.  
  31. def clean(text):
  32. return re.sub(r'[^\x00-\x7F]',' ', text)
  33.  
  34. def start():
  35. file = io.open('toUser.txt', 'w+')
  36. file.close()
  37. adventure.play()
  38. runFirstComponent()
  39.  
  40. def runThirdComponent(array):
  41. print('Went to Third Component.')
  42.  
  43. tmpfile = io.open('finalarchive.txt', 'r+')
  44. finarr = tmpfile.readlines()
  45. tmpfile.close()
  46.  
  47. # print(array)
  48.  
  49. final_array=[]
  50. for element in finarr:
  51. tmpvar=element
  52. if len(element) >= 1:
  53. final_array.append(element)
  54. for element in array:
  55. final_array.append(element)
  56.  
  57. with io.open("finalarchive.txt", "w+") as myfile:
  58. for element in final_array:
  59. myfile.write('%s\n' % element)
  60. myfile.close
  61.  
  62. nospace_final_array=[]
  63.  
  64. x=0
  65. for element in final_array:
  66. if len(element) > 1:
  67. nospace_final_array.append(element.replace('\n',''))
  68. x+=1
  69. finstr='\n'.join(nospace_final_array)
  70. print(finstr)
  71.  
  72. updatesession(finstr)
  73.  
  74. def runSecondComponent(array):
  75. print('Went to Second Component.')
  76. def readfile():
  77. tmpfile = io.open('output.txt', 'r+')
  78. tmparr = tmpfile.readlines()
  79. tmpfile.close()
  80. return tmparr
  81.  
  82. outarr=[]
  83. tmparr=[]
  84.  
  85. for command in array:
  86.  
  87. def error(command):
  88. tmparr.append('>' + command)
  89. tmparr.append('INVALID INPUT')
  90.  
  91. try:
  92. if (command not in ['quit', 'quit()', 'suspend', 'pause', 'save']):
  93. if (','not in command and '.' not in command and '/' not in command and '\\' not in command):
  94. pattern = re.compile('[^A-Z,a-z,(,)]')
  95. if pattern.findall(command):
  96. error(command)
  97. else:
  98. output='print(' + command + ')'
  99. exec(output)
  100. tmparr.append('>' + command)
  101. for line in readfile():
  102. tmparr.append(line)
  103. else:
  104. error(command)
  105. else:
  106. error(command)
  107. except Exception as e:
  108. error(command)
  109.  
  110. for element in tmparr:
  111. if element != '\n':
  112. outarr.append(element.replace('\n',''))
  113.  
  114. with io.open("toUser.txt", "a") as myfile:
  115. for element in outarr:
  116. myfile.write('%s\n' % element)
  117. myfile.close
  118.  
  119. return outarr
  120.  
  121. def runFirstComponent():
  122. print('Made it to First component')
  123.  
  124. while(True):
  125. headers='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36'
  126. try:
  127. req = urllib2.Request(url, headers={ 'User-Agent': headers })
  128. html = urllib2.urlopen(req, timeout=20).read()
  129. except Exception as e:
  130. print("Error: ", e)
  131. time.sleep(10)
  132. runFirstComponent()
  133.  
  134. html=clean((str)(html))
  135.  
  136. soup = soup = BeautifulSoup(html, "html.parser")
  137.  
  138. title=soup.html.head.title.text
  139.  
  140. doc, tag, text = Doc().tagtext()
  141.  
  142. with tag ('thread'):
  143. for posts in soup.findAll(('div'),{"class":"postContainer"}):
  144.  
  145. for reply in posts.findAll(('div'),{"class":"post reply"}):
  146. string=reply.find(('blockquote'),{"class":"postMessage"})
  147. string=(str)(string)
  148. string=re.sub('<[^>]+>', '\n',string)
  149. string=string.replace('&gt;', '>')
  150. array=string.splitlines()
  151. array=[x for x in array if x]
  152. array=list(map(str, array))
  153. with tag('post', name=posts.find(('span'),{"class":"postNum desktop"}).text[3:]):
  154. i=0
  155. for lines in array:
  156. with tag('line'):
  157. text(array[i].replace('$gt;', '>'))
  158. i+=1
  159.  
  160.  
  161. #prettying document up
  162. result = indent(
  163. doc.getvalue(),
  164. indentation = ' '*4,
  165. newline = '\r\n'
  166. )
  167.  
  168. result=result.replace('&gt;','>')
  169.  
  170.  
  171. # write xml to file
  172. file = io.open('output.xml', 'w+')
  173. file.write(result)
  174. file.close()
  175.  
  176.  
  177. e = xml.etree.ElementTree.parse('output.xml').getroot()
  178.  
  179. active=[]
  180.  
  181. #returns active posts in an array
  182. for posts in e.findall('post'):
  183. active.append((int)(posts.get('name')))
  184. newposts=active[:]
  185.  
  186. file=io.open('saved.txt', 'r')
  187. inactive=file.readlines()
  188. file.close()
  189. inactive=list(map(int, inactive))
  190.  
  191. s1=set(inactive)
  192. s2=set(active)
  193.  
  194. #remove posts found in saved.csv array
  195. for x in s1:
  196. for y in s2:
  197. if x == y:
  198. active.remove(x)
  199.  
  200. active=list(map(str, active))
  201. toProgram=[]
  202.  
  203. for post in e.findall('post'):
  204. for element in active:
  205. if (post.get('name')==element):
  206. i=0
  207. for line in post.findall('line'):
  208. try:
  209. tmp=line.text
  210. if len(tmp) < 30 and tmp[0] == ';':
  211. tmp=tmp[1:]
  212. toProgram.append(tmp)
  213. i+=1
  214. if (i == 8):
  215. break
  216. except:
  217. i+=0
  218. # writing new values to file
  219. file=io.open('saved.txt', 'w+')
  220. for posts in newposts:
  221. file.write((str)(posts) + '\n')
  222. file.close()
  223.  
  224. outarr = runSecondComponent(toProgram)
  225.  
  226. runThirdComponent(outarr)
  227.  
  228. sleep_time=random.randint(10,13)
  229. print('Sleeping for', sleep_time, ' seconds')
  230. time.sleep(sleep_time)
  231.  
  232. def updatesession(str):
  233. with requests.Session() as c:
  234.  
  235. headers='Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
  236.  
  237. url='http://www.pastebin.com/login'
  238. out = c.get(url, headers={'User-Agent': headers})
  239. tmp=out.content
  240. # tmp=tmp.replace('\n','')
  241. # information to include in the first POST message, for logging in
  242. csrf_token_login=gettoken(tmp)
  243. submit_hidden='submit_hidden'
  244. user_name='icarus42'
  245. user_password='f.u.'
  246. submit='Login'
  247. print(csrf_token_login)
  248.  
  249. # post information. You send your POST information, then after that GET the page
  250. login_data=dict(csrf_token_login=csrf_token_login, submit_hidden=submit_hidden, user_name=user_name, user_password=user_password, submit=submit)
  251.  
  252. try:
  253. c.post(url, data=login_data, headers={'Referer':url, 'User-Agent':headers})
  254.  
  255. logged_in_url='http://www.pastebin.com/u/icarus42'
  256. page=c.get(logged_in_url,headers={'User-Agent':headers})
  257. except Exception as e:
  258. print('[1] Exception', e)
  259.  
  260. paste_url='http://pastebin.com/edit/3t0WiT2q'
  261. out=c.get(paste_url, headers={'Referer':logged_in_url, 'User-Agent':headers})
  262. me=out.content
  263.  
  264. submit_hidden='submit_hidden'
  265. item_key='3t0WiT2q'
  266. csrf_token_3t0WiT2q=gettoken(me)
  267. paste_code=str
  268. paste_format='1'
  269. paste_expire_date='DNC'
  270. paste_private='0'
  271. paste_name='Colossal Caves'
  272. submit='Save+Changes'
  273.  
  274. submitted_data=dict(submit_hidden=submit_hidden, item_key=item_key,
  275. csrf_token_3t0WiT2q=csrf_token_3t0WiT2q, paste_code=paste_code,
  276. paste_format=paste_format, paste_expire_date=paste_expire_date,
  277. paste_private=paste_private, paste_name=paste_name, submit=submit)
  278.  
  279. try:
  280. c.post(paste_url, data=submitted_data, headers={'Referer':paste_url, 'User-Agent':headers})
  281.  
  282. finished_url='http://pastebin.com/3t0WiT2q'
  283. page=c.get(finished_url, headers={'Referer':paste_url, 'User-Agent':headers})
  284. except Exception as e:
  285. print('[2] Exception', e)
  286. print('Finished Downloading')
  287.  
  288. def start():
  289. adventure.play()
  290. runFirstComponent()
  291.  
  292. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement