Exemplo n.º 1
0
def User():
    mc = MediaContainer()
    headers = sort_headers(["Type", "Userid", "Username", "Container-start", "Container-Size", "Device", "Title"])
    container_start = int(headers.get("Container-Start") or DEFAULT_CONTAINER_START)
    container_size = int(headers.get("Container-Size") or DEFAULT_CONTAINER_SIZE)
    container_max = container_start + container_size
    users_data = query_user_stats(headers)

    if users_data is not None:
        users = users_data[0]
        devices = users_data[1]
        device_names = []
        for user, records in users.items():
            last_active = datetime.datetime.strptime("1900-01-01 00:00:00", DATE_STRUCTURE)
            uc = FlexContainer("User", {"userName": user}, False)
            sc = FlexContainer("Views")
            i = 0
            for record in records:
                viewed_at = datetime.datetime.fromtimestamp(record["lastViewedAt"])
                if viewed_at > last_active:
                    last_active = viewed_at
                if i >= container_max:
                    break
                if i >= container_start:
                    vc = FlexContainer("View", record, False)
                    if "deviceName" in record:
                        if record["deviceName"] not in device_names:
                            device_names.append(record["deviceName"])
                    sc.add(vc)
            uc.add(sc)
            uc.set("lastSeen", last_active)
            dp = FlexContainer("Devices", None, False)
            chrome_data = None
            for device in devices:
                if device["userName"] == user:
                    if device["deviceName"] in device_names:
                        if device["deviceName"] != "Chrome":
                            Log.Debug("Found a device for %s" % user)
                            dc = FlexContainer("Device", device)
                            dp.add(dc)
                        else:
                            chrome_bytes = 0
                            if chrome_data is None:
                                chrome_data = device
                            else:
                                chrome_bytes = device["totalBytes"] + chrome_data.get("totalBytes") or 0
                            chrome_data["totalBytes"] = chrome_bytes
            if chrome_data is not None:
                dc = FlexContainer("Device", chrome_data)
                dp.add(dc)
            uc.add(dp)
            mc.add(uc)

    Log.Debug("Still alive, returning data")

    return mc
Exemplo n.º 2
0
def build_tag_container(tag_type):
    selection = tag_type
    headers = sort_headers(["Container-Start", "Container-Size"])
    records = query_tag_stats(selection, headers)
    mc = MediaContainer()
    if records is not None:
        for record in records:
            sc = StatContainer(record)
            mc.add(sc)

    return mc
Exemplo n.º 3
0
def Clients():
    """
    Endpoint to scan LAN for cast devices
    """
    Log.Debug('Recieved a call to fetch all devices')
    # Grab our response header?
    casts = fetch_devices()

    mc = MediaContainer()
    for cast in casts:
        dc = CastContainer(cast)
        mc.add(dc)

    return mc
Exemplo n.º 4
0
def Devices():
    """

    Endpoint to scan LAN for cast devices
    """
    Log.Debug('Recieved a call to fetch cast devices')
    # Grab our response header?
    casts = fetch_devices()
    mc = MediaContainer()
    for cast in casts:
        Log.Debug("Cast type is " + cast['type'])
        if (cast['type'] == 'cast') | (cast['type'] == 'audio') | (cast['type'] == 'group'):
            dc = CastContainer(cast)
            mc.add(dc)

    return mc
Exemplo n.º 5
0
            cast = pychromecast.Chromecast(host, port)
            cast.wait()
            values['Type'] = cast.cast_type
            pc = PlexController(cast)
            cast.register_handler(pc)
            pc.play_media(values, log_data)
        except pychromecast.LaunchError, pychromecast.PyChromecastError:
            Log.Debug('Error connecting to host.')
            status = "Error"
        finally:
            if pc is not False:
                status = "Success"

    oc = MediaContainer({
        'Name': 'Playback Status',
        'Status': status,
        'Message': msg
    })

    return oc


def log_data(data):
    Log.Debug("Is there data?? " + JSON.StringFromObject(data))


