/ blottertrax / services / soundcloud.py
soundcloud.py
 1  import requests
 2  
 3  from blottertrax.config import Config
 4  from blottertrax.helper.array_util import ArrayUtil
 5  from blottertrax.services.threshold_service import ThresholdService
 6  from blottertrax.value_objects.parsed_submission import ParsedSubmission
 7  from blottertrax.value_objects.service_result import ThresholdServiceResult
 8  
 9  
10  class Soundcloud(ThresholdService):
11      config: Config = Config()
12      Threshold = config.SOUNDCLOUD.THRESHOLD
13  
14      def get_service_result(self, parsed_submission: ParsedSubmission) -> ThresholdServiceResult:
15          url = parsed_submission.get_final_url()
16          if 'soundcloud.com' not in url:
17              return ThresholdServiceResult.error()
18  
19          response = requests.get(
20              'https://api.soundcloud.com/resolve.json?url=' + url + '&client_id=' + self.config.SOUNDCLOUD.KEY
21          )
22  
23          if response.status_code != 200:
24              return ThresholdServiceResult.error()
25  
26          data = response.json()
27  
28          if ArrayUtil.safe_list_get(data, False, 'playback_count') is False:
29              return ThresholdServiceResult.error()
30  
31          return ThresholdServiceResult(data['playback_count'] > self.Threshold, data['playback_count'], self.Threshold,
32                                        'Soundcloud plays')
33  
34      def requires_fully_parsed_submission(self):
35          return False