def read_db(self): # Read all news news_cursor = self.connection.cursor() news_cursor.execute('SELECT * FROM News') news = news_cursor.fetchall() # Create object for all news if len(news) > 0: for single_news in news: self.content.append(News(single_news[0], single_news[1])) news_cursor.close() # Read all ads ads_cursor = self.connection.cursor() ads_cursor.execute('SELECT * FROM PrivateAds') ads = ads_cursor.fetchall() # Create object for all ads if len(ads)>0: for single_ad in ads: self.content.append(PrivateAd(single_ad[0], single_ad[1])) ads_cursor.close() # Read all tips tips_cursor = self.connection.cursor() tips_cursor.execute('SELECT * FROM Tips') tips = tips_cursor.fetchall() # Create object for all tips if len(tips)>0: for single_tip in tips: self.content.append(Tip(single_tip[0])) tips_cursor.close() # Return the list of content objects return self.content
def parse_newspaper(self, input_filename='.\\input\\input.txt'): # Read source newspaper input_file = open(input_filename, "r") input_text = input_file.read() input_file.close() # Split content to paragraphs paragraphs = input_text.split("\n") # Parse content str_num = 0 while str_num < len(paragraphs): current_str = paragraphs[str_num] if current_str[:5] == 'Type:': if current_str[6:] == 'Tip of the day': body = paragraphs[str_num + 1] body = Paragraph(body[6:]).normalized self.content.append(Tip(body)) str_num += 2 elif current_str[6:] == 'News': body = paragraphs[str_num + 1] body = Paragraph(body[6:]).normalized city = paragraphs[str_num + 2] self.content.append(News(body, city[6:])) str_num += 4 elif current_str[6:] == 'Private Ad': body = paragraphs[str_num + 1] body = Paragraph(body[6:]).normalized expiration_time = paragraphs[str_num + 2] self.content.append(PrivateAd(body, expiration_time[17:])) str_num += 4 else: print("Format error: Unknown or missing content type") str_num += 1
def read_content(self, input_filename='.\\input\\input.json'): self.content = [] # Read source content _file = open(input_filename, "r") input_text = _file.read() json_content = json.loads(input_text) for _record in json_content: if _record['Type'] == 'News': self.content.append(News(_record['Body'], _record['City'])) elif _record['Type'] == 'Tip of the day': self.content.append(Tip(_record['Body'])) elif _record['Type'] == 'Private Ad': self.content.append( PrivateAd(_record['Body'], _record['Expiration date'])) _file.close()
def ReadXML(self, input_filename='.\\input\\input.xml'): self.content = [] # Parse XML data tree = ET.parse(input_filename) root = tree.getroot() # Read news news = root.find('news') if news != None: for anews in news.findall('news'): self.content.append(News(anews[0].text, anews[1].text)) # Read Private Ads privateads = root.find('privateads') if privateads != None: for ad in privateads.findall('privatead'): self.content.append(PrivateAd(ad[0].text, ad[1].text)) # Read Tips tips = root.find('tips') if tips != None: for tip in tips.findall('tip'): self.content.append(Tip(tip[0].text))
elif mode == -1: # Define filename timestamp = datetime.now() filename = timestamp.strftime("%Y-%m-%d_%H-%M-%S") + ".txt" elif mode == 1: text = input("News text:\t") city = input("City:\t") newspaper.content.append(News(text, city)) elif mode == 2: text = input("Ads text:\t") exp_date = input("Expiration date DD/MM/YYYY:\t") newspaper.content.append(PrivateAd( text, exp_date)) # Create new peace of private ad elif mode == 3: text = input("Tip text:\t") newspaper.content.append(Tip(text)) # Create new peace of tip else: input( "Options are limited to 0-3 range.\nPress enter to continue..." ) break elif input_format == 2: input_filename = input( "Enter input filename or press Enter to use .\input\input.txt:") if input_filename == "": newspaper.parse_newspaper() else: newspaper.parse_newspaper(input_filename) break elif input_format == 3: input_filename = input(
input_text = input_file.read() input_file.close() # Split content to paragraphs paragraphs = input_text.split("\n") content = [] # Parse content str_num = 0 while str_num < len(paragraphs): current_str = paragraphs[str_num] if current_str[:5] == 'Type:': if current_str[6:] == 'Tip of the day': body = paragraphs[str_num + 1] body = Paragraph(body[6:]).normalized content.append(Tip(body)) str_num += 2 elif current_str[6:] == 'News': body = paragraphs[str_num + 1] body = Paragraph(body[6:]).normalized city = paragraphs[str_num + 2] content.append(News(body, city[6:])) str_num += 4 elif current_str[6:] == 'Private Ad': body = paragraphs[str_num + 1] body = Paragraph(body[6:]).normalized expiration_time = paragraphs[str_num + 2] days_to_expire = paragraphs[str_num + 3] content.append(PrivateAd(body, expiration_time[17:])) str_num += 4 else:
# Define filename timestamp = datetime.now() filename = timestamp.strftime("%Y-%m-%d_%H-%M-%S") + ".txt" elif mode == 1: text = input("News text:\t") city = input("City:\t") newspaper.content.append(News(text, city)) elif mode == 2: text = input("Ads text:\t") exp_date = input("Expiration date DD/MM/YYYY:\t") # Create new peace of private ad newspaper.content.append(PrivateAd(text, exp_date)) elif mode == 3: text = input("Tip text:\t") # Create new peace of tip newspaper.content.append(Tip(text)) else: input( "Options are limited to 0-3 range.\nPress enter to continue..." ) break elif input_format == 2: input_filename = input( "Enter input filename or press Enter to use .\input\input.txt:") if input_filename == "": newspaper.parse_newspaper() else: newspaper.parse_newspaper(input_filename) break elif input_format == 3: input_filename = input(