Exemple #1
0
def filter_regions(
    requester: nsapi.NSRequester, exclude: t.Iterable[str], *tags: str
) -> t.Collection[str]:
    """Returns a collection of regions retrieved from the world region tag api,
    after filtering is applied.
    """

    excludeSet = set(nsapi.clean_format(exclusion) for exclusion in exclude)

    return [
        region
        for region in requester.world().regions_by_tag(*tags)
        if nsapi.clean_format(region) not in excludeSet
    ]
Exemple #2
0
def retrieve_endings(
    requester: nsapi.NSRequester,
    since: t.Optional[int] = None,
    before: t.Optional[int] = None,
) -> t.Iterable[Ending]:
    """Returns the endings in the given timestamp frame.

    `before` defaults to now,
    and `since` defaults to 24 hours before `before`.
    """

    # Define default window in seconds
    DEFAULT_WINDOW = 24 * 3600

    # Check for before default
    if before:
        beforetime = before
    else:
        beforetime = int(time.time())

    # Check for since default
    if since:
        sincetime = since
    else:
        sincetime = beforetime - DEFAULT_WINDOW

    # Retrieve endings happening data
    happenings = requester.world().happenings(safe=False,
                                              filter="cte",
                                              sincetime=str(sincetime),
                                              beforetime=str(beforetime))

    output = []
    for happ in happenings:
        # Try to match the two patterns
        nationMatch = re.search(r"@@(.*)@@", happ.text)
        regionMatch = re.search(r"%%(.*)%%", happ.text)
        if nationMatch and regionMatch:
            # Pull the first group of each match, i.e. omit the @@/%% delimiters
            output.append(Ending(nation=nationMatch[1], region=regionMatch[1]))
        else:
            logging.warning(
                "Found CTE happening with no nation or no region: %s",
                happ.text)

    return output
Exemple #3
0
def uncrossed(requester: nsapi.NSRequester,
              nation: str,
              lead: str,
              duration: int = 3600) -> Iterable[str]:
    """Returns a iterable of non-recently endorsed nations sourced from lead endorsements
    `duration` is the number of seconds to check back in the nation's endo happenings
    """

    # Source endorsement list from lead
    logging.info("Retrieving lead endorser list")
    nations: Set[str] = set(
        requester.nation(nation).shard("endorsements").split(","))

    # Retrieve recent endo happenings in text form
    timestamp = int(time.time()) - duration
    logging.info("Retrieving endo happenings of nation since %s", timestamp)
    endos: List[str] = [
        happening.text for happening in requester.world().happenings(
            view=f"nation.{nation}",
            filter="endo",
            sincetime=str(timestamp),
        )
    ]

    # Parse endo happening texts
    # A text can be one of the following two 'distinct' forms:
    # based on 'hn67' == nation
    # '@@hn67@@ endorsed @@hn67_ii@@.' (outgoing)
    # '@@hn67_ii@@ endorsed @@hn67@@.' (incoming)
    # we only care about outgoing endorsements
    # the endorsed nation is retrieved using the following process:
    # .split() transforms the above outgoing example to ['', 'hn67', 'endorsed', 'hn67_ii', '']
    # index [3] retrieves the endorsed nation, index [1] retrieves the endorser
    # [1] is used to verify outgoing ( by checking == nation), and [3] retrieves the endorsee
    # Note: Splitting twice, could be optimized with pre-comprehension, for loop, or walrus operator
    logging.info("Filtering for outgoing endorsements")
    outgoing: Set[str] = set(
        text.split("@@")[3] for text in endos if text.split("@@")[1] == nation)

    # Unendorsed is obtained by subtracting the outgoing endorsements from the lead endorsement list
    logging.info("Obtaining uncross endorsed nations")
    unendorsed = nations - outgoing
    return unendorsed
Exemple #4
0
def delegacy(
    requester: nsapi.NSRequester,
    startTime: int = None,
    endTime: int = None,
    duration: int = None,
) -> Iterable[nsapi.Happening]:
    """Obtain all delegacy changes in the requested time period.

    Up to two of startTime, endTime, and duration should be provided.

    End time defaults to now, unless overridden by a combination of start and duration.
    Start time defaults to duration before end time, or 12 hours before end time otherwise.
    If all three arguments are given, duration is ignored.
    """
    # Determine start and end
    if endTime is None:
        if startTime is not None and duration is not None:
            endTime = startTime + duration
        else:
            # Time.time produces a float to represent milli, we just want seconds
            endTime = int(time.time())
    if startTime is None:
        if duration is not None:
            startTime = endTime - duration
        else:
            # 12 hours
            startTime = endTime - 12 * 3600

    # Filter happenings
    return [
        happening for happening in requester.world().happenings(
            safe=False,
            sincetime=str(startTime),
            beforetime=str(endTime),
            filter="member",
        ) if "WA Delegate" in happening.text
    ]
Exemple #5
0
def delegacy(
    requester: nsapi.NSRequester, startTime: int = None, endTime: int = None
) -> Iterable[nsapi.Happening]:
    """Returns all delegacy changes between start and end times (as epoch timestamps),
    endTime defaults to now, and startTime defaults to 12 hours before endTime
    """
    # Set start/end defaults
    if not endTime:
        # Time.time produces a float to represent milli, we just want seconds
        endTime = int(time.time())
    if not startTime:
        # 12 hours, 3600 seconds an hour, 12 hours previous
        startTime = endTime - 12 * 3600
    # Filter happenings
    return [
        happening
        for happening in requester.world().happenings(
            safe=False,
            sincetime=str(startTime),
            beforetime=str(endTime),
            filter="member",
        )
        if "WA Delegate" in happening.text
    ]