def genius_scrape_url(url, title): proxy = urllib.request.getproxies() r = requests.get(url, timeout=10, proxies=proxy) try: document = BeautifulSoup(r.text, 'html.parser') lyrics_div = document.find('div', class_='lyrics') lyrics_paragraphs = [] [ lyrics_paragraphs.append(elem.get_text()) for elem in lyrics_div.find_all('p') ] lyrics = ''.join(lyrics_paragraphs) return LYRICS_TUPLE(lyrics.strip(), url) except: if genius_key == '': logger.log(logger.LOG_LEVEL_INFO, SEARCH_ERROR.format(source='Genius', file=title)) else: logger.log(logger.LOG_LEVEL_ERROR, PARSE_ERROR.format(source='Genius', file=title)) return False return False
def azlyrics_scrape_url(url, title): proxy = urllib.request.getproxies() headers = requests.utils.default_headers( ) # AZLyrics filters against bots by inspecting user-agent headers.update({ 'User-Agent': random.choice(USER_AGENTS), }) r = requests.get(url, timeout=10, proxies=proxy, headers=headers) r.encoding = 'utf-8' try: document = BeautifulSoup(r.text, 'html.parser') lyrics = document.find('div', class_='', id='') [ elem.extract() for elem in lyrics.find_all( text=lambda text: isinstance(text, Comment)) ] # Remove all text that is a comment in lyrics [elem.extract() for elem in lyrics.find_all('div')] # Remove any sub-divs in lyrics [elem.extract() for elem in lyrics.find_all('script')] # Remove any scripts in lyrics [elem.extract() for elem in lyrics.find_all('i')] # Remove any italics in lyrics [elem.extract() for elem in lyrics.find_all('br')] # Remove <br> tags return LYRICS_TUPLE(lyrics.get_text().strip(), url) except: logger.log(logger.LOG_LEVEL_ERROR, PARSE_ERROR.format(source='AZLyrics', file=title)) return False return False
def __init__(self): with open("/sys/devices/system/cpu/cpu1/cpufreq/scaling_governor" ) as file: self.performance_mode = file.read() logger.log( "Created an instance of the modules.performance_handler.PerformanceHandler class." )
def musixmatch_search_for_url(artist, title): proxy = urllib.request.getproxies() url_params = urllib.parse.quote_plus(artist + ' ' + title) search_url = MUSIXMATCH_URL_BASE + '/search/{params}'.format( params=url_params) # print(search_url) headers = requests.utils.default_headers( ) # Musixmatch filters against bots by inspecting user-agent headers.update({ 'User-Agent': random.choice(USER_AGENTS), }) r = requests.get(search_url, timeout=10, proxies=proxy, headers=headers) try: document = BeautifulSoup(r.text, 'html.parser') search_result = document.find('div', class_='box-style-plain') search_result = search_result.find_all('a', href=True) url = MUSIXMATCH_URL_BASE + search_result[0]['href'] # print(url) except: logger.log(logger.LOG_LEVEL_INFO, SEARCH_ERROR.format(source='Musixmatch', file=title)) return False return url
def get_default_manager(self, ask_if_not_found: bool = False): """ Get default package manager. """ current_distro = distro.id() if current_distro not in self.couples and ask_if_not_found: print( "Sorry, but we cannot recognize your package manager. Now you cannot use a few of features." "But we support these managers: ") self._show_managers() manager = input("Please, choose one of them: ") self._check_if_manager_exists(manager) return self.managers[manager] elif current_distro not in self.couples and not ask_if_not_found: config_content = ConfigReader().read() assert "package_manager" in config_content, "You did not set the package manager in you config file!" return self.managers[config_content["package_manager"]] default_manager = self.couples[current_distro] logger.log( f"A default package manager was defined: {default_manager.name} at " "modules.package_managers.DefaultManager.get_default_manager method." ) return default_manager
def lyricwiki_scrape_url(url, title): proxy = urllib.request.getproxies() r = requests.get(url, timeout=10, proxies=proxy) try: document = BeautifulSoup(r.text, 'html.parser') lyrics = document.find( 'div', class_='lyricbox') # Find all divs with class lyricbox [ elem.extract() for elem in lyrics.find_all( text=lambda text: isinstance(text, Comment)) ] # Remove all text that is a comment in lyrics [elem.extract() for elem in lyrics.find_all('div')] # Remove any sub-divs in lyrics [elem.extract() for elem in lyrics.find_all('script')] # Remove any scripts in lyrics [elem.replace_with('\n') for elem in lyrics.find_all('br') ] # Remove <br> tags and reformat them into \n line breaks return LYRICS_TUPLE(lyrics.get_text().strip(), url) except: logger.log(logger.LOG_LEVEL_ERROR, PARSE_ERROR.format(source='LyricWiki', file=title)) return False return False
def setAlbumArt(self, imageData, deviceRatio): """Sets album art in song widget Args: imageData (bytes): A bytes literal containing embedded album art deviceRatio (int): Pixel ratio of the screen that this program runs on """ try: if imageData == b'' or imageData is None: albumImage = QtGui.QImage( utils.resource_path('./assets/art_empty.png')) else: albumImage = QtGui.QImage.fromData(imageData) if albumImage.isNull(): albumImage = QtGui.QImage( utils.resource_path('./assets/art_empty.png')) albumIcon = QtGui.QPixmap.fromImage(albumImage) albumIcon.setDevicePixelRatio(deviceRatio) self._iconWidth = deviceRatio * (self._albumArtLabel.width() - 10) self._iconHeight = deviceRatio * (self._albumArtLabel.height() - 10) self._albumArtLabel.setPixmap( albumIcon.scaled(self._iconWidth, self._iconHeight, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)) except: logger.log(logger.LOG_LEVEL_ERROR, 'Error setting album art.')
def _check_if_manager_exists(self, manager: str): try: assert manager in self.managers, "Chosen manager is not supported!" except AssertionError as error: logger.log(error) exit()
def __init__(self, author, message, deadline=None, full_deadline=None): self.author = author self.message = message self.deadline = full_deadline or deadline self.fixed_deadline = bool(full_deadline) self.home_dir = get_path("home") logger.log("An instance of the modules.note.Note class was created.")
def erase(self, name=None, names=None): """ Erase element or elements from a config. Attributes ---------- name: str Name of an erasing element. names: list [ "name", "name" ] List of names those should be erased. """ config = open(self.path, "r") config_backup = self.config_reader.read() config_content = self.config_reader.read() config.close() try: self._check_if_given_both(name, names) config = open(self.path, "w") config_content = self._erase_items(config_content, names or [name]) self._write_items_to_config(config, config_content or {}) config.close() logger.log("The param(s) ha(s/ve) been erased from the config.") except AssertionError as e: config_writer.write(items=config_backup) logger.log(e) sys.exit()
def write(self, name=None, value=None, items=None): """ Write element to a config. Arguments --------- name: str Name of a config attribute. value: str Value of a config attribute. items: dict { "name" : "value" } A dictionary of couples name: value. Any Items will be written to a config. """ try: assert os.path.isfile(self.path), "Config file does not exists!" config = open(self.path, "a") self._write_items_to_config(config, items or {name: value}) config.close() logger.log("The param(s) ha(s/ve) been written to the config.") except AssertionError as error: logger.log(error) sys.exit()
def setUrl(self, url): self._url = url try: if SongWidget.dialog is not None: if SongWidget.dialog.getFilepath() == self._filepath: SongWidget.dialog.updateUrl(url) except Exception as e: logger.log(logger.LOG_LEVEL_ERROR, str(e))
def setLyrics(self, lyrics): self._lyrics = lyrics try: if SongWidget.dialog is not None: if SongWidget.dialog.getFilepath() == self._filepath: SongWidget.dialog.updateLyrics(lyrics) except Exception as e: logger.log(logger.LOG_LEVEL_ERROR, str(e))
def read(self) -> dict: """ Read content of the configuration file. """ config_content = self._get_config_content() config_items = self._create_config_items(config_content) config = self._create_config(config_items) logger.log("The config has been read successfully.") return config
def _get_action(self, arg: str): try: assert arg in actions, f"An action with name {arg} does not exists!" return actions[arg] except AssertionError as error: logger.log(error) exit()
def check_if_config_exists(): """ Check if required configuration file exists. """ if not os.path.isfile(get_path("config")): logger.log( Warning( "✘ You haven't initialized! Execute doondler --init to create a" " user config file and continue!")) raise InitializationError( "Initialization error! Initialize and try again!")
def remove(self): """ Remove a config file. """ is_config_exists = os.path.isfile(self.main_path) assert is_config_exists, "You cannot remove file that not exists!" os.remove(self.main_path) logger.log( "The config has been removed successfully at the modules.config" ".Config.remove method.")
def _set_params(self): self.username["value"] = ask_param(self.username) self.home_dir["value"] = ask_param(self.home_dir) self.city["value"] = ask_param(self.city) self.handler["value"] = take_handler(self.handler) self.package_manager["value"] = ask_param(self.package_manager) logger.log( "Config params (username, home_dir, city, etc...) were set at the " "modules.config.Config._set_params method.")
def _create_config_item(self, key, value): try: assert bool(key) is True, "You cannot set empty parameter name!" assert bool(value) is True, "You cannot set empty parameter value!" return f"{key} = {value}\n" except AssertionError as e: logger.log(e) sys.exit()
def _write_items_to_config(self, config, items: dict): try: assert len(items.keys()) != 0, "You cannot set empty list of parameters!" for name, value in items.items(): self._write_par_to_config(config, name, value) except AssertionError as e: logger.log(e) sys.exit()
def _handle_playground_validation_error(self, error: PlaygroundValidationError, fields: list, ship: Ship): logger.set_traceback_showing_mode(False) logger.log(error) ship.unset() self._clear_fields_to_check() logger.log( Warning( f"The ship at fields {[f.index for f in fields]} was unset.")) logger.set_traceback_showing_mode(True)
def get_package_manager(manager_name: str): """ Create a package manager. """ try: managers = get_managers() package_manager = managers[manager_name] assert manager_name in managers, f"Package manager with name: {manager_name} doesn't exits!" return package_manager except (AssertionError, KeyError) as error: logger.log(error) exit()
def _parse_content(self): try: html = requests.get(self.api_url, headers=self.headers) assert html.status_code == 200, f"A city {self.city} does not exists in YandexPogoda service!" logger.log( "Html content was parsed successfully at method modules.synoptic.Synoptic._parse_content." ) return html.text except AssertionError as error: logger.log(error) exit()
def saveLyrics(self, lyrics): try: self._lyrics = lyrics self._settings = settings.Settings() lyric_grabber.write_file(artist=self._artist, title=self._title, write_info=self._settings.info, write_metadata=self._settings.metadata, write_text=self._settings.text, lyrics=lyrics, song_filepath=self._filepath) except Exception as e: logger.log(logger.LOG_LEVEL_ERROR, str(e))
def delete(self): """ Delete notification. """ try: error_mes = f"Notification with id {self.deadline} does not exists!" assert self._exists() is True, error_mes os.remove(f"{get_path('note')}{self.deadline}") os.remove(f"{get_path('note')}{self.deadline}_output") logger.log(f"Removed notification with id = {self.deadline}") except AssertionError as error: logger.log(error) exit()
def genius_scrape_url(url, title): proxy = urllib.request.getproxies() r = requests.get(url, timeout=10, proxies=proxy) try: document = BeautifulSoup(r.text, 'html.parser') # Genius seems to be returning two types of content # One has a 'lyrics' div, the other has Lyrics__Container lyrics_div = document.find('div', class_='lyrics') if lyrics_div: lyrics_paragraphs = [] [ lyrics_paragraphs.append(elem.get_text()) for elem in lyrics_div.find_all('p') ] lyrics = ''.join(lyrics_paragraphs) return LYRICS_TUPLE(lyrics.strip(), url) lyrics_containers = document.find_all( 'div', class_=re.compile('Lyrics__Container*')) if lyrics_containers: lyrics = '' for lyrics_container in lyrics_containers: # Genius puts annotations nested with the actual lyrics spans # In order to extract the lyrics correctly, need to replace HTML line breaks # with \n line breaks for br in lyrics_container.find_all('br'): br.replace_with('\n') lyrics += lyrics_container.text return LYRICS_TUPLE(lyrics, url) lyrics_container = document.find( 'div', class_=re.compile('LyricsPlaceholder__Message*')) if lyrics_container: # When the song is an instrumental, Genius sometimes puts a LyricsPlaceholder div lyrics = '[Instrumental]' return LYRICS_TUPLE(lyrics, url) except: if genius_key == '': logger.log(logger.LOG_LEVEL_INFO, SEARCH_ERROR.format(source='Genius', file=title)) else: logger.log(logger.LOG_LEVEL_ERROR, PARSE_ERROR.format(source='Genius', file=title)) return False return False
def _take_info(self): try: soup = BeautifulSoup(self._parse_content(), "lxml") title = soup.select("div.header-title.header-title_in-fact")[0] subtitle = soup.select("div.fact__time-yesterday-wrap")[0] main_weather_block = soup.find("div", class_="fact__temp-wrap") sub_weather_block = soup.find("div", class_="fact__props") return { "title": title.find("h1", id="main_title").get_text(), "subtitle": subtitle.get_text(), "temperature": main_weather_block.find( "span", class_="temp__value_with-unit").get_text(), "weather_type": main_weather_block.select( "div.link__condition.day-anchor.i-bem")[0].get_text(), "feels_like": main_weather_block.select( "div.term.term_orient_h.fact__feels-like")[0].get_text(), "wind_speed": sub_weather_block.find("div", class_="fact__wind-speed").get_text(), "humidity": sub_weather_block.find("div", class_="fact__humidity").get_text(), "pressure": sub_weather_block.find("div", class_="fact__pressure").get_text(), } except IndexError as error: message = soup.find("p", class_=["text-wrapper", "text-wrapper_info"]) message = message.get_text() if bool( message) else "*He hasn't said something*" logger.set_traceback_showing_mode(False) logger.log(error) logger.log( Warning( "It seems like a problem. I guess there is something wrong with a " "server. Please, wait and try again a little bit later.\n" "Also, you probably were banned by YandexPogoda service.\n" "Server said: %s" % message)) exit()
def create(self): """ Create a notification and start a system daemon. """ self._set_deadline() note_path = self.home_dir + "/.note_" + str(self.deadline) note = open(note_path, "w+") note.write(self._make_note()) note.close() os.system(f"chmod +x {get_path('note')}{self.deadline}") os.system(f"nohup python3 -u {get_path('note')}{self.deadline} > " f"{get_path('note')}{self.deadline}_output &") logger.log( Warning(f"Added a new note ({get_path('note')}{self.deadline})"))
def _get_config_content(self) -> str: try: if not os.path.isfile(self.path): raise InitializationError( "Oops! Config reader cannot find config file!") config = open(self.path, "r") config_content = config.read() config.close() return config_content except InitializationError as error: logger.log(error) sys.exit()
def gen_password(prefix: str, length: str): """ Generate a random password with set length and print it. """ try: password_generator = PasswordGenerator(prefix, int(length)) password = password_generator.gen_password() print( f"\n{to_green('A password was successfully generated!')}\nResult: {password}" ) print("Copied to the clipboard.\n") copy_to_clip(password) except Exception as error: logger.log(error) exit()