Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import pycurl
  2. import re
  3. import py4chan
  4. from StringIO import StringIO
  5.  
  6. # Search image for testing
  7. searchImage_1 = 'http://i.4cdn.org/a/1419119790448.jpg'
  8.  
  9. # Search image that doesn't have best guess
  10. searchImage_2 = 'http://i.4cdn.org/a/1419089215532.jpg'
  11.  
  12. # GetBestGuess takes image url and returns Google Image Search's Best Guess
  13. def GetBestGuess(searchImage):
  14. # String variable to storing html output
  15. storage = StringIO()
  16. # cURL
  17. curl = pycurl.Curl()
  18. curl.setopt(pycurl.URL, "https://www.google.com/searchbyimage?&image_url="+searchImage)
  19. curl.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0')
  20. # Writes cURL output to storage instead of to stdout
  21. curl.setopt(curl.WRITEFUNCTION, storage.write)
  22. curl.setopt(pycurl.FOLLOWLOCATION, True)
  23. # Returns raw html. Not readable
  24. curl.perform()
  25. curl.close()
  26. response = re.search(re.escape("italic\">")+"(.*?)"+re.escape("</a>"),storage.getvalue())
  27. if response:
  28. return response.group(1)
  29. else:
  30. return "No Guess"
  31.  
  32. def GetBoardContents(inputBoard):
  33. board = py4chan.Board(inputBoard)
  34. threadList = []
  35. # 4chan Boards 404 after 19 getThread iterations, due to 4chan Boards only holding
  36. # up to 274 threads in a given Board.
  37.  
  38. # A suggested method to keep the memory usage down on this script would be to
  39. # read in only recent posts. Defining "recent" may be a bit tricky, as 4chan
  40. # boards are all different, but some common number should work.
  41. for x in range(2,19):
  42. if not threadList:
  43. threadList = board.getThread(x)
  44. else:
  45. threadList = threadList + board.getThreads(x)
  46.  
  47. threadSubjectList = []
  48. # Thread's first posts are known as "subjects". The subjects sometimes contain
  49. # a file(often picture related) or some text in an attempt to create a conversation.
  50. # This conversation starter is excellent to use when getting an idea of what a Thread
  51. # may be about.
  52. for thread in threadList:
  53. threadSubjectList.append(thread.topic.FileUrl)
  54.  
  55. return threadSubjectList
  56.  
  57. def main():
  58. pass
  59.  
  60. print GetBestGuess(searchImage_2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement