예제 #1
0
파일: interface.py 프로젝트: emmercm/HarkFM
 def save_settings(self, form):
     form = json.loads(form)
     storage = harkfm.Storage()
     for key in form:
         if type(form[key]) is str and form[key].isdigit():
             form[key] = int(form[key])
         storage.config_set('settings/' + key, form[key])
     interface = harkfm.Interface()
     interface.index()
예제 #2
0
    def __init__(self, props=None):
        if props is None:
            props = {}

        if self.__class__.storage is None:
            self.__class__.storage = harkfm.Storage()
        if self.__class__.engine is None:
            self.__class__.engine = harkfm.Engine()
        if self.__class__.interface is None:
            self.__class__.interface = harkfm.Interface()
        if self.__class__.logger is None:
            self.__class__.logger = logging.getLogger('root')

        self.app = None

        self._artist = None
        self._artist_corrected = None
        self.artist_img = None
        self.artist_url = None
        self.artist_wiki = None
        self.artist_listeners = 0
        self.artist_plays_global = 0
        self.artist_plays = 0
        self.artist_tags = []
        self.artist_similar = []
        self.artist_gender = None
        self.artist_country = None

        self._track = None
        self._track_corrected = None
        self.track_url = None
        self.track_wiki = None
        self.track_duration = 3 * 60  # default tracks to 3 minutes
        self.track_plays = 0
        self.track_tags = []
        self.track_loved = None

        self._album = None
        self._album_corrected = None
        self.album_img = None
        self.album_url = None
        self.album_wiki = None
        self.album_year = 0

        self.start = 0
        self.listened = 0
        self.queued = 0

        self._corrected_gn = False
        self._corrected_lfm = False

        for prop in props.keys():
            setattr(self, prop, props[prop])
예제 #3
0
    def __init__(self):
        if self.__class__.config is None:
            # Parse JSON config
            json_file = os.path.splitext(__file__)[0] + '.json'
            self.__class__.config = harkfm.Util.json_load(json_file)

        if self.__class__.storage is None:
            self.__class__.storage = harkfm.Storage()
        if self.__class__.logger is None:
            self.__class__.logger = logging.getLogger('root')

        self.lfm_login()
        self.scrobbler_log()
예제 #4
0
파일: interface.py 프로젝트: emmercm/HarkFM
 def login(self, query):
     form = self._deserialize(query)
     storage = harkfm.Storage()
     api_key = harkfm.Engine.config['apis']['last.fm']['key']
     api_secret = harkfm.Engine.config['apis']['last.fm']['secret']
     try:
         network = pylast.get_lastfm_network(api_key, api_secret)
         session_key = pylast.SessionKeyGenerator(network).get_session_key(form['username'],  pylast.md5(form['password']))
         storage.config_set('apis/last.fm/session_key', session_key)
         interface = harkfm.Interface()
         interface.index()
     except Exception as e:
         harkfm.Interface.logger.error('%s  %s', type(e), e)