@route(APP + '/cmd')
def Cmd():
    """
    Media control command(s).
Exemplo n.º 6
0
def Growth():
    headers = sort_headers(["Interval", "Start", "End", "Container-Size", "Container-Start", "Type"])
    records = query_library_growth(headers)
    total_array = {}
    for record in records:
        dates = str(record["addedAt"])[:-9].split("-")

        year = str(dates[0])
        month = str(dates[1])
        day = str(dates[2])

        year_array = total_array.get(year) or {}
        month_array = year_array.get(month) or {}
        day_array = month_array.get(day) or []
        day_array.append(record)

        month_array[day] = day_array
        year_array[month] = month_array
        total_array[year] = year_array

    mc = MediaContainer()
    grand_total = 0
    types_all = {}
    for y in range(0000, 3000):
        y = str(y)
        year_total = 0
        if y in total_array:
            types_year = {}
            Log.Debug("Found a year %s" % y)
            year_container = FlexContainer("Year", {"value": y})
            year_array = total_array[y]
            Log.Debug("Year Array: %s" % JSON.StringFromObject(year_array))
            month_total = 0
            for m in range(1, 12):
                m = str(m).zfill(2)
                if m in year_array:
                    types_month = {}
                    Log.Debug("Found a month %s" % m)
                    month_container = FlexContainer("Month", {"value": m})
                    month_array = year_array[m]
                    for d in range(1, 32):
                        d = str(d).zfill(2)
                        if d in month_array:
                            types_day = {}
                            Log.Debug("Found a day %s" % d)
                            day_container = FlexContainer("Day", {"value": d}, False)
                            records = month_array[d]
                            for record in records:
                                ac = FlexContainer("Added", record, False)
                                record_type = record["type"]
                                temp_day_count = types_day.get(record_type) or 0
                                temp_month_count = types_month.get(record_type) or 0
                                temp_year_count = types_year.get(record_type) or 0
                                temp_all_count = types_all.get(record_type) or 0
                                types_day[record_type] = temp_day_count + 1
                                types_month[record_type] = temp_month_count + 1
                                types_year[record_type] = temp_year_count + 1
                                types_all[record_type] = temp_all_count + 1
                                day_container.add(ac)
                            month_total += day_container.size()
                            day_container.set("totalAdded", day_container.size())
                            for rec_type in types_day:
                                day_container.set("%sCount" % rec_type, types_day.get(rec_type))
                            month_container.add(day_container)
                    year_total += month_total
                    month_container.set("totalAdded", month_total)
                    for rec_type in types_month:
                        month_container.set("%sCount" % rec_type, types_month.get(rec_type))
                    year_container.add(month_container)
            year_container.set("totalAdded", year_total)
            for rec_type in types_year:
                year_container.set("%sCount" % rec_type, types_year.get(rec_type))
            grand_total += year_total
            mc.add(year_container)
    return mc
Exemplo n.º 7
0
def Library():
    mc = MediaContainer()
    headers = sort_headers(["Container-Size", "Type"])
    Log.Debug("Here's where we fetch some library stats.")
    sections = {}
    recs = query_library_stats(headers)
    records = recs[0]
    sec_counts = recs[1]
    for record in records:
        section = record["sectionTitle"]
        if section not in sections:
            sections[section] = []
        del (record["sectionTitle"])
        sections[section].append(dict(record))

    for name in sections:
        Log.Debug("Looping through section '%s'" % name)
        sec_id = sections[name][0]["section"]
        sec_type = sections[name][0]["sectionType"]
        section_types = {
            1: "movie",
            2: "show",
            3: "music",
            4: "photo",
            8: "music",
            13: "photo"
        }
        if sec_type in section_types:
            sec_type = section_types[sec_type]

        item_count = 0
        play_count = 0
        playable_count = 0
        section_children = []
        for record in sections[name]:
            item_count += record["totalItems"]
            if record['playCount'] is not None:
                play_count += record['playCount']
            if record["type"] in ["episode", "track", "movie"]:
                playable_count = record["totalItems"]

            item_type = str(record["type"]).capitalize()
            record_data = {
                "totalItems": record["totalItems"]
            }
            vc = AnyContainer(record_data, item_type, False)

            if record["lastViewedAt"] is not None:
                last_item = {
                    "title": record['title'],
                    "grandparentTitle": record['grandparentTitle'],
                    "art": record['art'],
                    "thumb": record['thumb'],
                    "ratingKey": record['ratingKey'],
                    "lastViewedAt": record['lastViewedAt'],
                    "username": record['username'],
                    "userId": record['userId']
                }
                li = AnyContainer(last_item, "lastViewed", False)
                vc.add(li)

            section_children.append(vc)

        section_data = {
            "title": name,
            "id": sec_id,
            "totalItems": item_count,
            "playableItems": playable_count,
            "playCount": play_count,
            "type": sec_type
        }
        sec_unique_played = sec_counts.get(str(sec_id)) or None
        if sec_unique_played is not None:
            Log.Debug("Hey, we got the unique count")
            section_data["watchedItems"] = sec_unique_played["viewedItems"]
        ac = AnyContainer(section_data, "Section", "False")
        for child in section_children:
            ac.add(child)

        mc.add(ac)

    return mc