def fetch_paginated_entities(url, payload, pages_amount):
    response = handle_request(url, payload)
    entities = response['response']['items']

    if not pages_amount:
        pages_amount = calculate_pages_amount(response['response']['count'])

    for page_number in range(1, pages_amount):
        payload['offset'] = 100 * page_number
        response = handle_request(url, payload)

        entities += response['response']['items']

    return entities
Exemple #2
0
def get_reactions_from_post(post_ids):
    reactors = defaultdict(lambda: defaultdict(int))

    for post_id in post_ids:
        url = f'https://graph.facebook.com/v9.0/{post_id}/reactions'

        payload = {
            'access_token': facebook_access_token,
        }
        response = handle_request(url, payload)

        for reaction in response['data']:
            reactor_id = reaction['id']

            # В шаге уроке приведен пример с выводом:
            # {
            #     "1629838317162928": {"LIKE": 3, "LOVE": 2, "WOW": 0,
            #                          "HAHA": 1, "SAD": 0, "ANGRY": 2, "THANKFUL": 3},
            # }
            # Если нужно обязательно вывести ноль у реакций, которые пользователь не ставил,
            # то я бы раскомментировал следующий код:
            # if reactor_id not in reactors:
            #     reactors[reactor_id] = REACTIONS_TEMPLATE

            reactors[reactor_id][reaction['type']] += 1

    return reactors
 def test_basic_do_get_with_wrong_playlist_id_returns_client_error(self):
     self.playlist = Stub(entries=[Stub()])
     self.rb.get_playlists.return_value = [self.playlist]
     result = handle_request(self.app, environ('/rest/playlists/1'), self.response)
     self.response.assert_called_with('400 Bad Request: there is no playlist with id 1',
             [('Content-type', 'text/html; charset=UTF-8')])
     self.rb.get_playlists.assert_called_with()
 def test_get_invalid_query(self):
     self.rb.query.side_effect = InvalidQueryException('Invalid query')
     result = handle_request(self.app, 
             environ('/rest/search/song'), 
             self.response)
     self.response.assert_called_with('400 Bad Request: Invalid query', 
             [('Content-type', 'text/html; charset=UTF-8')])
     self.rb.query.assert_called_with({'type': 'song'})
 def test_play_with_no_source_fails(self):
     self.rb.get_playlists.return_value = [self.playlist]
     self.rb.play_source.return_value = False
     result = handle_request(self.app, 
             environ('/rest/playlists', post_data='action=play_source'), 
             self.response)
     self.response.assert_called_with('400 Bad Request: no "source" parameter', 
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_play_invalid_source_fails(self):
     self.rb.get_playlists.return_value = [self.playlist]
     self.rb.play_source.return_value = False
     result = handle_request(self.app, 
             environ('/rest/playlists', post_data='action=play_source&source=10'), 
             self.response)
     self.response.assert_called_with('400 Bad Request: there is no playlist with id 10', 
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_basic_do_get(self):
     self.rb.library.artists = {'values' : {'a guy' : 55}, 'max': 55}
     result = handle_request(self.app, environ('/rest/library/artists'), self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     expected = json.loads('{"values": {"a guy": 55}, "max": 55}')
     returned = json.loads(result)
     self.assertEquals(expected, returned)
 def test_post_search(self):
     self.rb.query.return_value = [self.entry]
     result = handle_request(self.app, environ('/rest/search', post_data=''), self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     expected = json.loads('{ "entries" : [ { "duration" : "duration" , "location" : "location" , "last_played" : "last_played" , "album" : "album" , "title" : "title" , "genre" : "genre" , "year" : "year" , "rating" : "rating" , "id" : "id" , "track_number" : "track_number" , "play_count" : "play_count" , "bitrate" : "bitrate" , "artist" : "artist"  } ] }')
     returned = json.loads(result)
     self.assertEquals(expected, returned)
     self.rb.query.assert_called_with({})
 def test_basic_do_get(self):
     self.queue.get_play_queue.return_value = [Stub(id=1), Stub(id=2), Stub(id=3)]
     result = handle_request(self.app, environ('/rest/queue'), self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     returned = json.loads(result)
     self.queue.get_play_queue.assert_called_with()
     for index, entry in enumerate(returned['entries'], 1):
         self.assertEquals(index, entry['id'])
 def test_get_search_returns_empty_set(self):
     self.rb.query.return_value = []
     result = handle_request(self.app, 
             environ('/rest/search'), 
             self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     returned = json.loads(result)
     self.assertEquals({}, returned)
     self.rb.query.assert_called_with({})
 def test_query_with_params_and_limit(self):
     self.rb.query.return_value = [self.entry for i in range(5)]
     result = handle_request(self.app, 
             environ('/rest/search/song/limit/10/first/5'), 
             self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     returned = json.loads(result)
     self.assertEquals(5, len(returned['entries']))
     self.rb.query.assert_called_with({ 'type' : 'song', 'limit' : '10', 'first' : '5' })
 def test_basic_do_get_with_playlist_returns_right_element(self):
     self.playlist = Stub(entries=[Stub()])
     self.rb.get_playlists.return_value = [self.playlist]
     result = handle_request(self.app, environ('/rest/playlists/0'), self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     expected = json.loads('{ "id" : "id" , "name" : "name" , "type" : "source_type", "entries" : [{"last_played": "last_played", "title": "title", "genre": "genre", "album": "album", "bitrate": "bitrate", "track_number": "track_number", "id": "id", "duration": "duration", "year": "year", "play_count": "play_count", "location": "location", "artist": "artist", "rating": "rating"}]}')
     returned = json.loads(result)
     self.assertEquals(expected, returned)
     self.rb.get_playlists.assert_called_with()
Exemple #13
0
def get_group_id(groupname):
    url = 'https://api.vk.com/method/groups.getById'

    payload = {
        'group_id': groupname,
        'access_token': vk_service_key,
        'v': '5.126'
    }

    response = handle_request(url, payload)

    return -response['response'][0]['id']
 def test_get_status_when_not_playing_works(self):
     self.rb.get_playing_status.return_value = False
     self.rb.get_play_order.return_value = 'bla'
     self.rb.get_mute.return_value = True
     self.rb.get_volume.return_value = 1
     result = handle_request(self.app, environ('/rest/status'), self.response)
     self.response.assert_called_with('200 OK', 
             [('Content-type', 'application/json; charset=UTF-8'), 
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     expected = json.loads('{ "playing_order" : "bla" , "volume" : 1, "muted" : true, "playing" : false }')
     returned = json.loads(result)
     self.assertEquals(expected, returned)
 def test_play_playlist_fails(self):
     self.rb.get_playlists.return_value = [self.playlist]
     self.rb.play_source.return_value = False
     result = handle_request(self.app, 
             environ('/rest/playlists', post_data='action=play_source&playlist=0'), 
             self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     expected = json.loads('{ "result" : "BAD" }')
     returned = json.loads(result)
     self.assertEquals(expected, returned)
     self.rb.play_source.assert_called_with(self.playlist)
Exemple #16
0
def get_posts_list(facebook_group_id, facebook_access_token):
    url = f'https://graph.facebook.com/v9.0/{facebook_group_id}/feed'

    payload = {
        'access_token': facebook_access_token,
        'fields': 'id'
    }

    response = handle_request(url, payload)

    post_ids = [post['id'] for post in response['data']]

    return post_ids
 def test_post_search_with_post_params(self):
     self.rb.query.return_value = [self.entry]
     post_data = '&'.join(('album=calabaza', 'title=oruga', 'artist=uno', 'type=song', 
               'genre=classic', 'rating=4', 'first=1', 'limit=10'))
     result = handle_request(self.app, 
             environ('/rest/search', post_data=post_data), 
             self.response)
     self.response.assert_called_with('200 OK',
             [('Content-type', 'application/json; charset=UTF-8'),
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     expected = json.loads('{ "entries" : [ { "duration" : "duration" , "location" : "location" , "last_played" : "last_played" , "album" : "album" , "title" : "title" , "genre" : "genre" , "year" : "year" , "rating" : "rating" , "id" : "id" , "track_number" : "track_number" , "play_count" : "play_count" , "bitrate" : "bitrate" , "artist" : "artist"  } ] }')
     returned = json.loads(result)
     self.assertEquals(expected, returned)
     self.rb.query.assert_called_with({'album': 'calabaza', 'rating': '4', 'title': 'oruga', 'artist': 'uno', 'limit': '10', 'genre': 'classic', 'type': 'song', 'first': '1'})
Exemple #18
0
def get_likers(post_id):
    url = 'https://api.vk.com/method/likes.getList'

    payload = {
        'type': 'post',
        'owner_id': target_group_id,
        'item_id': post_id,
        'filter': 'likes',
        'count': 100,
        'access_token': vk_service_key,
        'v': '5.126'
    }

    response = handle_request(url, payload)
    return response['response']['items']
Exemple #19
0
def get_comments_from_posts(post_ids, days):
    commentators = set()
    edge_of_comments_date = today - timedelta(days=days)

    for post_id in post_ids:
        url = f'https://graph.facebook.com/v9.0/{post_id}/comments'

        payload = {
            'access_token': facebook_access_token,
            'fields': 'created_time,from'
        }
        response = handle_request(url, payload)

        for comment in response['data']:
            created_at = datetime.strptime(comment['created_time'], "%Y-%m-%dT%H:%M:%S+0000").date()
            if created_at > edge_of_comments_date:
                commentators.add(comment['from']['id'])

    return commentators
 def test_get_status_when_playing_works(self):
     self.rb.get_playing_status.return_value = True
     self.rb.get_play_order.return_value = 'bla'
     self.rb.get_mute.return_value = False
     self.rb.get_volume.return_value = 1
     self.rb.get_playing_entry.return_value = self.entry
     self.rb.get_playing_time.return_value = 10
     result = handle_request(self.app, environ('/rest/status'), self.response)
     self.response.assert_called_with('200 OK', 
             [('Content-type', 'application/json; charset=UTF-8'), 
                 ('Cache-Control: ', 'no-cache; must-revalidate')])
     expected = json.loads('''{ "playing_entry" : {
                     "artist" : "artist" , "title" : "title" , "duration" : 
                     "duration" , "genre" : "genre" , "id" : "id" , "year" : "year" , 
                     "bitrate" : "bitrate" , "location" : "location" , 
                     "rating" : "rating" , "last_played" : "last_played" , 
                     "play_count" : "play_count" , "track_number" : "track_number" , 
                     "album" : "album"  }, 
                     "playing_order" : "bla" , "muted" : false, 
                     "volume" : 1, "playing" : true, "playing_time" : 10 }''')
     returned = json.loads(result)
     self.assertEquals(expected, returned)
 def test_post_next_works(self):
     result = handle_request(self.app, environ("/rest/player", post_data="action=next"), self.response)
     self.rb.play_next.assert_called_with()
     self.rb.get_playing_status.assert_called_with()
 def test_invalid_search_type_fails(self):
     self.rb.query.return_value = [self.entry]
     result = handle_request(self.app, environ('/rest/library/calabaza'), self.response)
     self.response.assert_called_with('400 Bad Request: Invalid library filter "calabaza"',
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_post_dequeue_with_many_values_works(self):
     result = handle_request(
         self.app, environ("/rest/player", post_data="action=dequeue&entry_id=1,2"), self.response
     )
     self.queue.dequeue.assert_called_with([1, 2])
     self.rb.get_playing_status.assert_called_with()
 def test_get_without_parameters_fails(self):
     self.rb.query.return_value = [self.entry]
     result = handle_request(self.app, environ('/rest/library'), self.response)
     self.response.assert_called_with('404 NOT FOUND',
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_post_clear_queue_works(self):
     result = handle_request(self.app, environ("/rest/player", post_data="action=clear_queue"), self.response)
     self.queue.clear_play_queue.assert_called_with()
     self.rb.get_playing_status.assert_called_with()
 def test_post_play_entry_works(self):
     result = handle_request(
         self.app, environ("/rest/player", post_data="action=play_entry&entry_id=1"), self.response
     )
     self.rb.play_entry.assert_called_with(1)
     self.rb.get_playing_status.assert_called_with()
 def test_basic_do_post(self):
     result = handle_request(self.app, environ('/rest/queue', post_data='bla=1'), self.response)
     self.response.assert_called_with('405 method POST not allowed',
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_load_index(self):
     result = handle_request(self.app, environ('/'), self.response)
     self.assertIn('<html>', result)
 def test_post_mute_works(self):
     result = handle_request(self.app, environ("/rest/player", post_data="action=mute"), self.response)
     self.rb.toggle_mute.assert_called_with()
     self.rb.get_playing_status.assert_called_with()
 def test_load_style(self):
     result = handle_request(self.app, environ('/style.css'), self.response)
     self.assertIsNotNone(result)
 def test_load_index_by_full_name(self):
     result = handle_request(self.app, environ('/index.html'), self.response)
     self.assertIn('<html>', result)
 def test_not_found(self):
     result = handle_request(self.app, environ('invalid_file'), self.response)
     self.response.assert_called_with('404 NOT FOUND', 
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_play_with_no_action_fails(self):
     result = handle_request(self.app, 
             environ('/rest/playlists', post_data='source=1'), 
             self.response)
     self.response.assert_called_with('400 Bad Request: no "action" parameter', 
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_post_seek_works(self):
     result = handle_request(self.app, environ("/rest/player", post_data="action=seek&time=10"), self.response)
     self.rb.seek.assert_called_with(10)
     self.rb.get_playing_status.assert_called_with()
 def test_post_invalid_action(self):
     result = handle_request(self.app, 
             environ('/rest/playlists', post_data='action=invalid&playlist=0'), 
             self.response)
     self.response.assert_called_with('400 Bad Request: Unknown action invalid',
             [('Content-type', 'text/html; charset=UTF-8')])
 def test_basic_do_get_without_playlists_returns_error(self):
     self.rb.get_playlists.return_value = []
     result = handle_request(self.app, environ('/rest/playlists/0'), self.response)
     self.response.assert_called_with('404 NOT FOUND',
             [('Content-type', 'text/html; charset=UTF-8')])
     self.rb.get_playlists.assert_called_with()