The assignment was to display all the blog post title and author from Planet Fedora in the terminal.
The script is completely able to do this job and for extra it can show user the links to blogposts if needed.
1 #!/usr/bin/env python 2 import feedparser 3 import sys 4 5 def error(): 6 """ 7 Function for any kind of argumental error handling 8 """ 9 print 'Usage: ./rss2parser.py [LINK] [OPTION](optional)'+'\n' 10 print './rss2parser.py: error: you must provide URL like: http://planet.fedoraproject.org/rss*.xml'+'\n' 11 print '[OPTION]'+'\n' 12 print '-l or --link' 13 print ' Print links the posts' 14 sys.exit(-1) 15 16 17 18 def make_food(link, flag=0): 19 """ 20 Function to display post titles, respective author and original link to the posts(optional) 21 """ 22 #Parsing the rss20.xml document 23 feed = feedparser.parse(link) 24 #Counting total posts 25 total_posts = len(feed.entries) 26 #Counter for loop 27 count = 0 28 29 #Loop to do the job 30 while count < total_posts: 31 32 #Fectching author & post titles from 'feed' object 33 author_title = feed.entries[count].title 34 35 #List index 36 i=0 37 length= len(author_title) 38 author='' 39 post_title='' 40 41 #Fetching author 42 while author_title[i] != ':': 43 author = author + author_title[i] 44 i +=1 45 #Fetching title 46 while i < length: 47 post_title = post_title + author_title[i] 48 i +=1 49 count += 1 50 51 #Printing 52 print str(count) + ': Post Title' + post_title 53 print ' Author: ' + author 54 55 #It is optional part when argument -l or --link given then only it will be execute and show all the link to the respective posts 56 if flag == 1: 57 post_link=feed.entries[count-1].link 58 print ' Link: ' + post_link + '\n' 59 60 if __name__ == '__main__': 61 arg = len(sys.argv) - 1 62 if arg == 0 or arg > 2: 63 error() 64 if arg == 1: 65 make_food(sys.argv[1]) 66 sys.exit(0) 67 if arg == 2: 68 if sys.argv[2]=='-l' or sys.argv[2]=='--link': 69 make_food(sys.argv[1], 1) 70 sys.exit(0) 71 else: 72 error()
$ ./planetfeedparser.py [LINK] [OPTION](optional)
Link must be like: link1 or link2
Options available are: -l or --link, to display the links to blogposts
Run the above script like:
$ ./planetfeedparser.py http://planet.fedoraproject.org/rss10.xml -l
or:
$ python planetfeedparser.py http://planet.fedoraproject.org/rss20.xml
Here example output is given below:
sudip@sudip-mint planetfeedparser $ (master) python planetfeedparser.py http://planet.fedoraproject.org/rss10.xml --link 1: Post Title: Converting LibreOffice dialogs to .ui format, 300 conversions milestone Author: Caolán McNamara Link: http://blogs.linux.ie/caolan/2013/07/15/converting-libreoffice-dialogs-to-ui-format-300-conversions-milestone/ 2: Post Title: The GNOME pants are still alive? Author: Thomas Vander Stichele Link: http://thomas.apestaart.org/log/?p=1559 3: Post Title: How XMir and Mir fit together Author: Matthew Garrett Link: http://mjg59.dreamwidth.org/26254.html 4: Post Title: Linux init-systems Author: Daniel Pocock Link: http://danielpocock.com/linux-init-systems 5: Post Title: Fedora 20 kommt ohne SendMail Author: Fedora-Blog.de Link: http://feedproxy.google.com/~r/Fedora-blogde/~3/rAjpi5VdAhQ/ 6: Post Title: Week-end hacks Author: Bastien Nocera Link: http://www.hadess.net/2013/07/week-end-hacks.html