Advertisement
Guest User

send_comments.py

a guest
Aug 29th, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import sys
  6. import time
  7. import re
  8. import urllib
  9. import urllib2
  10. import cookielib
  11. import httplib
  12.  
  13.  
  14. COOKIE_FILE_TMPL = ".cookies/%s/cookie.txt"
  15. COOKIE_DIR_TMPL = ".cookies/"
  16.  
  17. COMMENTS_DIR = "comments/"
  18.  
  19. DEBUG = 0
  20. DEBUG_REPLY = 1
  21.  
  22.  
  23.  
  24. def main_login(cj, ljuser, ljpass):
  25.  
  26. #
  27. # load main page
  28. #
  29.  
  30. reqUrl = 'http://www.livejournal.com/'
  31. f = urllib2.urlopen(reqUrl)
  32. data = f.read()
  33. if (DEBUG):
  34. print "MAIN PAGE"
  35. #print data
  36.  
  37. #
  38. # request N1 (POST QUERY)
  39. #
  40.  
  41. refererUrl = reqUrl
  42. reqUrl = 'https://www.livejournal.com/login.bml?ret=1'
  43. reqData = urllib.urlencode({'mode': 'login',
  44. 'user': ljuser,
  45. 'password': ljpass}
  46. )
  47. req = urllib2.Request(url=reqUrl, data=reqData)
  48. req.add_header('Referer', refererUrl)
  49. f = urllib2.urlopen(req)
  50. data = f.read()
  51.  
  52. if (DEBUG):
  53. print "LIVEJOURNAL ANSWER\n"
  54. f = open("_login.txt", "w")
  55. f.write(data)
  56. f.close()
  57.  
  58. #re_getbackurl = re.compile(r'livejournal.com/logout.bml\?ret=1&user=')
  59. re_getbackurl = re.compile(r'livejournal.com/logout.bml')
  60. m = re.search(re_getbackurl, data)
  61. if (m):
  62. print "Login success: " + ljuser
  63. else:
  64. print "Login fail, check login and pass"
  65. return "fail"
  66.  
  67. return "ok"
  68.  
  69.  
  70. def main_thread_reply_one(cj, ljuser, ljpass, posturl, replytext):
  71.  
  72. #
  73. # Load post
  74. #
  75.  
  76. print posturl
  77.  
  78. reqUrl = posturl
  79. f = urllib2.urlopen(reqUrl)
  80. data = f.read()
  81. if (DEBUG):
  82. print "LOAD POST"
  83. print data
  84.  
  85.  
  86. f = open("_post.txt", "w")
  87. f.write(data)
  88. f.close()
  89.  
  90.  
  91. #
  92. # Parse lj_form_auth
  93. #
  94.  
  95. lj_form_auth = ""
  96. re_form_auth = re.compile(r' name=\\?"lj_form_auth\\?" value=\\?"([^"]+)\\?" ')
  97. m = re.search(re_form_auth, data, re.DOTALL|re.MULTILINE)
  98. if (m):
  99. lj_form_auth = m.group(1)
  100. if (DEBUG_REPLY):
  101. print "form auth: " + lj_form_auth
  102. else:
  103. print "livejournal lj_form_auth parsing error"
  104. return "fail"
  105.  
  106.  
  107. #
  108. # Parse chrp1
  109. #
  110.  
  111. chrp1 = ""
  112. re_form_auth = re.compile(r' name=\\?"chrp1\\?" value=\\?"([^"]+)\\?" ')
  113. m = re.search(re_form_auth, data)
  114. if (m):
  115. chrp1 = m.group(1)
  116. if (DEBUG_REPLY):
  117. print "form chrp1: " + chrp1
  118. else:
  119. print "livejournal chrp1 parsing error"
  120. return "fail"
  121.  
  122.  
  123. #
  124. # Parse chal
  125. #
  126.  
  127. chal = ""
  128. re_form_auth = re.compile(r" name='chal' id='login_chal' value='([^']+)' ")
  129. m = re.search(re_form_auth, data)
  130. if (m):
  131. chal = m.group(1)
  132. if (DEBUG_REPLY):
  133. print "form chal: " + chal
  134. else:
  135. #print "livejournal chal parsing error"
  136. #return "fail"
  137.  
  138. # try2
  139. chal = ""
  140. re_form_auth = re.compile(r' name="chal" value="([^"]+)" ')
  141. m = re.search(re_form_auth, data)
  142. if (m):
  143. chal = m.group(1)
  144. if (DEBUG_REPLY):
  145. print "form chal: " + chal
  146. else:
  147. print "livejournal chal parsing error"
  148. return "fail"
  149.  
  150.  
  151. #
  152. # Parse itemid
  153. #
  154.  
  155. itemid = ""
  156. re_form_auth = re.compile(r' name=\\?"itemid\\?" value=\\?"([^"]+)\\?" ')
  157. m = re.search(re_form_auth, data)
  158. if (m):
  159. itemid = m.group(1)
  160. if (DEBUG_REPLY):
  161. print "form itemid: " + itemid
  162. else:
  163. print "livejournal itemid parsing error"
  164. return "fail"
  165.  
  166.  
  167. #
  168. # Parse journal
  169. #
  170.  
  171. journal = ""
  172. re_form_auth = re.compile(r' name=\\?"journal\\?" value=\\?"([^"]+)\\?" ')
  173. m = re.search(re_form_auth, data)
  174. if (m):
  175. journal = m.group(1)
  176. if (DEBUG_REPLY):
  177. print "form journal: " + journal
  178. else:
  179. print "livejournal 'journal' parsing error"
  180. return "fail"
  181.  
  182.  
  183. #
  184. # Parse parenttalkid
  185. #
  186.  
  187. parenttalkid = ""
  188. re_form_auth = re.compile(r' name=\\?"parenttalkid\\?" value=\\?"([^"]+)\\?" ')
  189. m = re.search(re_form_auth, data)
  190. if (m):
  191. parenttalkid = m.group(1)
  192. if (DEBUG_REPLY):
  193. print "form parenttalkid: " + parenttalkid
  194. else:
  195. print "livejournal 'parenttalkid' parsing error"
  196. return "fail"
  197.  
  198. #
  199. #
  200. #
  201.  
  202. print "form data parsed"
  203.  
  204. #
  205. # convert ljuser to lowercase and "-" to "_"
  206. #
  207.  
  208. ljuser=ljuser.lower()
  209. ljuser=ljuser.replace("-", "_");
  210.  
  211. #
  212. # request N2 (POST) replyto
  213. #
  214.  
  215. replyto = parenttalkid
  216.  
  217. refererUrl = reqUrl
  218. reqUrl = 'http://www.livejournal.com/talkpost_do.bml'
  219. reqData = urllib.urlencode({
  220. 'lj_form_auth': lj_form_auth,
  221. 'chal': chal,
  222. 'response': "",
  223. 'replyto': replyto,
  224. 'parenttalkid': parenttalkid,
  225. 'itemid': itemid,
  226. 'journal': journal,
  227. 'stylemine': "",
  228. 'editid': "0",
  229. 'talkpost_do': "0",
  230. 'chrp1': chrp1,
  231. 'json': "1",
  232. 'usertype': "cookieuser",
  233. 'cookieuser': ljuser,
  234. 'userpost': "",
  235. 'password': "",
  236. 'do_login': "",
  237. 'openid:url': "",
  238. 'oiddo_login': "",
  239. 'subject': "",
  240. 'subjecticon': "none",
  241. 'prop_picture_keyword': "",
  242. 'body': replytext
  243. })
  244.  
  245. req = urllib2.Request(url=reqUrl, data=reqData)
  246. req.add_header('Referer', refererUrl)
  247. f = urllib2.urlopen(req)
  248. data = f.read()
  249.  
  250. print "form data send"
  251.  
  252. f = open("_replyresp.txt", "w")
  253. f.write(data)
  254. f.close()
  255.  
  256. if (DEBUG):
  257. print "REPLY POST ANSWER\n"
  258. print data
  259.  
  260.  
  261. print "Comment post reply:"
  262. print data.strip()
  263.  
  264. #
  265. # checking return data
  266. #
  267.  
  268. #
  269. # should be like
  270. # xdreceiver.html?type=commentator%2Fsubmit&status=redirect
  271. #
  272.  
  273. re_find = re.compile(r'xdreceiver.html\?type=commentator%2Fsubmit&status=redirect')
  274. m = re.search(re_find, data)
  275. if (m):
  276. print "Now comment posted, success!"
  277. else:
  278. print "livejournal reply response error"
  279. return "fail"
  280.  
  281.  
  282. return "ok"
  283.  
  284.  
  285.  
  286. #
  287. # Main process code
  288. #
  289. def main_process(ljuser, ljpass, community, posturl):
  290.  
  291. #
  292. # make ".cookies" dir
  293. #
  294.  
  295. cookies_dir = os.path.join(os.getcwd(), COOKIE_DIR_TMPL)
  296. if (not(os.path.exists(cookies_dir))):
  297. os.mkdir(cookies_dir)
  298.  
  299. #
  300. # prepare request cookies
  301. #
  302.  
  303. COOKIE_FILE_PATH = COOKIE_FILE_TMPL % ljuser
  304. cookie_file = os.path.join(os.getcwd(), COOKIE_FILE_PATH)
  305.  
  306. cj = cookielib.CookieJar()
  307. cj = cookielib.MozillaCookieJar()
  308.  
  309. if (not(os.path.exists(cookie_file))):
  310. if (not(os.path.exists(os.path.dirname(cookie_file)))):
  311. os.mkdir(os.path.dirname(cookie_file))
  312. cj.save(cookie_file)
  313.  
  314. cj.load(cookie_file)
  315. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  316. opener.addheaders = [('User-agent', 'Mozilla/5.0')]
  317. urllib2.install_opener(opener)
  318.  
  319. #
  320. # Login
  321. #
  322.  
  323. ret = main_login(cj, ljuser, ljpass)
  324. if ret == "fail":
  325. print "Login failed"
  326. exit(1)
  327.  
  328.  
  329. print "Sending comments in community: " + community
  330.  
  331. if (1):
  332.  
  333. #poster = "test7"
  334. #data = "some comment7"
  335. #replytext = "<b>" + poster + "</b>: " + data
  336.  
  337. replytext = "Хотя бы газовый баллончик иметь, перцовый."
  338.  
  339. #print poster
  340. print replytext
  341.  
  342. ret = main_thread_reply_one(cj, ljuser, ljpass, posturl, replytext)
  343. if ret == "ok":
  344. print "Reply successful"
  345. else:
  346. print "Reply failed"
  347.  
  348.  
  349. #
  350. # Saving state for analyze
  351. #
  352.  
  353. print "All done!"
  354.  
  355. return
  356.  
  357.  
  358. def main():
  359.  
  360. community = "http://ru-psiholog-svu.livejournal.com/"
  361.  
  362. posturl = "http://ru-psiholog.livejournal.com/5486401.html?replyto=290311489"
  363.  
  364. #posturl = "http://ru-psiholog-svu.livejournal.com/937.html?replyto=1193"
  365. #posturl = "http://ru-psiholog-svu.livejournal.com/937.html?replyto=937"
  366.  
  367. ljuser="deniska2014"
  368. ljpass="XXXXXXXXX"
  369.  
  370. main_process(ljuser, ljpass, community, posturl)
  371.  
  372. return
  373.  
  374.  
  375. if __name__ == "__main__":
  376. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement