tuankiet65

Yahoo Blog search

Dec 7th, 2012
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. # This tool was created by alard at https://gist.github.com/4fa302a54ea8b5aa5c28
  4.  
  5. import json
  6. import re
  7. import time
  8. import urllib
  9. from collections import deque
  10.  
  11. from tornado import ioloop, httpclient, gen
  12.  
  13. USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27"
  14.  
  15. class YahooBlogFinder(object):
  16.   BLOCK_TIMEOUT = 120
  17.   WAIT = 10
  18.  
  19.   def __init__(self, words_to_search, concurrent=1):
  20.     self.queue = deque([ (1,w) for w in words_to_search ])
  21.     self.running = 0
  22.     self.concurrent = concurrent
  23.     self.blogs_found = set()
  24.     self.http_client = httpclient.AsyncHTTPClient()
  25.     self.blocked_until = 0
  26.  
  27.   def start(self):
  28.     if self.blocked_until > time.time():
  29.       ioloop.IOLoop.instance().add_timeout(self.blocked_until, self.start)
  30.       return
  31.  
  32.     while len(self.queue) > 0 and self.running < self.concurrent:
  33.       begin, word = self.queue.popleft()
  34.       self.check_one(begin, word)
  35.  
  36.     if self.running == 0:
  37.       ioloop.IOLoop.instance().stop()
  38.  
  39.   def check_one(self, begin, word):
  40.     self.running += 1
  41.  
  42.     enc_word = urllib.quote(word.encode("utf-8"))
  43.  
  44.     url = ("http://vn.blog.search.yahoo.com/search?p=%s&fr=uh-yblog&n=100&b=%d" % (enc_word, begin))
  45.     print "(%s, %d) %s" % (word, begin, url)
  46.  
  47.     req = httpclient.HTTPRequest(
  48.         url,
  49.         connect_timeout=10, request_timeout=30,
  50.         use_gzip=True, user_agent=USER_AGENT)
  51.     req.begin = begin
  52.     req.word = word
  53.     self.http_client.fetch(req, self.handle_response)
  54.  
  55.   def handle_response(self, response):
  56.     word = response.request.word
  57.     begin = response.request.begin
  58.  
  59.     if response.error:
  60.       if response.error.code == 999:
  61.         print "(%s, %d): Error %d, blocked!" % (word, begin, response.error.code)
  62.         self.queue.append((begin, word))
  63.         self.blocked_until = time.time() + self.BLOCK_TIMEOUT
  64.  
  65.       else:
  66.         print "(%s, %d): Error %d" % (word, begin, response.error.code)
  67.         self.queue.append((begin, word))
  68.  
  69.     else:
  70.       for url in re.findall(r'http://blog.yahoo.com/([^/"\'&<>]+)', response.body):
  71.         self.blogs_found.add(url)
  72.  
  73.       if re.search(r'id="pg-next"', response.body):
  74.         self.queue.append(( begin+100, word ))
  75.  
  76.       print "Blogs found: %d" % len(self.blogs_found)
  77.  
  78.     self.running -= 1
  79.     ioloop.IOLoop.instance().add_timeout(time.time() + self.WAIT, self.start)
  80.  
  81.  
  82.  
  83.  
  84. print "Loading keywords..."
  85. http_client = httpclient.HTTPClient()
  86. res = http_client.fetch("http://tracker.archiveteam.org:8124/request", method="POST", body="")
  87. task = json.loads(res.body)
  88.  
  89. ybf = YahooBlogFinder(task["words"])
  90. ybf.start()
  91. ioloop.IOLoop.instance().start()
  92.  
  93. print "Submitting %d blogs." % len(ybf.blogs_found)
  94. json_body = json.dumps({ "id": task["id"], "found": [ a for a in ybf.blogs_found ], "done": task["words"] })
  95. res = http_client.fetch("http://tracker.archiveteam.org:8124/submit", method="POST",
  96.                         body=json_body, headers={"Content-Type": "application/json"})
Advertisement
Add Comment
Please, Sign In to add comment