1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | #!/usr/bin/python import os import sys import re import urllib import urllib2 import cookielib import httplib # # config start # username = "your-twitter-username" password = "your-twitter-password" # # config end # COOKIE_FILE = ".cookie/cookies.txt" DEBUG = 0 def main_connect(): # # prepare request cookies # cookie_file = os.path.join(os.getcwd(), COOKIE_FILE) 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) # # main site # reqUrl = 'http://twite.ru/' f = urllib2.urlopen(reqUrl) cj.save(cookie_file) data = f.read() if (DEBUG): print "MAIN SITE" print data # # auth selection # refererUrl = reqUrl reqUrl = 'http://www.twite.ru/blogger' req = urllib2.Request(url=reqUrl) req.add_header('Referer', refererUrl) f = urllib2.urlopen(req) cj.save(cookie_file) data = f.read() if (DEBUG): print "AUTH SELECT" print data # # Check if we already logged # re_loginfail = re.compile(r'class="blogger_info"') m = re.search(re_loginfail, data) if (m): #print "We are already logged" return "ok" print "!!!" print "We need re-login" print "!!!" # # redirect on twitter # refererUrl = reqUrl reqUrl = 'http://www.twite.ru/blogger/twitter_auth' req = urllib2.Request(url=reqUrl) req.add_header('Referer', refererUrl) f = urllib2.urlopen(req) cj.save(cookie_file) data = f.read() if (DEBUG): print "REDIR ON TWITTER" print data # # Parse "authenticity_token" and "oauth_token" # re_token_auth = re.compile(r'<input name="authenticity_token" type="hidden" value="(\w+)" />') m = re.search(re_token_auth, data) if (m): token_auth = m.group(1) if (DEBUG): print "token auth: " + token_auth else: print "twitter login page parsing error on authenticity_token" print "or redirection on twitter was failed. So, just try a bit later." return "fail" re_token_oauth = re.compile(r'<input id="oauth_token" name="oauth_token" type="hidden" value="(\w+)" />') m = re.search(re_token_oauth, data) if (m): token_oauth = m.group(1) if (DEBUG): print "token oauth: " + token_oauth else: print "twitter login page parsing error on oauth_token" return "fail" # # get url from 302 location # reqUrl = f.geturl() if (DEBUG): print "REDIR URL: " + reqUrl # # request N1 (POST QUERY) # refererUrl = reqUrl reqUrl = 'http://api.twitter.com/oauth/authorize' reqData = urllib.urlencode({'authenticity_token': token_auth, 'oauth_token': token_oauth, 'session[username_or_email]': username, 'session[password]': password} ) req = urllib2.Request(url=reqUrl, data=reqData) req.add_header('Referer', refererUrl) f = urllib2.urlopen(req) cj.save(cookie_file) data = f.read() if (DEBUG): print "TWITTER ANSWER\n" print data re_getbackurl = re.compile(r'<meta http-equiv="refresh" content="0;url=([^"]+?)">') m = re.search(re_getbackurl, data) if (m): twite_backurl = m.group(1) if (DEBUG): print "Twite back url: " + twite_backurl else: print "getting twite back url error" return "fail" # # request N2 (Get back to twite) # reqUrl = twite_backurl req = urllib2.Request(url=reqUrl) f = urllib2.urlopen(req) cj.save(cookie_file) data = f.read() if (DEBUG): print "TWITE ANSWER\n" print data # # get url from 302 location # reqUrl = f.geturl() if (DEBUG): print "REDIR URL: " + reqUrl return "ok" def check_tweets(): # # prepare request cookies # cookie_file = os.path.join(os.getcwd(), COOKIE_FILE) 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) # # ajax request for tweets - "get_offers" # refererUrl = 'http://www.twite.ru/blogger' reqUrl = 'http://www.twite.ru/ajax/get_offers' req = urllib2.Request(url=reqUrl) req.add_header('Referer', refererUrl) f = urllib2.urlopen(req) cj.save(cookie_file) data = f.read() if (DEBUG): print "TWITE GET OFFERS" print data # # Parse answer here, if need # # # ajax request for blogger summary info - "blogger_info" # refererUrl = 'http://www.twite.ru/blogger' reqUrl = 'http://www.twite.ru/ajax/blogger_info' req = urllib2.Request(url=reqUrl) req.add_header('Referer', refererUrl) f = urllib2.urlopen(req) cj.save(cookie_file) data = f.read() if (DEBUG): print "TWITE BLOGGER INFO" print data # # Parse answer # return def main(): ret = main_connect() if ret == "ok": print "Login successful" else: print "Login failed" ret = check_tweets() if __name__ == "__main__": main() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #!/usr/bin/python import os import sys import re import urllib import urllib2 import cookielib import httplib import time import xmpp # # config start # xmpp_jid = 'jid-id-for-this-app@jabber.ru' xmpp_pwd = 'jid-pass' to = 'your-jid-for-receive-notification@jabber.ru' msg1 = 'We have a tweet for 3-star account' msg2 = 'Cookie expired for 3-star account' # # config end # COOKIE_FILE = ".cookie/cookies.txt" DEBUG = 0 OFFERSPATH = "offers" def send_msg(textforsend): jid = xmpp.protocol.JID(xmpp_jid) client = xmpp.Client(jid.getDomain(),debug=[]) client.connect() client.auth(jid.getNode(),str(xmpp_pwd),resource='xmpppy') client.send(xmpp.protocol.Message(to,textforsend)) client.disconnect() return def check_tweets(): # # prepare request cookies # cookie_file = os.path.join(os.getcwd(), COOKIE_FILE) 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/4.1')] urllib2.install_opener(opener) # # ajax request for tweets - "get_offers" # refererUrl = 'http://www.twite.ru/blogger' reqUrl = 'http://www.twite.ru/ajax/get_offers' req = urllib2.Request(url=reqUrl) req.add_header('Referer', refererUrl) f = urllib2.urlopen(req) cj.save(cookie_file) data = f.read() if (DEBUG): print "TWITE GET OFFERS" print data # # Parse answer # # check if cookie expired re_expired = re.compile(r'<a href="/exit">') m = re.search(re_expired, data) if (m): print "Cookie expired, needed relogin\n" send_msg(msg2) return # check for available tweets re_token_oauth = re.compile(r'<div id="no_offers">') m = re.search(re_token_oauth, data) if (m): print "No tweets" return else: print "We have active tweets!!!" # # Log offers # if (not(os.path.exists(OFFERSPATH))): os.mkdir(OFFERSPATH) dtime = time.strftime("%Y%m%d_%H%M%S",time.localtime()) f = open(OFFERSPATH+"/"+dtime+".txt",'w') f.write(data) f.close() # # Parse offers # # <div id="divoffer123456"> send_msg(msg1) return def main(): # # for run from cron script # pathname = os.path.dirname(sys.argv[0]) fullpath = os.path.abspath(pathname) os.chdir(fullpath) # # check ad tweets # ret = check_tweets() if __name__ == "__main__": main() |
twite_pub_release.zip
Интересные криминальные новости Ленинградской области. Оказывается там столько всего происходит, любопытно почитать.
One Response
Leave a Reply
<a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>