def select_random_unused_file(self, bool_many_folders, cdir,
                                  used_photos_data):
        if bool_many_folders:
            # select random folder in currect directory
            directory = dir_actions.select_random_folder(cdir)
        else:
            directory = cdir

        dataparser = DataParser()
        same_photo = True
        while same_photo:
            # select a random file name from path
            random_filename = dir_actions.select_rand_file(directory)

            # random_filename_path = directory + "\\" + random_filename
            random_filename_path = directory + "/" + random_filename

            # check if filename was used before
            same_photo = dataparser.value_exists_in_kv(used_photos_data,
                                                       random_filename_path,
                                                       "filename")
            # check if its not infogeneral.json
            if same_photo == False and random_filename == "infogeneral.json":
                same_photo = True

            # try a different folder
            if same_photo and bool_many_folders:
                directory = dir_actions.select_random_folder(cdir)

        return directory, random_filename_path, random_filename
Example #2
0
    def get_text(self,
                 selection_alg,
                 data_file,
                 data_format,
                 directory=None,
                 file_name=None,
                 application=None,
                 db_engine="sqlite"):
        if selection_alg == "random":
            if data_format == "database":
                if application == "quotes":
                    # (quote, author) =
                    general_info = self.get_quote(db_engine, data_file)
                if application == "poetry":
                    general_info = self.get_poem_data(db_engine, data_file)
        elif selection_alg == "direct":
            if data_format == "txt":
                general_info = txt_actions.read_txt(data_file)
        elif selection_alg == "description":
            if data_format == "json":
                parser = Parser()
                general_info = parser.get_values_json(directory, data_file)
            elif data_format == "database":
                general_info = self.get_scifi_description(
                    db_engine, data_file, file_name)

        return general_info
    def get_content(self, content_parameters, content_type):
        parser = DataParser()
        content = []
        info = None
        # print(content_parameters)
        for content_params in content_parameters:
            # print(content_params)
            if content_params["selection_alg"] == "description":
                description = self.filechooser.get_content_description(
                    content.pop(), content_params, info, content_type)
                content.append(description)
            else:
                # returns: file_dir, dir_only, file_only
                info = self.new_get_content(content_params)
                if isinstance(info, str):
                    item = parser.format_item_kv(
                        content_params["content_type"], info)
                else:
                    item = parser.format_item_kv(
                        content_params["content_type"], info[0])
                    if len(content_parameters) == 1:
                        tmp_list = []
                        tmp_list.append(item)
                        item = parser.format_item_kv(
                            content_params["content_type"], tmp_list)

                if content_type == 'list':
                    tmp_list = []
                    tmp_list.append(item)
                    item = parser.format_item_kv(
                        content_params["content_type"], tmp_list)

                content.append(item)
        return content
    def new_get_content(self, params, dir_only=None, file_only=None):
        directory = None
        br_history = None
        parser = DataParser()
        file_chooser = FileChooser()
        if (params["content_type"] == "image"
                or params["content_type"] == "audio"):
            if 'directory' in params:
                directory = parser.substr(stra=params['directory'],
                                          strb=getpass.getuser())
            if 'broadcast_history' in params:
                br_history = parser.substr(stra=params['broadcast_history'],
                                           strb=getpass.getuser())
            rec_fold = None
            if 'recursive_folders' in params:
                rec_fold = params['recursive_folders']
            br_hist_ftype = None
            if 'broadcast_history_ftype' in params:
                br_hist_ftype = params['broadcast_history_ftype']

            # file_loc, dir_only, file_name
            return file_chooser.get_file(params['selection_alg'], rec_fold,
                                         directory, br_history, br_hist_ftype)
        elif params["content_type"] == "text":
            data_file = parser.substr(stra=params['data_file'],
                                      strb=getpass.getuser())

            general_info = file_chooser.get_text(
                directory=dir_only,
                selection_alg=params['selection_alg'],
                data_file=data_file,
                data_format=params['data_format'],
                file_name=file_only)
            text = parser.format_caption(params['format_rules'], general_info)
            return text
Example #5
0
    def test_substr_bool(self):
        parser = Parser()

        result_a = parser.substr_bool(True, "Hello %s", "Bye")
        self.assertEqual("Hello Bye", result_a)

        result_b = parser.substr_bool(False, "Hello", "Bye")
        self.assertEqual("Hello", result_b)
