()
| 11 | |
| 12 | |
| 13 | def main(): |
| 14 | # loads .env variables |
| 15 | load_dotenv() |
| 16 | API_KEY = os.getenv("NEWS_API_KEY") |
| 17 | |
| 18 | # check validity of command-line arguments |
| 19 | try: |
| 20 | if len(sys.argv) == 2: |
| 21 | news_query = sys.argv[1] |
| 22 | else: |
| 23 | raise IndexError() |
| 24 | except IndexError: |
| 25 | sys.exit("Please provide correct number of command-line arguments") |
| 26 | |
| 27 | try: |
| 28 | # get number of articles from user |
| 29 | while True: |
| 30 | try: |
| 31 | num_articles = int(input("Enter number of articles: ")) |
| 32 | break |
| 33 | except ValueError: |
| 34 | continue |
| 35 | |
| 36 | # fetch news articles based on user's query |
| 37 | articles = fetch_news(API_KEY, query=news_query, max_articles=num_articles) |
| 38 | |
| 39 | # output printing title, summary and no. of words in the summary |
| 40 | for i, article in enumerate(articles): |
| 41 | capitalized_title = capitalize_title(article["title"]) |
| 42 | print(f"\n{i + 1}. {capitalized_title}") |
| 43 | |
| 44 | content = article.get("content") or article.get("description") or "" |
| 45 | if not content.strip(): |
| 46 | print("No content to oversimplify.") |
| 47 | continue |
| 48 | |
| 49 | summary = summarize_text(content) # returns summary |
| 50 | count = word_count(summary) # returns word count |
| 51 | print(f"\nOVERSIMPLIFIED:\n{summary}\n{count} words\n") |
| 52 | |
| 53 | # ask user whether they want to save the output in a txt file |
| 54 | while True: |
| 55 | saving_status = ( |
| 56 | input("Would you like to save this in a text file? (y/n): ") |
| 57 | .strip() |
| 58 | .lower() |
| 59 | ) |
| 60 | if saving_status == "y": |
| 61 | save_summary(article["title"], summary) |
| 62 | break |
| 63 | elif saving_status == "n": |
| 64 | break |
| 65 | else: |
| 66 | print("Try again\n") |
| 67 | continue |
| 68 | |
| 69 | except Exception as e: |
| 70 | print("ERROR:", e) |
no test coverage detected