import sys
import requests

from tornado import web
from tornado import ioloop

from pytube import YouTube

from feedgen.feed import FeedEntry
from feedgen.feed import FeedGenerator


class PlaylistHandler(web.RequestHandler):
    def get(self, playlist):
        print('Playlist: {}'.format(playlist))
        self.add_header('Content-type', 'application/rss+xml')
        playlist = playlist.split('/')
        if len(playlist) < 2:
            playlist.append('video')
        payload = {
            'part': 'snippet',
            'id': playlist[0],
            'key': sys.argv[2]
        }
        request = requests.get('https://www.googleapis.com/youtube/v3/playlists', params=payload)
        response = request.json()
        fg = FeedGenerator()

        fg.title(response['items'][0]['snippet']['title'])
        fg.subtitle(response['items'][0]['snippet']['title'])
        fg.id('http://' + self.request.host + self.request.uri)
        fg.author({'name': response['items'][0]['snippet']['channelTitle']})
        fg.icon(response['items'][0]['snippet']['thumbnails']['default']['url'])
        payload = {
            'part': 'snippet',
            'maxResults': 25,
            'playlistId': playlist[0],
            'key': sys.argv[1]
        }
        request = requests.get('https://www.googleapis.com/youtube/v3/playlistItems', params=payload)
        response = request.json()
        fg.updated(response['items'][0]['snippet']['publishedAt'])
        for item in response['items']:
            snippet = item['snippet']
            entry = FeedEntry()
            entry.title(snippet['title'])
            entry.updated(snippet['publishedAt'])
            entry.id('http://{url}/{type}/{vid}'.format(url=self.request.host,
                                                        type=playlist[1],
                                                        vid=snippet['resourceId']['videoId']))
            entry.author({'name': snippet['channelTitle']})
            entry.pubdate(snippet['publishedAt'])
            entry.link({'href': 'http://www.youtube.com/watch?v=' + snippet['resourceId']['videoId'],
                        'title': snippet['title']})
            entry.summary(snippet['description'])
            fg.add_entry(entry)
        self.write(fg.rss_str())
        self.finish()


class VideoHandler(web.RequestHandler):
    def get(self, video):
        print('Video: {}'.format(video))
        yt = YouTube('http://www.youtube.com/watch?v=' + video)
        vid = sorted(yt.filter("mp4"), key=lambda video: int(video.resolution[:-1]), reverse=True)[0]
        self.redirect(vid.url)


def make_app():
    return web.Application([
        (r'/playlist/(.*)', PlaylistHandler),
        (r'/video/(.*)', VideoHandler)
    ])

if __name__ == '__main__':
    app = make_app()
    app.listen(sys.argv[1])
    ioloop.IOLoop.current().start()

@@ -1546,21 +1546,18 @@
         
+f
 e
-ntry
  = FeedE
@@ -1575,21 +1575,18 @@
         
+f
 e
-ntry
 .title(s
@@ -1614,21 +1614,18 @@
         
+f
 e
-ntry
 .updated
@@ -1661,21 +1661,18 @@
         
+f
 e
-ntry
 .id('htt
@@ -1778,19 +1778,16 @@
         
- 
-  
 type=pla
@@ -1796,19 +1796,16 @@
 ist%5B1%5D,%0A
-   
         
@@ -1900,21 +1900,18 @@
         
+f
 e
-ntry
 .author(
@@ -1957,21 +1957,18 @@
         
+f
 e
-ntry
 .pubdate
@@ -2004,21 +2004,18 @@
         
+f
 e
-ntry
 .link(%7B'
@@ -2112,19 +2112,16 @@
         
-   
 'title':
@@ -2152,21 +2152,18 @@
         
+f
 e
-ntry
 .summary
@@ -2212,21 +2212,18 @@
 d_entry(
+f
 e
-ntry
 )%0A      

change entry to fe