Example #6
0
 def __init__(self, credentials):
     parser = Parser()
     #tele_sess = parser.substr(TELE_session, getpass.getuser())
     #self.client = TC(tele_sess, TELE_api_id, TELE_api_hash)
     tele_sess = parser.substr(credentials["TELE_session"],
                               getpass.getuser())
     self.client = TC(tele_sess, credentials["TELE_api_id"],
                      credentials["TELE_api_hash"])
     self.client.start()
Example #7
0
    def setUp(self):
        self.mockinfo = Mockinfo()
        self.parser = Parser()

        self.scifi_data = (11, 'Aedel Fakhrie', '',
                           'https://www.artstation.com/aedel', '', None, None,
                           None)
        self.expected_scifi = 'Artist: Aedel Fakhrie | ' \
                              '<a href="https://www.artstation.com/aedel">'\
                              ' Artstation </a> |'
Example #8
0
 def __init__(self, keys):
     parser = Parser()
     #vk_token = parser.substr(vk_token_loc, getpass.getuser())
     vk_token = parser.substr(keys["vk_token_loc"], getpass.getuser())
     self.token = txt_actions.read_txt(vk_token)
     #self.user_id = vk_user_id
     #self.album_id = vk_album_id
     #self.version = vk_version
     self.user_id = keys["vk_user_id"]
     self.album_id = keys["vk_album_id"]
     self.version = keys["vk_version"]
Example #9
0
 def test_format_caption_travel(self):
     expected = 'Photo by: Ales Krivec  | '\
                '<a href="https://www.instagram.com/dreamypixels">'\
                ' Instagram </a> |'
     format_caption = [{
         "Photo by: %s ": 0
     }, {
         ' | <a href="%s"> Instagram </a> |': 3
     }]
     parser = Parser()
     caption_result = parser.format_caption(format_caption,
                                            self.mockinfo.travel_values)
     self.assertEqual(expected, caption_result)
    def get_content_description(self, content_item, content_params, info,
                                content_type):
        united_content = []
        united_content.append(content_item)

        # not supposed to be here
        dataparser = DataParser()

        item_text = dataparser.format_item_kv(
            "text", self.new_get_content(content_params, info[1], info[2]))
        united_content.append(item_text)

        item = dataparser.format_item_kv(content_type, united_content)
        return item
Example #11
0
    def test_get_values_json(self):
        expected_info = "Ales Krivec", "", "",\
                        "https://www.instagram.com/dreamypixels", "",\
                        "https://www.facebook.com/aleskphotography",\
                        "https://500px.com/gljivec", "", ""

        parser = Parser()

        home = expanduser("~")
        test_dir = home + "/Projects/social-networks-poster/test/mock_data"

        info = parser.get_values_json(self.mockinfo.mockdir,
                                      "infogeneral.json")

        self.assertEqual(expected_info, info)
    def get_content_requirements(self, json_options):
        social_network = json_options["social_network"]
        blog_username = json_options["blog_username"]
        tags = json_options["tags"]
        application = json_options["application"]

        content = Content(social_network, blog_username, tags, application)
        parser = DataParser()

        #content_item_format = json_options["content_type"]

        for item_param in json_options["content"]:
            if item_param["content_type"] == "text":
                data_file = parser.substr(stra=item_param["data_file"],
                                          strb=getpass.getuser())

                item_text = ContentText(item_param["content_type"],
                                        item_param["selection_alg"],
                                        item_param["data_format"], data_file,
                                        item_param["format_rules"], None)
                content.add_content_item(item_text)
            elif item_param["content_type"] == "image":
                directory = parser.substr(stra=item_param["directory"],
                                          strb=getpass.getuser())
                br_history = parser.substr(
                    stra=item_param['broadcast_history'],
                    strb=getpass.getuser())

                item_image = ContentImage(
                    item_param["content_type"],
                    item_param["selection_alg"],
                    directory,
                    item_param["recursive_folders"],
                    history_ftype=item_param["broadcast_history_ftype"],
                    history_loc=br_history)
                content.add_content_item(item_image)

        return content
