from datetime import datetime


def get_libraries(plex):
    """ Return Plex libraries, their contents, media titles, & time updated """
    plex.reload()
    movies = plex.search(libtype="movie")
    movies.sort(key=lambda x: x.addedAt or x.updatedAt)
    shows = plex.search(libtype="show")
    shows.sort(key=lambda x: x.addedAt or x.updatedAt)

    return {
        "movies": movies,
        "movie_titles": [movie.title for movie in movies],
        "shows": shows,
        "show_titles": [show.title for show in shows],
        "updated": datetime.now(),
    }