예제 #1
0
    def add_url_to_group(url, group):
        """
        Add url to selected group

        Args:
            url (string): selected url
            group (string): name of selected group

        Returns:
            string: Returns index of added url from url list
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)
        for i, entry in enumerate(res['urls']):
            if entry['actual_url'] == url:
                if group in res['groups']:
                    if i not in res['groups'][group]:
                        res['groups'][group].append(i)
                    else:
                        return -1
                else:
                    res['groups'][group] = [i]
                dbh.add_entry(username, res)
                return i
예제 #2
0
    def remove_url(url):
        """
        Removes url from database

        Args:
            url (string): removes url from database
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)

        for i, entry in enumerate(res['urls']):
            if entry['actual_url'] == url:
                for group in res['groups']:
                    if i in res['groups'][group] and group != 'Most Popular URLs':
                        res['groups'][group].remove(i)

                dbh.add_entry(username, res)
                stats = dbh.get_entry("__all_urls_statistics__")
                if stats == None:
                    return
                url_exists = False
                for i, stat in enumerate(stats):
                    if url in stat:
                        url_exists = True
                        stats[i][1] -= 1
                        break
                if url_exists and stats[i][1] == 0:
                    stats.pop(i)
                dbh.add_entry("__all_urls_statistics__", stats)
                return
예제 #3
0
    def append_downloaded_articles(url, articles):
        """
        Appends downloaded articles to database

        Args:
            url (string): url to rss
            articles (list): list of articles objects
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)

        for i, entry in enumerate(res['urls']):
            if entry['actual_url'] == url:
                for nart in articles:
                    addThisUrl = True
                    for eart in entry['articles']:
                        if eart['title'] == nart.title:
                            addThisUrl = False
                            break

                    if addThisUrl:
                        nentry = {
                            "title": nart.title,
                            "link": nart.link,
                            "desc": nart.content,
                            "pub_date": nart.pubDate,
                            "pub_date_parsed": nart.pub_date_parsed,
                            "seen": False,
                        }

                        entry['articles'].append(nentry)

        dbh.add_entry(username, res)
예제 #4
0
    def test_pickle_storing(self):
        db = DatabaseHandler(db_path="pytests/", dbIsTemp=True)

        l = [str(i) for i in range(100)]
        db.add_entry('testkey', l)

        res = db.get_entry('testkey')
        self.assertNotEqual(res, None)

        self.assertEqual(res, l)

        DatabaseHandler.destroy_database()
예제 #5
0
    def test_basic_operations(self):
        db = DatabaseHandler(db_path="pytests/", dbIsTemp=True)

        db.add_entry('testkey', 'testvalue')

        res = db.get_entry('testkey')
        self.assertEqual(res, 'testvalue')

        res = db.delete_entry('testkey')
        self.assertEqual(res, True)

        res = db.get_entry('nonvalidtestkey')
        self.assertEqual(res, None)

        DatabaseHandler.destroy_database()
예제 #6
0
    def remove_url_from_group(url, group):
        """
        Removes url from selected group in database

        Args:
            url ([type]): [description]
            group ([type]): [description]
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)

        for i, entry in enumerate(res['urls']):
            if entry['actual_url'] == url:
                if group in res['groups'] and i in res['groups'][group]:
                    res['groups'][group].remove(i)
                    dbh.add_entry(username, res)

                return
예제 #7
0
    def set_article_seen(url, seen):
        """
        After reading article this method is used to inform database about
        viwing it

        Args:
            url (string): url to article
            seen (bool): true if article has been seen, otherwise false 
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)

        for i, entry in enumerate(res['urls']):
            for j, article in enumerate(entry['articles']):
                if article['link'] == url:
                    res['urls'][i]['articles'][j]['seen'] = seen
                    break

        dbh.add_entry(username, res)
예제 #8
0
    def add_group(group):
        """
        Add group to the database if one doesn't exists

        Args:
            group (string): name of the group

        Returns:
            bool: True if group has been added succesfully
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)

        if any([entry for entry in res['groups'] if group in entry]):
            return False

        res['groups'][group] = []
        dbh.add_entry(username, res)
        return True
예제 #9
0
    def remove_group(group):
        """
        Remove group from the database

        Args:
            group (string): name of the group

        Returns:
            bool: True if group has been removed succesfully
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)

        for i, entry in enumerate(res['groups']):
            if group in entry:
                res['groups'].pop(group)
                dbh.add_entry(username, res)
                return True
        return False
예제 #10
0
    def create_user(self):
        """

        """
        dbh = DatabaseHandler()

        value = {
            'password': self.password,
            'urls': [],
            'groups': {
                'All': [],
            },
        }

        result = dbh.add_entry(self.username, value)
        return result
예제 #11
0
    def add_url(url):
        """
        Adds url to database

        Args:
            url (string): url to rss.xml 

        Returns:
            int: index of newly added url in database, returns -1 if url was already in use and -2 if url wasn't in all directory
        """
        dbh = DatabaseHandler()

        username = CredentialsHandler.lastUsername
        res = dbh.get_entry(username)
        ret_index = -1
        url_index = -1
        for i, entry in enumerate(res['urls']):
            if entry['actual_url'] == url:
                url_index = i
                break
        if url_index == -1:
            new_entry = {
                'actual_url': url,
                'rss_title': None,
                'rss_link': None,
                'rss_desc': None,
                'articles': [],
            }

            res['urls'].append(new_entry)
            ret_index = len(res['urls'])-1
            dbh.add_entry(username, res)
        elif url_index in res['groups']['All']:
            return ret_index
        elif url_index not in res['groups']['All']:
            ret_index = -2
        stats = dbh.get_entry("__all_urls_statistics__")
        if stats == None:
            stats = []
            stats.append([url, 1])
            dbh.add_entry("__all_urls_statistics__", stats)
            return ret_index
        url_exists = False
        for i, stat in enumerate(stats):
            if url in stat:
                url_exists = True
                stats[i][1] += 1
                break
        if not url_exists:
            stats.append([url, 1])
        dbh.add_entry("__all_urls_statistics__", stats)
        return ret_index