Example #13
0
 def create_item(self, content_item):
     parser = Parser()
     filechooser = FileChooser()
     textchooser = TextChooser()
     content_params = content_item.content_item[0]
     if content_params.content_type == "text":
         general_info = textchooser.get_text(
             content_params.selection_alg,
             content_params.data_file,
             content_params.data_format,
             None,
             None,
             application=content_item.application)
         if content_item.application == "poetry":
             content_item.item_text = parser.format_poem(
                 general_info[0], general_info[1])
         else:
             content_item.item_text = parser.format_caption(
                 content_params.format_rules, general_info)
         return content_item
     elif content_params.content_type == "image":
         image_path = filechooser.get_file(content_params.selection_alg,
                                           content_params.recursive_folders,
                                           content_params.directory,
                                           content_params.history_loc,
                                           content_params.history_ftype)
         content_item.item_image_path = image_path[0]
         if len(content_item.content_item) > 1:
             general_info = textchooser.get_text(
                 content_item.content_item[1].selection_alg,
                 content_item.content_item[1].data_file,
                 content_item.content_item[1].data_format, image_path[1],
                 image_path[2])
             content_item.item_text = parser.format_caption(
                 content_item.content_item[1].format_rules, general_info)
         return content_item
Example #14
0
    def test_get_values_from_kv_pairs(self):
        parser = Parser()

        result = parser.get_values_from_kv_pairs(self.mockinfo.infogeneral)

        self.assertEqual(self.mockinfo.travel_values_list, result)
Example #15
0
class TestParser(unittest.TestCase):
    def setUp(self):
        self.mockinfo = Mockinfo()
        self.parser = Parser()

        self.scifi_data = (11, 'Aedel Fakhrie', '',
                           'https://www.artstation.com/aedel', '', None, None,
                           None)
        self.expected_scifi = 'Artist: Aedel Fakhrie | ' \
                              '<a href="https://www.artstation.com/aedel">'\
                              ' Artstation </a> |'

    def test_substr(self):
        expected = "Photo by: Yours Truly "
        stra = "Photo by: %s "
        strb = "Yours Truly"

        result = self.parser.substr(stra, strb)
        self.assertEqual(expected, result)

        expected2 = "Yours Truly"
        result2 = self.parser.substr(strb, stra)
        self.assertEqual(expected2, result2)

    def test_format_caption_travel(self):
        expected = 'Photo by: Ales Krivec  | '\
                   '<a href="https://www.instagram.com/dreamypixels">'\
                   ' Instagram </a> |'
        format_caption = [{
            "Photo by: %s ": 0
        }, {
            ' | <a href="%s"> Instagram </a> |': 3
        }]
        parser = Parser()
        caption_result = parser.format_caption(format_caption,
                                               self.mockinfo.travel_values)
        self.assertEqual(expected, caption_result)

    def test_format_caption_scifi(self):
        format_rules_scifi = [{
            "Artist: %s": 1
        }, {
            ' | <a href="%s"> Artstation </a> |': 3
        }, {
            ' | <a href="%s"> Instagram </a> |': 5
        }, {
            ' | <a href="%s"> Deviantart </a> |': 4
        }, {
            ' | <a href="%s"> Website </a> |': 2
        }]
        result_text_scifi = parser.format_caption(format_rules_scifi,
                                                  self.scifi_data)
        self.assertEqual(self.expected_scifi, result_text_scifi)

    def test_substr_bool(self):
        parser = Parser()

        result_a = parser.substr_bool(True, "Hello %s", "Bye")
        self.assertEqual("Hello Bye", result_a)

        result_b = parser.substr_bool(False, "Hello", "Bye")
        self.assertEqual("Hello", result_b)

    def test_get_values_from_kv_pairs(self):
        parser = Parser()

        result = parser.get_values_from_kv_pairs(self.mockinfo.infogeneral)

        self.assertEqual(self.mockinfo.travel_values_list, result)

    def test_get_values_json(self):
        expected_info = "Ales Krivec", "", "",\
                        "https://www.instagram.com/dreamypixels", "",\
                        "https://www.facebook.com/aleskphotography",\
                        "https://500px.com/gljivec", "", ""

        parser = Parser()

        home = expanduser("~")
        test_dir = home + "/Projects/social-networks-poster/test/mock_data"

        info = parser.get_values_json(self.mockinfo.mockdir,
                                      "infogeneral.json")

        self.assertEqual(expected_info, info)