def update_article_description(article_id): article = db.get_article(article_id) if article == None: print("There is no article with that ID. article NOT found.\n") else: print() display_single_article(article, str(article.id)) article_choice = btc.read_int_ranged('1 to edit article description, 2 to leave as is: ' , min_value = 1, max_value = 2) if article_choice == 1: description_choice = btc.read_text('View article description? y/n: ') if description_choice == 'y': article_summary = na.get_article_summary(article.link) print(article_summary) # article_text = Article.get_text(article.link) # #article_text = dm.get_cleaned_text(link) # article_text = article_text.split() # article_text = [i for i in article_text if de.isEnglish(i) == True] # article_text = ' '.join(article_text) # print(article_text) new_description = btc.read_text('Enter new description or "." to cancel: ') if new_description != '.': db.update_article_description(article_id, new_description) print('Article description updated.\n') else: print('Edit cancelled, article description unchanged') else: print('Edit cancelled, article description unchanged')
def scrape_article_name(article_id): article = db.get_article(article_id) if article == None: print('There is no article with that ID. article NOT found.\n') else: print() display_single_article(article, str(article.id)) article_choice = btc.read_int_ranged('1 to rescrape title, 2 to leave as is: ', min_value = 1, max_value = 2) if article_choice == 1: try: new_article_news_item = na.get_article_from_url(article.link) new_title = new_article_news_item.title #new_title = na.get_article_title(article.link) #new_title = Article.get_title(article.link) print(''' New title: {0} Old title: {1}'''.format(new_title, article.name)) except: new_title = 'Title scrape failed' title_choice = btc.read_int_ranged('1 to replace title, 2 to keep original title: ', min_value = 1, max_value = 2) if title_choice == 1: db.update_article_name(article_id, new_title) elif title_choice == 2: print('article update cancelled') elif article_choice == 2: print('article update cancelled')
def update_article_name(article_id): article = db.get_article(article_id) if article == None: print("There is no article with that ID. article NOT found.\n") else: print() display_single_article(article, str(article.id)) article_choice = btc.read_int_ranged('1 to edit article title, 2 to leave as is: ' , min_value = 1, max_value = 2) if article_choice == 1: try: newsItem1 = na.get_article_from_url(article.link) updated_title = newsItem1.title except Exception as e: print('Scrape failed because of {0}'.format(e)) updated_title = 'Invalid' print('Rescraped title: {0}'.format(updated_title)) title_choice = btc.read_int_ranged('1 - existing title, 2 - scraped title, 3 - manual input: ', 1, 3) if title_choice == 1: print('Title update cancelled, article title unchanged.') return elif title_choice == 2: db.update_article_name(article_id, updated_title) print('Title update complete. Return to main menu.') elif title_choice == 3: new_title = btc.read_text('Enter new title or . to cancel: ') if new_title != '.': db.update_article_name(article_id, new_title) else: print('Edit cancelled, return to main menu') return else: print('Edit cancelled, article title unchanged')
def export_roundup(): roundup_title = btc.read_text('Enter the roundup title or "." to cancel: ') filename = btc.read_text('Enter the filename or "." to cancel: ') if roundup_title != '.': roundup_categories = db.get_categories() for category in roundup_categories: category.articles = db.get_articles_by_category_id(category.id) roundup_docx.create_complete_roundup(filename=filename, roundup_title=roundup_title, categories=roundup_categories)
def export_roundup_by_year(): roundup_title = btc.read_text('Enter the roundup title: ') roundup_year = btc.read_int_ranged('Enter roundup year: ', 1, 2100) filename = btc.read_text('Enter roundup filename: ') roundup_choice = btc.read_int_ranged('Enter 1 to export roundup, 2 to cancel: ', 1, 2) if roundup_choice == 1: roundup_categories = db.get_categories() for category in roundup_categories: category.articles = db.yearly_roundup_articles(roundup_year, category.id) roundup_docx.create_complete_roundup(filename=filename, roundup_title=roundup_title, categories=roundup_categories) #display_title() elif roundup_choice == 2: print('Roundup export cancelled. Return to main menu.\n')
def update_category(category_id=0): if category_id == 0: category_id = int(input("category ID: ")) category = db.get_category(category_id) articles = db.get_articles_by_category_id(category_id) display_articles(articles, category.name.upper()) new_category_name = btc.read_text("Enter new category name or '.' to cancel: ") if new_category_name != '.': update_choice = btc.read_int_ranged("1 to change article name to {0}, 2 to cancel: ".format(new_category_name), 1, 2) if update_choice == 1: db.update_category(category_id, new_category_name) print('Category update complete\n') elif update_choice == 2: print('Update cancelled.\n')
def update_article_author(article_id): article = db.get_article(article_id) if article == None: print("There is no article with that ID. article NOT found.\n") else: print() display_single_article(article, str(article.id)) article_choice = btc.read_int_ranged('1 to edit article author, 2 to leave as is: ' , min_value = 1, max_value = 2) if article_choice == 1: new_author = btc.read_text('Enter new author name or . to cancel: ') if new_author != '.': db.update_article_author(article_id, new_author) else: print('Edit cancelled, article title unchanged')
def get_stats(command): del command stats_choice = btc.read_int_ranged('1 - monthly stats; 2 - yearly stats; 3 - main menu: ', 1, 3) if stats_choice in range(1, 3): year = btc.read_int_ranged('Enter article year: ', 1, 2100) if stats_choice in range(1, 2): month = btc.read_int_ranged('Enter article month: ', 1, 12) get_monthly_category_stats(month, year) else: get_yearly_category_stats(year) else: time.sleep(0.25) print('Returning to main menu.\n') time.sleep(0.25)
def get_csv_in_directory(): file_list = glob.glob('*.csv') print('Files available: ') for item in file_list: print(item, end='\n') #try: importing_file_name = btc.read_text('Enter filename from the list or ". " to cancel: ') if importing_file_name != '.': filename='{0}.csv'.format(importing_file_name) csv_articles = create_csv_list(filename) print(csv_articles) #csv_articles = [csv_item_to_article(csv_article) for csv_article in csv_articles] print('Articles to import:') try: for article in csv_articles: try: csv_article = csv_item_to_article(article) try: csv_article.name = Article.get_title(csv_article.link) except Exception as e: print(e) csv_article.name = 'Not specified' db.add_article_from_csv(csv_article) print(csv_article.name + " was added to database.\n") #print('Import complete, return to main menu \n') except Exception as e: print(e) print('Article import failed.') continue print('Import complete, return to main menu') except Exception as e: print(e)
def finalize_desc_month(command): if not command or command == '': new_month = btc.read_int_ranged('Enter new month: ', min_value = 1, max_value = 12) new_year = btc.read_int_ranged('Enter new year: ', min_value = 1, max_value = 2100) articles_to_finalize = db.get_articles_by_month(month=new_month, year=new_year) articles_remaining = len(articles_to_finalize) for article in articles_to_finalize: print('{0} unreviewed articles'.format(articles_remaining)) update_article_description(article.id) description_choice = btc.read_int_ranged('{0} descriptions remaining. Press 1 to continue, 2 to cancel: '.format(articles_remaining), 1, 2) articles_remaining -= 1 if description_choice == 2: print('Update descriptions cancelled') break
def display_articles_by_publication(): publication_snippet = btc.read_text("Publication: ") publications = db.display_articles_by_publication(publication_snippet) if publications == None: print("There are no articles by that publication. article NOT found.\n") else: print() display_articles(publications, "PUBLICATION: " + str(publications[0].publication))
def display_articles_by_author(): author_snippet = btc.read_text("Author Name: ") articles = db.display_articles_by_author(author_snippet) if articles == None: print("There are no articles by that author. article NOT found.\n") else: print() #display_single_article(article, str(article.id)) display_articles(articles, "AUTHOR: " + str(articles[0].author))
def update_article_category(article_id): article = db.get_article(article_id) if article == None: print("There is no article with that ID. article NOT found.\n") else: print() display_single_article(article, str(article.id)) article_choice = btc.read_int_ranged('1 to edit article category, 2 to leave as is: ' , min_value = 1, max_value = 2) if article_choice == 1: new_category_id = btc.read_int('Enter new category_id: ') result = db.get_category(new_category_id) if result == None: print('There is no category with that ID, article category NOT updated.\n') else: db.update_article_category(article_id, new_category_id) else: print('Edit cancelled, article title unchanged')
def display_articles_by_name(): title_snippet = btc.read_text('Enter article title or "." to cancel: ') if title_snippet != '.': result = db.display_article_by_name(title_snippet) if result == None: print('There is no article with that name.\n') else: display_single_article(result, str(result.id)) else: print('Search cancelled, returning to main menu.')
def finalize_article_descriptions(month, year=2019): undescribed = db.finalize_descriptions(month, year) undescribed_articles = len(undescribed) print('{0} undescribed articles'.format(undescribed_articles)) for article in undescribed: print('{0} undescribed articles'.format(undescribed_articles)) update_article_description(article.id) description_choice = btc.read_int_ranged('{0} descriptions remaining. Press 1 to continue, 2 to cancel: '.format(undescribed_articles), 1, 2) if description_choice == 2: print('Update descriptions cancelled') break
def get_articles_by_category(): category = btc.read_text("Enter category name or number here: ") if category.isalpha() == True: display_articles_by_category_name(category) elif category.isnumeric() == True: try: category_id = int(category) display_articles_by_category_id(category_id) except: print('Article search cancelled. Return to main menu.\n')
def delete_category(): category_id = int(input("category ID: ")) articles_in_category = db.get_articles_by_category_id(category_id) if len(articles_in_category) > 0: print('Category contains articles, cannot be deleted') elif len(articles_in_category) == 0: delete_choice = btc.read_float_ranged('Press 1 to delete, 2 to cancel: ', 1, 2) if delete_choice == 1: db.delete_category(category_id) print('Category deleted.\n') elif delete_choice == 2: print('Delete cancelled, returning to category menu')
def update_article_date(article_id): article = db.get_article(article_id) if article == None: print("There is no article with that ID. article NOT found.\n") else: print() display_single_article(article, str(article.id)) article_choice = btc.read_int_ranged('1 to edit article date, 2 to leave as is: ' , min_value = 1, max_value = 2) if article_choice == 1: new_day = btc.read_int_ranged('Enter new day: ', min_value = 1, max_value = 31) new_month = btc.read_int_ranged('Enter new month: ', min_value = 1, max_value = 12) new_year = btc.read_int_ranged('Enter new year: ', min_value = 1, max_value = 2100) date_choice = btc.read_int_ranged('1 to change date to: {0}/{1}/{2}, 2 to cancel: '.format(new_month, new_day, new_year), min_value=1, max_value=2) if date_choice == 1: db.update_article_date(article_id, new_day, new_month, new_year) print('Update complete.\n') elif date_choice == 2: print('Edit cancelled, article date unchanged') else: print('Edit cancelled, article date unchanged')
def date_search(search_choice): #search_choice = btc.read_int_ranged('Options:\n1 - day\n2 - month\n3 - year\n4 - cancel\nEnter your choice: ', 1, 4) if search_choice in range(1, 4): #user searches by year year = btc.read_int('Year: ') if search_choice in range(1, 3): month = btc.read_int('Month: ') if search_choice in range(1, 2): day = btc.read_int('Day: ') try: assert datetime.date(day=day, month=month, year = year) display_articles_by_date(year, month, day) except ValueError: print('Invalid date. Return to main menu.') return #if Article.validate_date(day=day, month=month, year=year) == True: #display_articles_by_date(year, month, day) #else: # print('Invalid date selected') else: display_articles_by_month(month, year) else: display_articles_by_year(year) else: print('Search cancelled. ')
def finalize_title_updates(month, year): articles = db.get_articles_by_month(month=month, year=year) articles_remaining = len(articles) for article in articles: print('{0} articles remaining'.format(articles_remaining)) display_single_article(article, title_term = article.id) strip_choice = btc.read_int_ranged('1 to update title, 2 to skip, 3 to return to main menu: ', 1, 3) if strip_choice == 1: update_article_name(article.id) articles_remaining -= 1 if strip_choice == 2: articles_remaining -= 1 print('Article title unchanged.') if strip_choice == 3: print('strip titles cancelled') #display_title() break
def manual_add(link=None): if (link == None) or (not link): print('Link', link) print('No link supplied, manual_add must be followed by link.') return else: print('Manual article creation/n') print('Link: {0}'.format(link)) display_categories() #print(link) #if link == None: # print('No link supplied, manual_add must be followed by link.') new_article_category = btc.read_int('Enter category for article: ') category = db.get_category(new_article_category) assert category != None new_article = Article.from_input(link=link, category=category) if new_article == None: print('Article creation cancelled. Returning to main menu.') return db.add_article(new_article) print(new_article.title + " was added to database.\n")
def export_roundup_by_category(): display_categories() roundup_categories = db.get_categories() categories_remaining = len(roundup_categories) categories_for_roundup = [] for category in roundup_categories: print('Categories remaining: {0}'.format(categories_remaining)) print('Include {0}'.format(category.name)) category_choice = btc.read_int_ranged('1 to include, 2 to exclude: ', 1, 2) if category_choice != 1: categories_for_roundup.append(category) roundup_title = btc.read_text('Enter the roundup title: ') roundup_month = btc.read_int_ranged('Enter roundup month: ', 1, 12) roundup_year = btc.read_int_ranged('Enter roundup year: ', 1, 2100) filename = btc.read_text('Enter roundup filename: ') roundup_choice = btc.read_int_ranged('Enter 1 to export roundup, 2 to cancel: ', 1, 2) if roundup_choice == 1: for category in categories_for_roundup: # for category in roundup_categories: category.articles = db.get_articles_for_roundup(roundup_month, roundup_year, category.id) roundup_docx.create_complete_roundup(filename=filename, roundup_title=roundup_title, categories=categories_for_roundup) #display_title() elif roundup_choice == 2: print('Roundup export cancelled. Return to main menu.\n')
def add_article_from_newspaper(link): ''' Adds an article from the newspaper module after downloading it ''' try: #link = btc.read_text('Link or "." to cancel: ') #try: for i in tqdm.tqdm(range(1)): newNewsItem = na.get_article_from_url(link) print(newNewsItem) #except Exception as e: #print(e) #get article title try: name = newNewsItem.title #get the title for the article print('NameTest {0}'.format(name)) except Exception as e: print(e) name = btc.read_text('Please enter title: ') #get article author try: author = ' '.join(newNewsItem.authors) #get article publication except Exception as e: print(e) author = btc.read_text('Please enter author: ') try: #works for most websites, but not Sudan Tribune publication = newNewsItem.meta_data['og']['site_name'] except Exception as e: print(e) publication = btc.read_text('Please enter publication: ') try: year = newNewsItem.publish_date.year except Exception as e: print(e) year = btc.read_int_ranged('Please enter year: ', 1, 2200) try: month = newNewsItem.publish_date.month except Exception as e: print(e) month = btc.read_int_ranged('Please enter month: ', 1, 12) try: day = newNewsItem.publish_date.day except Exception as e: print(e) day = btc.read_int_ranged('Please enter day: ', 1, 31) try: summary = newNewsItem.summary except Exception as e: print(e) print('Summary download failed') summary = 'Summary not found' try: keywords = ', '.join(newNewsItem.keywords) except Exception as e: print(e) print('Keyword download failed') keywords= 'keywords not found' print('TITLE - {0} - AUTHOR {1}'.format(name, author)) print('DATE - {0}/{1}/{2} - PUBLICATION {3}'.format(month, day, year, publication)) #print(author) #print(publication) #print('{0}/{1}/{2}'.format(month, day, year)) #print(summary) print('KEYWORDS: ', keywords) display_categories() category_id = btc.read_text("Category ID: ") category = db.get_category(category_id) if category == None: print('There is no category with that ID. article NOT added.\n') return description_choice = btc.read_text('View article description? y/n: ') if description_choice == 'y': print('Title: {0}'.format(name)) print('Summary: {0}'.format(summary)) print('Keywords: {0}'.format(keywords)) description = btc.read_text("Description or '.' to cancel: ") if description == ".": return else: article = Article(name=name, year=year, month=month,day=day, category=category, link=link, description=description, author=author, publication=publication) db.add_article(article) print(name + " was added to database.\n") except Exception as e: print('Article download failed.', e)