Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import os
- import sys
- import time
- import re
- import urllib
- import urllib2
- import cookielib
- import httplib
- COOKIE_FILE_TMPL = ".cookies/%s/cookie.txt"
- COOKIE_DIR_TMPL = ".cookies/"
- COMMENTS_DIR = "comments/"
- DEBUG = 0
- DEBUG_REPLY = 1
- def main_login(cj, ljuser, ljpass):
- #
- # load main page
- #
- reqUrl = 'http://www.livejournal.com/'
- f = urllib2.urlopen(reqUrl)
- data = f.read()
- if (DEBUG):
- print "MAIN PAGE"
- #print data
- #
- # request N1 (POST QUERY)
- #
- refererUrl = reqUrl
- reqUrl = 'https://www.livejournal.com/login.bml?ret=1'
- reqData = urllib.urlencode({'mode': 'login',
- 'user': ljuser,
- 'password': ljpass}
- )
- req = urllib2.Request(url=reqUrl, data=reqData)
- req.add_header('Referer', refererUrl)
- f = urllib2.urlopen(req)
- data = f.read()
- if (DEBUG):
- print "LIVEJOURNAL ANSWER\n"
- f = open("_login.txt", "w")
- f.write(data)
- f.close()
- #re_getbackurl = re.compile(r'livejournal.com/logout.bml\?ret=1&user=')
- re_getbackurl = re.compile(r'livejournal.com/logout.bml')
- m = re.search(re_getbackurl, data)
- if (m):
- print "Login success: " + ljuser
- else:
- print "Login fail, check login and pass"
- return "fail"
- return "ok"
- def main_thread_reply_one(cj, ljuser, ljpass, posturl, replytext):
- #
- # Load post
- #
- print posturl
- reqUrl = posturl
- f = urllib2.urlopen(reqUrl)
- data = f.read()
- if (DEBUG):
- print "LOAD POST"
- print data
- f = open("_post.txt", "w")
- f.write(data)
- f.close()
- #
- # Parse lj_form_auth
- #
- lj_form_auth = ""
- re_form_auth = re.compile(r' name=\\?"lj_form_auth\\?" value=\\?"([^"]+)\\?" ')
- m = re.search(re_form_auth, data, re.DOTALL|re.MULTILINE)
- if (m):
- lj_form_auth = m.group(1)
- if (DEBUG_REPLY):
- print "form auth: " + lj_form_auth
- else:
- print "livejournal lj_form_auth parsing error"
- return "fail"
- #
- # Parse chrp1
- #
- chrp1 = ""
- re_form_auth = re.compile(r' name=\\?"chrp1\\?" value=\\?"([^"]+)\\?" ')
- m = re.search(re_form_auth, data)
- if (m):
- chrp1 = m.group(1)
- if (DEBUG_REPLY):
- print "form chrp1: " + chrp1
- else:
- print "livejournal chrp1 parsing error"
- return "fail"
- #
- # Parse chal
- #
- chal = ""
- re_form_auth = re.compile(r" name='chal' id='login_chal' value='([^']+)' ")
- m = re.search(re_form_auth, data)
- if (m):
- chal = m.group(1)
- if (DEBUG_REPLY):
- print "form chal: " + chal
- else:
- #print "livejournal chal parsing error"
- #return "fail"
- # try2
- chal = ""
- re_form_auth = re.compile(r' name="chal" value="([^"]+)" ')
- m = re.search(re_form_auth, data)
- if (m):
- chal = m.group(1)
- if (DEBUG_REPLY):
- print "form chal: " + chal
- else:
- print "livejournal chal parsing error"
- return "fail"
- #
- # Parse itemid
- #
- itemid = ""
- re_form_auth = re.compile(r' name=\\?"itemid\\?" value=\\?"([^"]+)\\?" ')
- m = re.search(re_form_auth, data)
- if (m):
- itemid = m.group(1)
- if (DEBUG_REPLY):
- print "form itemid: " + itemid
- else:
- print "livejournal itemid parsing error"
- return "fail"
- #
- # Parse journal
- #
- journal = ""
- re_form_auth = re.compile(r' name=\\?"journal\\?" value=\\?"([^"]+)\\?" ')
- m = re.search(re_form_auth, data)
- if (m):
- journal = m.group(1)
- if (DEBUG_REPLY):
- print "form journal: " + journal
- else:
- print "livejournal 'journal' parsing error"
- return "fail"
- #
- # Parse parenttalkid
- #
- parenttalkid = ""
- re_form_auth = re.compile(r' name=\\?"parenttalkid\\?" value=\\?"([^"]+)\\?" ')
- m = re.search(re_form_auth, data)
- if (m):
- parenttalkid = m.group(1)
- if (DEBUG_REPLY):
- print "form parenttalkid: " + parenttalkid
- else:
- print "livejournal 'parenttalkid' parsing error"
- return "fail"
- #
- #
- #
- print "form data parsed"
- #
- # convert ljuser to lowercase and "-" to "_"
- #
- ljuser=ljuser.lower()
- ljuser=ljuser.replace("-", "_");
- #
- # request N2 (POST) replyto
- #
- replyto = parenttalkid
- refererUrl = reqUrl
- reqUrl = 'http://www.livejournal.com/talkpost_do.bml'
- reqData = urllib.urlencode({
- 'lj_form_auth': lj_form_auth,
- 'chal': chal,
- 'response': "",
- 'replyto': replyto,
- 'parenttalkid': parenttalkid,
- 'itemid': itemid,
- 'journal': journal,
- 'stylemine': "",
- 'editid': "0",
- 'talkpost_do': "0",
- 'chrp1': chrp1,
- 'json': "1",
- 'usertype': "cookieuser",
- 'cookieuser': ljuser,
- 'userpost': "",
- 'password': "",
- 'do_login': "",
- 'openid:url': "",
- 'oiddo_login': "",
- 'subject': "",
- 'subjecticon': "none",
- 'prop_picture_keyword': "",
- 'body': replytext
- })
- req = urllib2.Request(url=reqUrl, data=reqData)
- req.add_header('Referer', refererUrl)
- f = urllib2.urlopen(req)
- data = f.read()
- print "form data send"
- f = open("_replyresp.txt", "w")
- f.write(data)
- f.close()
- if (DEBUG):
- print "REPLY POST ANSWER\n"
- print data
- print "Comment post reply:"
- print data.strip()
- #
- # checking return data
- #
- #
- # should be like
- # xdreceiver.html?type=commentator%2Fsubmit&status=redirect
- #
- re_find = re.compile(r'xdreceiver.html\?type=commentator%2Fsubmit&status=redirect')
- m = re.search(re_find, data)
- if (m):
- print "Now comment posted, success!"
- else:
- print "livejournal reply response error"
- return "fail"
- return "ok"
- #
- # Main process code
- #
- def main_process(ljuser, ljpass, community, posturl):
- #
- # make ".cookies" dir
- #
- cookies_dir = os.path.join(os.getcwd(), COOKIE_DIR_TMPL)
- if (not(os.path.exists(cookies_dir))):
- os.mkdir(cookies_dir)
- #
- # prepare request cookies
- #
- COOKIE_FILE_PATH = COOKIE_FILE_TMPL % ljuser
- cookie_file = os.path.join(os.getcwd(), COOKIE_FILE_PATH)
- cj = cookielib.CookieJar()
- cj = cookielib.MozillaCookieJar()
- if (not(os.path.exists(cookie_file))):
- if (not(os.path.exists(os.path.dirname(cookie_file)))):
- os.mkdir(os.path.dirname(cookie_file))
- cj.save(cookie_file)
- cj.load(cookie_file)
- opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
- opener.addheaders = [('User-agent', 'Mozilla/5.0')]
- urllib2.install_opener(opener)
- #
- # Login
- #
- ret = main_login(cj, ljuser, ljpass)
- if ret == "fail":
- print "Login failed"
- exit(1)
- print "Sending comments in community: " + community
- if (1):
- #poster = "test7"
- #data = "some comment7"
- #replytext = "<b>" + poster + "</b>: " + data
- replytext = "Хотя бы газовый баллончик иметь, перцовый."
- #print poster
- print replytext
- ret = main_thread_reply_one(cj, ljuser, ljpass, posturl, replytext)
- if ret == "ok":
- print "Reply successful"
- else:
- print "Reply failed"
- #
- # Saving state for analyze
- #
- print "All done!"
- return
- def main():
- community = "http://ru-psiholog-svu.livejournal.com/"
- posturl = "http://ru-psiholog.livejournal.com/5486401.html?replyto=290311489"
- #posturl = "http://ru-psiholog-svu.livejournal.com/937.html?replyto=1193"
- #posturl = "http://ru-psiholog-svu.livejournal.com/937.html?replyto=937"
- ljuser="deniska2014"
- ljpass="XXXXXXXXX"
- main_process(ljuser, ljpass, community, posturl)
- return
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement