reddit.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. Reddit
  3. @website https://www.reddit.com/
  4. @provide-api yes (https://www.reddit.com/dev/api)
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title, content, thumbnail, publishedDate
  9. """
  10. import json
  11. from cgi import escape
  12. from urllib import urlencode
  13. from urlparse import urlparse
  14. from datetime import datetime
  15. # engine dependent config
  16. categories = ['general', 'images', 'news', 'social media']
  17. page_size = 25
  18. # search-url
  19. search_url = 'https://www.reddit.com/search.json?{query}'
  20. # do search-request
  21. def request(query, params):
  22. query = urlencode({'q': query,
  23. 'limit': page_size})
  24. params['url'] = search_url.format(query=query)
  25. return params
  26. # get response from search-request
  27. def response(resp):
  28. img_results = []
  29. text_results = []
  30. search_results = json.loads(resp.text)
  31. # return empty array if there are no results
  32. if 'data' not in search_results:
  33. return []
  34. posts = search_results.get('data', {}).get('children', [])
  35. # process results
  36. for post in posts:
  37. data = post['data']
  38. # extract post information
  39. params = {
  40. 'url': data['url'],
  41. 'title': data['title']
  42. }
  43. # if thumbnail field contains a valid URL, we need to change template
  44. thumbnail = data['thumbnail']
  45. url_info = urlparse(thumbnail)
  46. # netloc & path
  47. if url_info[1] != '' and url_info[2] != '':
  48. params['thumbnail_src'] = thumbnail
  49. params['template'] = 'images.html'
  50. img_results.append(params)
  51. else:
  52. created = datetime.fromtimestamp(data['created_utc'])
  53. params['content'] = escape(data['selftext'])
  54. params['publishedDate'] = created
  55. text_results.append(params)
  56. # show images first and text results second
  57. return img_results + text_results