website_twitter.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import json
  4. import logging
  5. import requests
  6. from odoo import api, fields, models
  7. API_ENDPOINT = 'https://api.twitter.com'
  8. API_VERSION = '1.1'
  9. REQUEST_TOKEN_URL = '%s/oauth2/token' % API_ENDPOINT
  10. REQUEST_FAVORITE_LIST_URL = '%s/%s/favorites/list.json' % (API_ENDPOINT, API_VERSION)
  11. URLOPEN_TIMEOUT = 10
  12. _logger = logging.getLogger(__name__)
  13. class WebsiteTwitter(models.Model):
  14. _inherit = 'website'
  15. twitter_api_key = fields.Char(string='Twitter API key', help='Twitter API Key')
  16. twitter_api_secret = fields.Char(string='Twitter API secret', help='Twitter API Secret')
  17. twitter_screen_name = fields.Char(string='Get favorites from this screen name')
  18. @api.model
  19. def _request(self, website, url, params=None):
  20. """Send an authenticated request to the Twitter API."""
  21. access_token = self._get_access_token(website)
  22. try:
  23. request = requests.get(url, params=params, headers={'Authorization': 'Bearer %s' % access_token}, timeout=URLOPEN_TIMEOUT)
  24. request.raise_for_status()
  25. return request.json()
  26. except requests.HTTPError as e:
  27. _logger.debug("Twitter API request failed with code: %r, msg: %r, content: %r",
  28. e.response.status_code, e.response.reason, e.response.content)
  29. raise
  30. @api.model
  31. def _refresh_favorite_tweets(self):
  32. ''' called by cron job '''
  33. website = self.env['website'].search([('twitter_api_key', '!=', False),
  34. ('twitter_api_secret', '!=', False),
  35. ('twitter_screen_name', '!=', False)])
  36. _logger.debug("Refreshing tweets for website IDs: %r", website.ids)
  37. website.fetch_favorite_tweets()
  38. def fetch_favorite_tweets(self):
  39. WebsiteTweets = self.env['website.twitter.tweet']
  40. tweet_ids = []
  41. for website in self:
  42. if not all((website.twitter_api_key, website.twitter_api_secret, website.twitter_screen_name)):
  43. _logger.debug("Skip fetching favorite tweets for unconfigured website %s", website)
  44. continue
  45. params = {'screen_name': website.twitter_screen_name}
  46. last_tweet = WebsiteTweets.search([('website_id', '=', website.id),
  47. ('screen_name', '=', website.twitter_screen_name)],
  48. limit=1, order='tweet_id desc')
  49. if last_tweet:
  50. params['since_id'] = int(last_tweet.tweet_id)
  51. _logger.debug("Fetching favorite tweets using params %r", params)
  52. response = self._request(website, REQUEST_FAVORITE_LIST_URL, params=params)
  53. for tweet_dict in response:
  54. tweet_id = tweet_dict['id'] # unsigned 64-bit snowflake ID
  55. tweet_ids = WebsiteTweets.search([('tweet_id', '=', tweet_id)]).ids
  56. if not tweet_ids:
  57. new_tweet = WebsiteTweets.create(
  58. {
  59. 'website_id': website.id,
  60. 'tweet': json.dumps(tweet_dict),
  61. 'tweet_id': tweet_id, # stored in NUMERIC PG field
  62. 'screen_name': website.twitter_screen_name,
  63. })
  64. _logger.debug("Found new favorite: %r, %r", tweet_id, tweet_dict)
  65. tweet_ids.append(new_tweet.id)
  66. return tweet_ids
  67. def _get_access_token(self, website):
  68. """Obtain a bearer token."""
  69. r = requests.post(
  70. REQUEST_TOKEN_URL,
  71. data={'grant_type': 'client_credentials',},
  72. auth=(website.twitter_api_key, website.twitter_api_secret),
  73. timeout=URLOPEN_TIMEOUT,
  74. )
  75. r.raise_for_status()
  76. data = r.json()
  77. access_token = data['access_token']
  78. return access_token