def follow_user(request, area_data, reaction_index): access_token, client_id = get_token("Twitch", area_data["user_id"], twitch=True) username = "" user_to_follow = "" params = area_data["reaction"][reaction_index]["parameters"] for param in params: if 'user-to-follow' in param: user_to_follow = param['user-to-follow'] elif 'your-username' in param: username = param['your-username'] user_id, following_id = get_user_id(user_to_follow, username, access_token, client_id) resp = requests.post(URL_FOLLOW_USER, headers={ 'Authorization': 'Bearer {}'.format(access_token), 'Client-Id': client_id, 'Content-Type': 'application/json' },json={ 'to_id': following_id, 'from_id': user_id }) if resp.status_code not in [204]: raise ValueError('Error when follow the user {}. Error: {}, Code: {}'.format(user_to_follow, resp.text, resp.status_code))
def add_songs_to_playlists(request, area_data, reaction_index): print("Spotify add song to playlist for user: "******"user_id"]) access_token = get_token("Spotify", area_data["user_id"]) song_id = get_songs_ids( area_data["reaction"][reaction_index]["parameters"][0]["song-list"], access_token) playlists_new = [] if request["data"]["data"]["playlists"]: playlists_new = request["data"]["data"]["playlists"] else: params = area_data["reaction"][reaction_index]["parameters"] for param in params: if 'playlist' in param: playlists_new.append(param['playlist']) for playlist in playlists_new: playlist_data_for_request = "" for song in song_id: playlist_data_for_request += "{},".format(song) resp = requests.post(ADD_ITEM_TO_PLAYLIST.format(playlist), params={"uris": playlist_data_for_request}, headers={ "Authorization": "Bearer {}".format(access_token), "Content-Type": "application/json", "Accept": "application/json" }) if resp.status_code not in [200, 201]: raise ValueError( "Error when add song on the spotify playlist. Error : {} , Code = {}" .format(resp.text, resp.status_code)) print("Add song to playlist was accepted !")
def create_repo(request, area_data, reaction_index): access_token = get_token("Github", area_data["user_id"]) params = area_data["reaction"][reaction_index]["parameters"] repository_name = "" description = "" for param in params: if 'repository-name' in param: repository_name = param['repository-name'] elif 'description' in param: description = param['description'] print("Create repository {}".format(repository_name)) resp = requests.post(CREATE_REPO_URL, headers={ "Accept": "application/vnd.github.v3+json", "Authorization": "token {}".format(access_token), }, json={ "name": repository_name, "description": description, }) if resp.status_code != 201: raise ValueError( "Error when create repository {}. Error : {}, Code : {}".format( repository_name, resp.text, resp.status_code))
def follow_artist(request, area_data, reaction_index): access_token = get_token("Spotify", area_data["user_id"]) artist_name = area_data["reaction"][reaction_index]["parameters"][0][ "artist-name"] print(artist_name) id = get_artist_ids(artist_name, access_token) print(id) resp = requests.put(GET_ARTIST_URL.format(id), headers={ "Authorization": "Bearer {}".format(access_token), "Content-Type": "application/json", "Accept": "application/json" }) if resp.status_code not in [200, 201, 204]: raise ValueError( 'Error when follow artist : {}. Error: {}, Code: {}'.format( artist_name, resp.text, resp.status_code))
def like_song(request, area_data, reaction_index): access_token = get_token("Spotify", area_data["user_id"]) song_name = [ area_data["reaction"][reaction_index]["parameters"][0]["song-name"] ] ids = get_songs_ids(song_name, access_token) resp = requests.put(LIKE_SONG.format(ids[0].replace('spotify:track:', '')), headers={ "Authorization": "Bearer {}".format(access_token), "Content-Type": "application/json", "Accept": "application/json" }) if resp.status_code not in [200, 201]: raise ValueError( 'Error when like the song: {}. Error: {}, Code: {}'.format( area_data["reaction"][reaction_index]["parameters"][0] ["song-name"], resp.text, resp.status_code))
def follow_user(request, area_data, reaction_index): access_token = get_token("Github", area_data["user_id"]) following = "" params = area_data["reaction"][reaction_index]["parameters"] for param in params: if 'user-to-follow' in param: following = param['user-to-follow'] resp = requests.put(FOLLOW_USER_URL.format(following), headers={ "Accept": "application/vnd.github.v3+json", "Authorization": "token {}".format(access_token), }) if resp.status_code > 300: raise ValueError( "Error when follow the user {}. Error: {}, Code : {}".format( following, resp.text, resp.status_code)) print("Follow user {}".format(following))
def create_playlist(request, area_data, reaction_index): access_token = get_token("Spotify", area_data["user_id"]) username = get_username(access_token) resp = requests.post(CREATE_PLAYLIST_URL.format(username), headers={ "Authorization": "Bearer {}".format(access_token), "Content-Type": "application/json", "Accept": "application/json" }, json={ 'name': area_data["reaction"][reaction_index] ["parameters"][0]["playlist-name"] }) if resp.status_code not in [200, 201]: raise ValueError( 'Error when create the playlist {}. Error: {}, Code: {}'.format( area_data["reaction"][reaction_index]["parameters"][0] ["playlist-name"], resp.text, resp.status_code))
def add_colab(request, area_data, reaction_index): collaborator_name = "" your_username = "" repository_name = "" params = area_data["reaction"][reaction_index]["parameters"] access_token = get_token("github", area_data["user_id"]) if request["data"]["data"]["repos"]: repository_name = request["data"]["data"]["repos"] for param in params: if 'collaborator-name' in param: collaborator_name = param["collaborator-name"] elif 'your-username' in param: your_username = param["your-username"] else: for param in params: if 'collaborator-name' in param: collaborator_name = param["collaborator-name"] elif 'your-username' in param: your_username = param["your-username"] elif 'repository-name' in param: repository_name = param['repository-name'] resp = requests.put(ADD_COLLAB_URL.format(your_username, repository_name, collaborator_name), headers={ "Accept": "application/vnd.github.v3+json", "Authorization": "token {}".format(access_token), }, json={"permission": "admin"}) if resp.status_code != 201: raise ValueError( 'Error when add {} to the repo {}. Error: {}, Code: {}'.format( collaborator_name, repository_name, resp.text, resp.status_code))