# Buggy code
def getYoutubePosts(ids):
    videos = []
    with FuturesSession() as session:
        futures = [
            session.get(
                "https://www.youtube.com/feeds/videos.xml?channel_id={id}".format(
                    id=id.channelId
                )
            )
            for id in ids
        ]
        for future in as_completed(futures):
            resp = future.result()
            rssFeed = feedparser.parse(resp.content)
            for vid in rssFeed.entries:
                time = datetime.datetime.now() - datetime.datetime(
                    *vid.published_parsed[:6]
                )

                if time.days >= 7:
                    continue

                video = ytPost()
                video.date = vid.published_parsed
                video.timeStamp = getTimeDiff(vid.published_parsed)
                video.channelName = vid.author_detail.name
                video.channelId = vid.yt_channelid
                video.channelUrl = vid.author_detail.href
                video.id = vid.yt_videoid
                video.videoTitle = vid.title
                video.videoThumb = vid.media_thumbnail[0]["url"].replace("/", "~")
                video.views = vid.media_statistics["views"]
                video.description = vid.summary_detail.value
                video.description = re.sub(
                    r"^https?:\/\/.*[\r\n]*",
                    "",
                    video.description[0:120] + "...",
                    flags=re.MULTILINE,
                )
                videos.append(video)
    return videos


# Traceback (KeyError)
Traceback (most recent call last):
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/feedparser.py", line 398, in __getattr__
return self.__getitem__(key)
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/feedparser.py", line 356, in __getitem__
return dict.__getitem__(self, key)
KeyError: 'published_parsed'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/flask_login/utils.py", line 272, in decorated_view
return func(*args, **kwargs)
File "/home/ubuntu/Yotter/app/routes.py", line 199, in youtube
videos = getYoutubePosts(ids)
File "/home/ubuntu/Yotter/app/routes.py", line 745, in getYoutubePosts
time = datetime.datetime.now() - datetime.datetime(*vid.published_parsed[:6])
File "/home/ubuntu/Yotter/venv/lib/python3.8/site-packages/feedparser.py", line 400, in __getattr__
raise AttributeError("object has no attribute '%s'" % key)
AttributeError: object has no attribute 'published_parsed'

# Fixed code
def getYoutubePosts(ids):
    videos = []
    with FuturesSession() as session:
        futures = [
            session.get(
                "https://www.youtube.com/feeds/videos.xml?channel_id={id}".format(
                    id=id.channelId
                )
            )
            for id in ids
        ]
        for future in as_completed(futures):
            resp = future.result()
            rssFeed = feedparser.parse(resp.content)
            for vid in rssFeed.entries:
                try:
                    time = datetime.datetime.now() - datetime.datetime(
                        *vid.published_parsed[:6]
                    )
                except:
                    time = 0

                if time.days >= 7:
                    continue

                video = ytPost()
                try:
                    video.date = vid.published_parsed
                except:
                    video.date = datetime.datetime.utcnow()
                try:
                    video.timeStamp = getTimeDiff(vid.published_parsed)
                except:
                    video.timeStamp = "Unknown"

                video.channelName = vid.author_detail.name
                video.channelId = vid.yt_channelid
                video.channelUrl = vid.author_detail.href
                video.id = vid.yt_videoid
                video.videoTitle = vid.title
                video.videoThumb = vid.media_thumbnail[0]["url"].replace("/", "~")
                video.views = vid.media_statistics["views"]
                video.description = vid.summary_detail.value
                video.description = re.sub(
                    r"^https?:\/\/.*[\r\n]*",
                    "",
                    video.description[0:120] + "...",
                    flags=re.MULTILINE,
                )
                videos.append(video)
    return videos