Advertisement
Guest User

Untitled

a guest
Feb 13th, 2023
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. # You shouldn't change  name of function or their arguments
  2. # but you can change content of the initial functions.
  3. from argparse import ArgumentParser
  4. from typing import List, Optional, Sequence
  5. import requests
  6.  
  7.  
  8. class UnhandledException(Exception):
  9.     pass
  10.  
  11.  
  12. def rss_parser(
  13.     xml: str,
  14.     limit: Optional[int] = None,
  15.     json: bool = False,
  16. ) -> List[str]:
  17.     """
  18.    RSS parser.
  19.  
  20.    Args:
  21.        xml: XML document as a string.
  22.        limit: Number of the news to return. if None, returns all news.
  23.        json: If True, format output as JSON.
  24.  
  25.    Returns:
  26.        List of strings.
  27.        Which then can be printed to stdout or written to file as a separate lines.
  28.  
  29.    Examples:
  30.        >>> xml = '<rss><channel><title>Some RSS Channel</title><link>https://some.rss.com</link><description>Some RSS Channel</description></channel></rss>'
  31.        >>> rss_parser(xml)
  32.        ["Feed: Some RSS Channel",
  33.        "Link: https://some.rss.com"]
  34.        >>> print("\\n".join(rss_parser(xmls)))
  35.        Feed: Some RSS Channel
  36.        Link: https://some.rss.com
  37.    """
  38.     # Your code goes here
  39.  
  40. def main(argv: Optional[Sequence] = None):
  41.     """
  42.    The main function of your task.
  43.    """
  44.     parser = ArgumentParser(
  45.         prog="rss_reader",
  46.         description="Pure Python command-line RSS reader.",
  47.     )
  48.     parser.add_argument("source", help="RSS URL", type=str, nargs="?")
  49.     parser.add_argument(
  50.         "--json", help="Print result as JSON in stdout", action="store_true"
  51.     )
  52.     parser.add_argument(
  53.         "--limit", help="Limit news topics if this parameter provided", type=int
  54.     )
  55.  
  56.     args = parser.parse_args(argv)
  57.     xml = requests.get(args.source).text
  58.     try:
  59.         print("\n".join(rss_parser(xml, args.limit, args.json)))
  60.         return 0
  61.     except Exception as e:
  62.         raise UnhandledException(e)
  63.  
  64.  
  65. if __name__ == "__main__":
  66.     main()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement