class RofiSkyss: def __init__(self): self.rofi = Rofi() cache_dir = os.path.expanduser('~/.cache/rofi-skyss') self.stop_groups_cache = os.path.join(cache_dir, 'stop_groups.json') self.api_url = 'https://api.skyss.no/ws/mobile' self.api_auth = ('mobile', 'g7pEtkRF@') # ensure that the cache dir exists os.makedirs(cache_dir, exist_ok=True) def call_api(self, endpoint): url = '{}/{}'.format(self.api_url, endpoint) response = requests.get(url, auth=self.api_auth) if response.status_code == 200: return response.json() else: return False def get_stop_groups(self): if os.path.exists(self.stop_groups_cache): with open(self.stop_groups_cache) as f: return json.load(f) else: self.rofi.status('Downloading list of stop groups ...') stop_groups = self.call_api('stopgroups') if not stop_groups: self.rofi.error('Failed to download stop groups, sorry :(') sys.exit(1) with open(self.stop_groups_cache, 'w') as f: json.dump(stop_groups, f) self.rofi.close() return stop_groups def get_stops(self, identifier): self.rofi.status('Getting stops and passing times') stop_group = self.call_api('stopgroups/' + identifier) if not stop_group: self.rofi.error('Failed to get stops and passing times, sorry :(') sys.exit(1) routes = [] for group in stop_group['StopGroups']: for stop in group['Stops']: if 'RouteDirections' not in stop: self.rofi.error( 'The stop group is missing routes, sorry :(') sys.exit(1) for route in stop['RouteDirections']: routes.append(route) if not routes: self.rofi.error('The stop group is missing routes, sorry :(') sys.exit(1) route_names = [ '{} - {}'.format(route['PublicIdentifier'], route['DirectionName']) for route in routes ] index, status = self.rofi.select('Route: ', route_names) if status == 0: route = routes[index] times = [time['DisplayTime'] for time in route['PassingTimes']] self.rofi.select('Time: ', times, '<b>{}</b>'.format(route_names[index]), width=len(route_names[index]) * -1) def run(self): stop_groups = self.get_stop_groups()['StopGroups'] stop_group_names = [group['Description'] for group in stop_groups] index, status = self.rofi.select('Stop group: ', stop_group_names) if status == 0: stop_group = stop_groups[index] self.get_stops(stop_group['Identifier'])
def run(): config, config_dir = load_config() parser = argparse.ArgumentParser() parser.add_argument("-a", "--add-to-playlist", action="store_true", help="Add current track to a playlist") parser.add_argument("-l", "--like-current", action="store_true", help="Like current track") parser.add_argument("-st", "--search-track", action="store_true", help="Search for a track") parser.add_argument('-i', '--case-sensitive', action='store_true', help='Enable case sensitivity') parser.add_argument('-r', '--args', nargs=argparse.REMAINDER, help='Command line arguments for rofi. ' 'Separate each argument with a space.') args = parser.parse_args() rofi_args = args.args or [] if not args.case_sensitive: rofi_args.append('-i') rofi = Rofi() scope = "user-library-read user-read-currently-playing user-read-playback-state user-library-modify " \ "user-modify-playback-state playlist-modify-private playlist-read-private playlist-modify-public" sp = spotipy.Spotify(auth_manager=spotipy.oauth2.SpotifyOAuth(client_id=config['spotipy']['client_id'], client_secret=config['spotipy']['client_secret'], redirect_uri=config['spotipy']['redirect_uri'], scope=scope, cache_path=(config_dir + "/token"))) if args.add_to_playlist: track_id, track_meta = getCurrentTrack(sp) playlists = getPlaylists(sp, onlyEditable=True, username=config['spotify']['spotify_username']) playlists_names = [d['name'] for d in playlists['items']] index, key = rofi.select("To which playlist do you want to add " + track_meta + "? ", playlists_names, rofi_args=rofi_args) if key == -1: sys.exit(0) target_playlist_id = playlists['items'][index]['id'] result = addTrackToPlaylist(rofi, rofi_args, sp, config['spotify']['spotify_username'], target_playlist_id, playlists_names[index], track_id, track_meta) if not result == 0: if config['settings'].getboolean('show_add_to_playlist_popups'): rofi.status(track_meta + " added to " + playlists_names[index] + ".", rofi_args=rofi_args) time.sleep(2) rofi.close() sys.exit(0) if args.like_current: track_id, track_meta = getCurrentTrack(sp) sp.current_user_saved_tracks_add({track_id}) rofi.status(track_meta + " liked.", rofi_args=rofi_args) time.sleep(2) rofi.close() if args.search_track: trackquery = rofi.text_entry('Search for a track: ', rofi_args=rofi_args) results = sp.search(trackquery, limit=config['settings']['track_search_max_entries'], type="track") if not results['tracks']['items']: rofi.status("No tracks found.", rofi_args=rofi_args) time.sleep(2) rofi.close() else: tracks = [] for index, track in enumerate(results['tracks']['items']): tracks.append({'id': track['id'], 'artists': getArtistsTitleForID(sp, track['id'])[0], 'title': track['name'], 'uri': track['uri']}) rofi_tracks = [d['artists'] + " - " + d['title'] for d in tracks] index_track, key_track = rofi.select("Select a track: ", rofi_tracks, rofi_args=rofi_args) if key_track == -1: sys.exit(0) index_todo, key_todo = rofi.select(rofi_tracks[index_track] + ": ", ["Add to queue", "Add to playlist", "Play"], rofi_args=rofi_args) if key_todo == -1: sys.exit(0) if index_todo == 0: sp.add_to_queue(tracks[index_track]['id']) if config['settings'].getboolean('show_playback_popups'): rofi.status(rofi_tracks[index_track] + " added to queue.", rofi_args=rofi_args) time.sleep(2) rofi.close() if index_todo == 1: playlists = getPlaylists(sp, onlyEditable=True, username=config['spotify']['spotify_username']) playlists_names = [d['name'] for d in playlists['items']] index_playlist, key_playlist = rofi.select("To which playlist do you want to add " + rofi_tracks[index_track] + "? ", playlists_names, rofi_args=rofi_args) if key_playlist == -1: sys.exit(0) target_playlist_id = playlists['items'][index_playlist]['id'] result = addTrackToPlaylist(rofi, rofi_args, sp, config['spotify']['spotify_username'], target_playlist_id, playlists_names[index_playlist], tracks[index_track]['id'], rofi_tracks[index_track]) if not result == 0: if config['settings'].getboolean('show_add_to_playlist_popups'): rofi.status(rofi_tracks[index_track] + " added to " + playlists_names[index_playlist] + ".", rofi_args=rofi_args) time.sleep(2) rofi.close() if index_todo == 2: sp.start_playback(uris=[tracks[index_track]['uri']]) if config['settings'].getboolean('show_playback_popups'): rofi.status("Playing " + rofi_tracks[index_track] + ".", rofi_args=rofi_args) time.sleep(2) rofi.close() sys.exit(0) curr_track_id, curr_track_meta = getCurrentTrack(sp) index, key = rofi.select("Currently playing: " + curr_track_meta + " ", ["Add current song to playlist", "Like current track", "Search track"], rofi_args=rofi_args) if index == 0: rofi_args = args.args or [] rofi_args.append("-a") subprocess.run(["rofi-spotify", ", ".join(rofi_args)]) if index == 1: rofi_args = args.args or [] rofi_args.append("-l") subprocess.run(["rofi-spotify", ", ".join(rofi_args)]) if index == 2: rofi_args = args.args or [] rofi_args.append("-st") subprocess.run(["rofi-spotify", ", ".join(rofi_args)]) sys.exit(0)
class RofiSkyss: def __init__(self): self.rofi = Rofi() cache_dir = os.path.expanduser('~/.cache/rofi-skyss') self.stop_groups_cache = os.path.join(cache_dir, 'stop_groups.json') self.api_url = 'https://api.skyss.no/ws/mobile' self.api_auth = ('mobile', 'g7pEtkRF@') # ensure that the cache dir exists os.makedirs(cache_dir, exist_ok=True) def call_api(self, endpoint): url = '{}/{}'.format(self.api_url, endpoint) response = requests.get(url, auth=self.api_auth) if response.status_code == 200: return response.json() else: return False def get_stop_groups(self): if os.path.exists(self.stop_groups_cache): with open(self.stop_groups_cache) as f: return json.load(f) else: self.rofi.status('Downloading list of stop groups ...') stop_groups = self.call_api('stopgroups') if not stop_groups: self.rofi.error('Failed to download stop groups, sorry :(') sys.exit(1) with open(self.stop_groups_cache, 'w') as f: json.dump(stop_groups, f) self.rofi.close() return stop_groups def get_stops(self, identifier): self.rofi.status('Getting stops and passing times') stop_group = self.call_api('stopgroups/' + identifier) if not stop_group: self.rofi.error('Failed to get stops and passing times, sorry :(') sys.exit(1) routes = [] for group in stop_group['StopGroups']: for stop in group['Stops']: if 'RouteDirections' not in stop: self.rofi.error('The stop group is missing routes, sorry :(') sys.exit(1) for route in stop['RouteDirections']: routes.append(route) if not routes: self.rofi.error('The stop group is missing routes, sorry :(') sys.exit(1) route_names = ['{} - {}'.format(route['PublicIdentifier'], route['DirectionName']) for route in routes] index, status = self.rofi.select('Route: ', route_names) if status == 0: route = routes[index] times = [time['DisplayTime'] for time in route['PassingTimes']] self.rofi.select('Time: ', times, '<b>{}</b>'.format(route_names[index]), width=len(route_names[index]) * -1) def run(self): stop_groups = self.get_stop_groups()['StopGroups'] stop_group_names = [group['Description'] for group in stop_groups] index, status = self.rofi.select('Stop group: ', stop_group_names) if status == 0: stop_group = stop_groups[index] self.get_stops(stop_group['Identifier'])
if time.time() > float(config['auth']['expires_at']): authorize(config['global']['redirect_uri'], config['global']['client_id'], config['global']['secret'], "refresh_token", config['auth']['refresh_token']) config.read(config_file) client = spotify.Client(config['global']['client_id'], config['global']['secret']) user = client.user_from_token(config['auth']['access_token']) player = user.get_player() devices = user.get_devices() if len(devices) == 0: r.error("No device found where music could get played on") exit(0) device = devices[0] r.status("Loading playlists...") playlists = user.get_all_playlists() r.close() if "default_device" in config["global"]: for d in devices: if str(d) == config["global"]["default_device"]: device = d options = [] for p in playlists: options.append(p.name) msg = "<b>Currently Playing:</b> " curr = user.currently_playing() if 'item' in curr: