def parse(self): """Download and parse the stored URLs.""" for ad_url in self.urls: soup = BeautifulSoup(self.download_html(ad_url)) self.found = True address = soup.find(attrs={'data-qa': "is24-expose-address"}) if address is None: error = soup.find('div', {'id': 'oss-error'}) if 'nicht gefunden' in str(error): self.found = False LOGGER.error('Not found: %s', ad_url) else: # Take the first non blank line found in the address div street = [line.strip() for line in address.find_all(text=True) if line.strip()][0] street = ' '.join(street.split()) if not self.found: continue neighborhood_content = [text_only.strip() for text_only in address.children if isinstance(text_only, str)] neighborhood = [zipcode.split(',')[0] for zipcode in neighborhood_content if zipcode][0] if 'Die vollständige Adresse' in neighborhood: self.full_address = ' '.join(street.split()) else: self.full_address = '{}, {}'.format(street, neighborhood) map_url = self.MAP_URL.format( origin=self.full_address.replace(' ', '+'), destination=self.DEFAULT_DESTINATION) self.browse(map_url, 'Google Maps') self.browse(ad_url, 'AD') return self
def browse(url, description): """Open the URL in the browser, and shows a description on the command line. :param url: :param description: """ LOGGER.info('%s: %s', description, url) webbrowser.open(url) sleep(.2)
def create_symbolic_links(): """Create symbolic links for files and dirs, following what's stored on the config file.""" dot_files_dir = read_config( 'dirs', 'dotfiles', os.path.realpath(os.path.join(os.path.dirname(__file__), '../dotfiles'))) if not os.path.exists(dot_files_dir): LOGGER.warning("The directory '%s' does not exist", dot_files_dir) return LOGGER.info("Directory with dot files: '%s'", dot_files_dir) LOGGER.info("Creating links for files in [%s]", SECTION_SYMLINKS_FILES) links = {} cut = len(dot_files_dir) + 1 for root, _, files in os.walk(dot_files_dir): for one_file in files: source_file = os.path.join(root, one_file) key = source_file[cut:] raw_link_name = read_config(SECTION_SYMLINKS_FILES, key, '') links[key] = (source_file, raw_link_name) # http://stackoverflow.com/questions/9001509/how-can-i-sort-a-python-dictionary-sort-by-key/13990710#13990710 for key in sorted(links): (source_file, raw_link_name) = links[key] create_link(key, source_file, raw_link_name, False) LOGGER.info("Creating links for dirs in [%s]", SECTION_SYMLINKS_DIRS) if CONFIG.has_section(SECTION_SYMLINKS_DIRS): for key in CONFIG.options(SECTION_SYMLINKS_DIRS): raw_link_name = read_config(SECTION_SYMLINKS_DIRS, key, '') create_link(key, key, raw_link_name, True) save_config()
def window_monitor(save_logs=True): """Loop to monitor open windows of the selected applications. An app can have multiple windows, each one with its title. :param save_logs: True to save logs (default), False to only display what would be saved (dry run). :return: """ last = {} monitor_start_time = datetime.now() LOGGER.info('Starting the window monitor now (%s)...', monitor_start_time.strftime(TIME_FORMAT)) if not save_logs: LOGGER.error('Not saving logs to the database') try: while True: sleep(.2) for app, new_titles in list_windows().items(): assert isinstance(app, str) assert isinstance(new_titles, list) if app not in last.keys(): last[app] = defaultdict(tuple) for index, new_title in enumerate(new_titles): if last[app][index] and last[app][index][1] == new_title: continue last_info = last[app][index] # Time since last saved time, or since the beginning of the monitoring start_time = last_info[0] if last_info else monitor_start_time end_time = datetime.now() # Save time info for the next change of window title last[app][index] = (end_time, new_title) # Save logs only after the first change of title old_title = last_info[1] if last_info else '' if old_title: try: video = SESSION_INSTANCE.query(Video).filter(Video.path == old_title).one() video_id = video.video_id except NoResultFound: video_id = None window_log = WindowLog(start_dt=start_time, end_dt=end_time, app_name=app, title=old_title, video_id=video_id) LOGGER.info(window_log) if save_logs: SESSION_INSTANCE.add(window_log) SESSION_INSTANCE.commit() if new_title: LOGGER.warning("%s Open window in %s: %s", end_time.strftime(TIME_FORMAT), app, new_title) except KeyboardInterrupt: return
def add_to_playlist(videos): """Add one or more videos to VLC's playlist. :param videos: One or more video paths. :type videos: list|str :return: True if videos were added to the playlist. :rtype: bool """ if not is_vlc_running(): LOGGER.error('VLC is not running, please open it first.') return False videos = [videos] if isinstance(videos, str) else videos pipe = pipes.Template() pipe.append('xargs -0 vlc --quiet --no-fullscreen --no-auto-preparse --no-playlist-autostart', '--') with pipe.open_w(PIPEFILE) as handle: handle.write('\0'.join(videos)) LOGGER.info('%d videos added to the playlist.', len(videos)) return True