예제 #5
0
파일: scanner.py 프로젝트: emmercm/HarkFM
    def __init__(self):
        if self.__class__._config is None:
            # Parse JSON config
            json_file = os.path.splitext(__file__)[0] + '.json'
            self.__class__._config = harkfm.Util.json_load(json_file)
            # Compile JSON config
            if 'windows' in self.__class__._config:
                for window_id, window in enumerate(self.__class__._config['windows']):
                    for prop in window['window']:
                        self.__class__._config['windows'][window_id]['window'][prop] = re.compile(window['window'][prop])
                    for prop in window['regex']:
                        self.__class__._config['windows'][window_id]['regex'][prop] = re.compile(window['regex'][prop])
            if 'replace' in self.__class__._config:
                for category in self.__class__._config['replace']:
                    for replace_id, replace in enumerate(self.__class__._config['replace'][category]):
                        self.__class__._config['replace'][category][replace_id][0] = re.compile(
                            self.__class__._config['replace'][category][replace_id][0])

        if self.__class__.storage is None:
            self.__class__.storage = harkfm.Storage()
        if self.__class__.logger is None:
            self.__class__.logger = logging.getLogger('root')

        class ScannerThread(QThread):
            updated = QtCore.pyqtSignal(dict)

            def setup(self, scanner):
                self.scanner = scanner

            def run(self):
                engine = harkfm.Engine()
                firefox = harkfm.Firefox()

                windows = []
                while True:
                    # Scan for a new window
                    while len(windows) == 0:
                        if 'windows' in self.scanner._config and len(self.scanner._config['windows']) > 0:
                            if os.name == 'nt':
                                import win32gui

                                def win_search(hwnd, lParam):
                                    if win32gui.IsWindowVisible(hwnd):
                                        # Get window properties
                                        try:
                                            w_class = win32gui.GetClassName(hwnd)
                                            w_text = win32gui.GetWindowText(hwnd)
                                        except Exception as e:
                                            self.scanner.logger.error('%s  %s', type(e), e)
                                            return
                                        # Ignore some default windows
                                        if w_class in [
                                            'IME',
                                            'MSCTFIME UI',
                                            'tooltips_class32'
                                        ]:
                                            return
                                        # Look for match in self.scanner._config['windows']
                                        try:
                                            for idx, window in enumerate(self.scanner._config['windows']):
                                                if (
                                                    ('class' not in window['window'] or (w_class and re.search(window['window']['class'], w_class))) and
                                                    ('title' not in window['window'] or (w_text and re.search(window['window']['title'], w_text)))
                                                ):
                                                    windows.append((idx, hwnd, w_class, win32gui.GetWindowText))
                                                    break
                                        except Exception as e:
                                            self.scanner.logger.error('%s  %s', type(e), e)

                                win32gui.EnumWindows(win_search, None)

                                if firefox.pids():
                                    for hwnd in firefox.hwnd():
                                        if win32gui.IsWindow(hwnd):
                                            w_class = win32gui.GetClassName(hwnd) if hwnd is not None else ''
                                            for tab in firefox.tabs():
                                                w_text = tab['title'] if 'title' in tab else ''
                                                for idx, window in enumerate(self.scanner._config['windows']):
                                                    if (
                                                        ('class' not in window['window'] or re.search(window['window']['class'], w_class)) and
                                                        ('title' not in window['window'] or re.search(window['window']['title'], w_text))
                                                    ):
                                                        windows.append((idx, tab, w_class, firefox.tab_title))
                                                        break

                        time.sleep(0.5)

                    # Get info from window
                    if len(windows) > 0:
                        if os.name == 'nt':
                            import win32gui

                            # Get window properties
                            window = self.scanner._config['windows'][windows[0][0]]
                            if type(windows[0][1]) is int and not win32gui.IsWindow(windows[0][1]):
                                windows = []
                                self.updated.emit({})
                                continue
                            w_class = windows[0][2](windows[0][1]) if hasattr(windows[0][2], '__call__') else windows[0][2]
                            w_text = windows[0][3](windows[0][1]) if hasattr(windows[0][3], '__call__') else windows[0][3]

                            # Check if we lost window match
                            if (
                                not w_text or
                                ('class' in window['window'] and not re.search(window['window']['class'], w_class)) or
                                ('title' in window['window'] and not re.search(window['window']['title'], w_text))
                            ):
                                windows = []
                                self.updated.emit({})
                                continue

                            # Parse window title (if changed)
                            if not hasattr(engine.current, 'w_text') or w_text != engine.current.w_text:
                                props = {'w_text': w_text, 'app': window}
                                if 'title' in window['window']:
                                    w_text = harkfm.Util.regex(window['window']['title'], w_text)
                                for prop in window['regex']:
                                    props[prop] = harkfm.Util.regex(window['regex'][prop], w_text)
                                    if props[prop] is None: props[prop] = ''
                                    # Do replacements
                                    if self.scanner.storage.config_get('settings/correct/scanner'):
                                        for replace in self.scanner._config['replace']['all']:
                                            props[prop] = re.sub(replace[0], replace[1], props[prop])
                                        for replace in self.scanner._config['replace'][prop]:
                                            props[prop] = re.sub(replace[0], replace[1], props[prop])

                                self.updated.emit(props)

                    time.sleep(0.1)

        def emit_updated(props):
            engine = harkfm.Engine()
            if props and props['artist'] and props['track']:
                engine.current = harkfm.Track(props)
            else:
                engine.current = None

        t = ScannerThread(harkfm.Interface.qMainWindow)
        t.updated.connect(emit_updated)
        t.setup(self)
        t.start()