tumblr.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import requests
  2. import simplejson as json
  3. import time
  4. import urllib
  5. import re
  6. from bs4 import BeautifulSoup
  7. import urlparse
  8. import oauth2
  9. blog_uri = "http://api.tumblr.com/v2/blog/"
  10. user_uri = "http://api.tumblr.com/v2/user/info"
  11. request_token_url = 'http://www.tumblr.com/oauth/request_token'
  12. authorize_url = 'http://www.tumblr.com/oauth/authorize'
  13. access_token_url = 'http://www.tumblr.com/oauth/access_token'
  14. def simpleoauthurl(consumer_key, consumer_secret):
  15. #set consumer
  16. consumer = oauth2.Consumer(consumer_key, consumer_secret)
  17. #getting token
  18. client = oauth2.Client(consumer)
  19. resp, content = client.request(request_token_url, "GET")
  20. if int(resp['status']) != 200:
  21. raise Exception("Invalid response %s." % resp['status'])
  22. #parse content into dictionary
  23. request_token = dict(urlparse.parse_qsl(content))
  24. oauth_key = request_token['oauth_token']
  25. oauth_secret = request_token['oauth_token_secret']
  26. return authorize_url+"?oauth_token="+oauth_key+"&redirect_uri=http%3A%2F%2Flocalhost%3A8000/hackathon/tumblr"
  27. def getUserInfo(oauth_verifier):
  28. ''' Return user's information. '''
  29. return "getUserInfo()"
  30. def getBlogInfo(user, consumer_key):
  31. ''' Return blogger's blog information. '''
  32. blog_info = blog_uri + user +".tumblr.com/info?api_key="+consumer_key
  33. req = requests.get(blog_info)
  34. jsonlist = json.loads(req.content)
  35. meta = jsonlist['meta']
  36. response = jsonlist['response']
  37. blog = response['blog']
  38. blog['updated'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(blog['updated']))
  39. return blog
  40. def getTaggedInfo(tag, consumer_key):
  41. ''' Return tags related to blog with certain tag. '''
  42. tagged_uri = "http://api.tumblr.com/v2/tagged?tag="+tag+"&api_key="+consumer_key+"&limit=20"
  43. req = requests.get(tagged_uri)
  44. jsonlist = json.loads(req.content)
  45. tags = []
  46. meta = jsonlist['meta']
  47. body = jsonlist['response']
  48. for blog in body:
  49. for data in blog:
  50. if data == "tags":
  51. #print blog[data]
  52. for i in blog[data]:
  53. m = re.match("(.*)(s*)s(t*)t(a*)a(r*)r(b*)b(u*)u(c*)c(k*)k(.*)", i.lower())
  54. if not m:
  55. tags.append(i)
  56. return tags
  57. def getTaggedBlog(tag, consumer_key):
  58. ''' Return the tagged blogs's captions or post.'''
  59. tagged_uri = "http://api.tumblr.com/v2/tagged?tag="+tag+"&api_key="+consumer_key+"&limit=2"
  60. req = requests.get(tagged_uri)
  61. jsonlist = json.loads(req.content)
  62. meta = jsonlist['meta']
  63. body = jsonlist['response']
  64. tagtext = []
  65. for blog in body:
  66. #print "####"
  67. for data in blog:
  68. #post
  69. if data == "body":
  70. if blog[data]:
  71. #print blog[data]
  72. soup = BeautifulSoup(blog[data])
  73. text = soup.get_text()
  74. tagtext.append(text)
  75. #an image
  76. if data == "caption":
  77. if blog[data]:
  78. #print blog[data]
  79. soup = BeautifulSoup(blog[data])
  80. text = soup.get_text()
  81. tagtext.append(text)
  82. return tagtext