コード例 #1
0
    def make_as2_payload(self, actor_id: str, portal_url: str):

        as2_payload = template_as2(self.event_base_url, self.portal_name)

        # as2_payload["activity"]["prov:used"].\
        #     append({"@id": prov_api_url})
        actor = {}
        actor["url"] = portal_url
        actor["type"] = "Person"
        actor["id"] = actor_id
        as2_payload["event"]["actor"] = actor

        target = {}
        target["id"] = portal_url
        target["type"] = ["Collection"]
        as2_payload["event"]["target"] = target
        as2_payload["event"]["published"] = as2_payload["event"][
            "prov:generatedAtTime"]
        as2_payload["event"]["type"] = [
            "Update", "tracker:ArtifactInteraction", "tracker:Tracker"
        ]

        obj = {}
        items = [{
            "type": ["Link", "WebPage", "schema:ProfilePage"],
            "href": portal_url
        }]
        obj["totalItems"] = len(items)
        obj["items"] = items
        obj["type"] = "Collection"
        as2_payload["event"]["object"] = obj

        yield as2_payload
コード例 #2
0
    def make_as2_payload(self, events: iter, actor_id: str,
                         portal_username: str, prov_api_url: str,
                         last_token: str):
        """
        Converts the Slideshare API response into ActivityStream message.

        :param events: the list of slides from the slideshare api response.
        :yield: a generator list of ActivityStream messages.
        """
        if not bool(events):
            return []

        for slide in events:
            as2_payload = template_as2(self.event_base_url,
                                       self.portal_name,
                                       last_token=last_token)

            as2_payload["activity"]["prov:used"].append({"@id": prov_api_url})

            actor = {}
            actor["url"] = f"https://slideshare.net/" \
                f"{portal_username}"
            actor["type"] = "Person"
            actor["name"] = portal_username
            actor["id"] = actor_id

            image = {}
            image["type"] = "Link"
            image["href"] = slide.find("ThumbnailSmallURL").text
            actor["image"] = image
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = actor.get("url")
            target["type"] = ["Collection"]
            as2_payload["event"]["target"] = target

            created_at = slide.find("Created").text
            c_date = datetime.strptime(created_at, "%Y-%m-%d %H:%M:%S %Z")
            as2_payload["event"]["published"] = c_date.strftime(
                "%Y-%m-%dT%H:%M:%SZ")
            as2_payload["event"]["type"] = [
                "Add", "tracker:ArtifactCreation", "tracker:Tracker"
            ]

            obj = {}
            items = [{
                "type":
                ["Link", "Article", "schema:PresentationDigitalDocument"],
                "href":
                slide.find("URL").text
            }]
            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"
            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #3
0
    def make_as2_payload(self, event, users_found: list):
        as2_payload = template_as2(self.event_base_url, self.portal_name)
        oai_pmh_url = self.portal.get("event_urls", {}).\
            get("oai_pmh_url")
        as2_payload["activity"]["prov:used"].\
            append({"@id": oai_pmh_url})
        as2_payload["activity"]["prov:used"][0]["prov:used"].append(
            {"id": "https://github.com/mloesch/sickle"})

        for user in users_found:
            actor_id = user.get("id")
            portal_username = user.get("username")
            portal_user_id = user.get("userId")

            if not actor_id or not portal_user_id or not portal_username:
                LOG.debug("""
                    Unable to format as2 message without one of the following
                    variables: actor_id, user_id, or username. skipping.
                    """)
                return False

            actor = {}
            actor["url"] = "https://figshare.com/authors/{}/{}"\
                .format(
                    portal_username, portal_user_id)
            actor["type"] = "Person"
            actor["id"] = actor_id
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = self.portal.get("portal_url")
            target["type"] = ["Collection"]
            as2_payload["event"]["target"] = target

            pubtime = datetime.strptime(
                event.get("date")[0], "%Y-%m-%dT%H:%M:%SZ")
            pubtime = pubtime.strftime("%Y-%m-%dT%H:%M:%SZ")
            as2_payload["event"]["published"] = pubtime
            as2_payload["event"]["type"] = [
                "Create", "tracker:ArtifactCreation", "tracker:Tracker"
            ]

            obj = {}
            items = []
            for link in event.get("relation", []):
                items.append({
                    "type": ["Link", "Document", "schema:Dataset"],
                    "href": link
                })
            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"
            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #4
0
    def make_as2_payload(self, events: iter, actor_id: str,
                         portal_username: str, prov_api_url: str,
                         last_token: str):

        profile_url = "https://www.twitter.com/%s"
        tweet_url = "/status/%s"

        for event in events:
            as2_payload = template_as2(self.event_base_url,
                                       self.portal_name,
                                       last_token=last_token)
            as2_payload["activity"]["prov:used"].append({"@id": prov_api_url})

            actor = {}
            actor["url"] = profile_url % event.get("user",
                                                   {}).get("screen_name")
            actor["type"] = "Person"
            actor["id"] = actor_id
            actor["name"] = portal_username

            image = {}
            image["type"] = "Link"
            image["href"] = event.get("user",
                                      {}).get("profile_image_url_https")
            actor["image"] = image
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = actor.get("url")
            target["type"] = ["Collection", "schema:Blog"]
            as2_payload["event"]["target"] = target

            created_at = event.get("created_at")
            c_date = datetime.strptime(created_at, "%a %b %d %H:%M:%S %z %Y")
            # Force naive datetime
            c_date = c_date.astimezone().replace(tzinfo=None)
            as2_payload["event"]["published"] = c_date.strftime(
                "%Y-%m-%dT%H:%M:%SZ")
            as2_payload["event"]["type"] = [
                "Create", "tracker:ArtifactCreation", "tracker:Tracker"
            ]

            obj = {}
            url = actor.get("url") + tweet_url % event.get("id_str")
            items = [{
                "type": ["Link", "Note", "schema:SocialMediaPosting"],
                "href": url
            }]
            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"
            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #5
0
    def on_create_event(self, event, event_type, actor_id, portal_username,
                        etag):
        """
        Converts the CreateEvent GitHub API event to an AS2 message.

        :param event: (dict) The event from Github API
        :param event_type: (str) the type of event: CreateEvent
        :return: (dict) the converted AS2 message.
        """
        as2_payload = {}
        if not event or not event_type:
            return as2_payload

        as2_payload = template_as2(self.event_base_url,
                                   PORTAL_NAME,
                                   last_token=etag)
        portal_url = self.portal.get("portal_url")
        as2_payload["activity"]["prov:used"].append({"@id": portal_url})

        as2_payload["event"]["actor"] = self.get_actor_md(
            event.get("actor"), actor_id, portal_username)

        target = {}
        target["id"] = GithubTracker._make_html_url(
            event.get("actor", {}).get("url"))
        target["type"] = ["Collection"]
        as2_payload["event"]["target"] = target

        as2_payload["event"]["published"] = event.get("created_at")
        as2_payload["event"]["type"] = [
            "Create", "tracker:ArtifactCreation", "tracker:Tracker"
        ]

        ref_type = event.get("payload", {}).get("ref_type", "")
        if ref_type == "tag":
            tag = ref_type = event.get("payload", {}).get("ref")
            href = "{}/releases/tag/{}".format(
                GithubTracker._make_html_url(event.get("repo", {}).get("url")),
                tag)
        else:
            href = GithubTracker._make_html_url(
                event.get("repo", {}).get("url"))

        obj = {}
        items = [{
            "type": ["Link", "Document", "schema:SoftwareSourceCode"],
            "href": href
        }]
        obj["totalItems"] = len(items)
        obj["items"] = items
        obj["type"] = "Collection"
        as2_payload["event"]["object"] = obj

        return as2_payload
コード例 #6
0
    def on_watch_event(self, event, event_type, actor_id, portal_username,
                       etag):
        """
        Converts the WatchEvent GitHub API event to an AS2 message.
        Equivalent to starring a repository:
        https://developer.github.com/v3/activity/events/types/#watchevent

        :param event: (dict) The event from Github API
        :param event_type: (str) the type of event: WatchEvent
        :return: (dict) the converted AS2 message.
        """
        as2_payload = {}
        if not event or not event_type:
            return as2_payload

        as2_payload = template_as2(self.event_base_url,
                                   PORTAL_NAME,
                                   last_token=etag)

        payload = event.get("payload")
        event_type_prop = EVENT_TYPES.get(event_type)
        event_md = payload.get(event_type_prop)
        # if payload doesn't include author use different key
        if not event_md:
            event_md = event.get("actor")

        portal_url = self.portal.get("portal_url")
        as2_payload["activity"]["prov:used"].append({"@id": portal_url})

        target = {}
        target["id"] = GithubTracker._make_html_url(
            event.get("repo", {}).get("url"))
        target["type"] = ["Collection"]
        as2_payload["event"]["target"] = target

        as2_payload["event"]["actor"] = self.get_actor_md(
            event_md, actor_id, portal_username)
        as2_payload["event"]["published"] = event.get("created_at")
        as2_payload["event"]["type"] = [
            "Like", "tracker:ArtifactInteraction", "tracker:Tracker"
        ]

        obj = {}
        items = [{
            "type": ["Link", "Document", "schema:SoftwareSourceCode"],
            "href":
            GithubTracker._make_html_url(event.get("repo", {}).get("url"))
        }]
        obj["totalItems"] = len(items)
        obj["items"] = items
        obj["type"] = "Collection"
        as2_payload["event"]["object"] = obj

        return as2_payload
コード例 #7
0
    def make_as2_payload(self, events: iter, actor_id: str,
                         portal_user_id: str, portal_username: str,
                         prov_api_url: str):
        """
        Converts Publons response into ActivityStream message.

        :param annotations: list of annotations received from Publons
        API.
        :return: a generator list of ActivityStream messages.
        """

        for event in events.get("results"):
            as2_payload = template_as2(self.event_base_url, self.portal_name)

            as2_payload["activity"]["prov:used"].\
                append({"@id": prov_api_url})
            actor = {}
            if portal_username:
                actor["name"] = portal_username
            actor["id"] = actor_id
            actor["type"] = "Person"
            actor["url"] = "https://publons.com/author/{}/".format(
                portal_user_id)
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = self.portal.get("portal_url")
            target["type"] = ["Collection"]
            as2_payload["event"]["target"] = target

            if event.get("date_reviewed") == str(datetime.now().year):
                as2_payload["event"]["published"] = as2_payload["event"][
                    "prov:generatedAtTime"]
            else:
                as2_payload["event"]["published"] = "{}-01-01T00:00:00Z"\
                    .format(event.get("date_reviewed"))
            as2_payload["event"]["type"] = [
                "Add", "tracker:ArtifactInteraction", "tracker:Tracker"
            ]

            obj = {}
            items = [{
                "type": ["Link", "Note", "schema:Review"],
                "href":
                event.get("ids", {}).get("academic", {}).get("url")
            }]
            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"
            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #8
0
    def make_as2_payload(self, posts: iter, actor_id: str, prov_api_url: str):
        """
        Converts Stack Overflow response into ActivityStream message.

        :param annotations: list of annotations received from Stack Overflow
        API.
        :return: a generator list of ActivityStream messages.
        """

        for event in posts.get("items"):
            as2_payload = template_as2(self.event_base_url, self.portal_name)

            as2_payload["activity"]["prov:used"].\
                append({"@id": prov_api_url})
            actor = {}
            actor["id"] = actor_id
            actor["type"] = "Person"
            actor["url"] = event.get("owner", {}).get("link")
            image = {}
            image["type"] = "Link"
            image["href"] = event.get("owner", {}).get("profile_image")
            actor["image"] = image
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = self.portal.get("portal_url")
            target["type"] = ["Collection"]
            as2_payload["event"]["target"] = target

            published = datetime.fromtimestamp(event.get("creation_date")).\
                strftime("%Y-%m-%dT%H:%M:%SZ")

            as2_payload["event"]["published"] = published
            as2_payload["event"]["type"] = [
                "Add", "tracker:ArtifactInteraction", "tracker:Tracker"
            ]

            obj = {}
            post_type = event.get("post_type")
            item_type = []
            if post_type == "question":
                item_type = ["Link", "Note", "schema:Question"]
            else:
                item_type = ["Link", "Note", "schema:Answer"]

            items = [{"type": item_type, "href": event.get("link")}]
            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"
            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #9
0
    def on_push_event(self, event, event_type, actor_id, portal_username,
                      etag):
        """
        Converts the PushEvent GitHub API event to an AS2 message.

        :param event: (dict) The event from Github API
        :param event_type: (str) the type of event: PushEvent
        :return: (dict) the converted AS2 message.
        """
        as2_payload = {}
        if not event or not event_type:
            return as2_payload

        as2_payload = template_as2(self.event_base_url,
                                   PORTAL_NAME,
                                   last_token=etag)

        payload = event.get("payload")
        event_type_prop = EVENT_TYPES.get(event_type)
        event_md = payload.get(event_type_prop)

        portal_url = self.portal.get("portal_url")
        as2_payload["activity"]["prov:used"].append({"@id": portal_url})

        as2_payload["event"]["actor"] = self.get_actor_md(
            event.get("actor"), actor_id, portal_username)

        target = {}
        target["id"] = GithubTracker._make_html_url(
            event.get("repo").get("url"))
        target["type"] = ["Collection"]
        as2_payload["event"]["target"] = target

        as2_payload["event"]["published"] = event.get("created_at")
        as2_payload["event"]["type"] = [
            "Add", "tracker:ArtifactInteraction", "tracker:Tracker"
        ]

        obj = {}
        if isinstance(event_md, list) and len(event_md) > 0:
            items = [{
                "type": ["Link", "Document", "schema:SoftwareSourceCode"],
                "href":
                GithubTracker._make_html_url(event_md[0].get("url"))
            }]
            obj["totalItems"] = len(items)
            obj["items"] = items
        obj["type"] = "Collection"
        as2_payload["event"]["object"] = obj

        return as2_payload
コード例 #10
0
    def make_as2_payload(self, feed: dict, actor_id: str, portal_url: str,
                         portal_username: str, prov_api_url: str):

        actor = {}
        actor_url = "{}author/{}".format(portal_url, portal_username)
        actor["url"] = actor_url
        actor["type"] = "Person"
        actor["id"] = actor_id
        actor["name"] = portal_username

        target_name = feed.get("feed", {}).get("title")

        for event in feed.get("entries"):

            # skip entries that don't match the given username
            if event.get("author") != portal_username:
                continue

            as2_payload = template_as2(self.event_base_url, self.portal_name)

            as2_payload["activity"]["prov:used"].\
                append({"@id": prov_api_url})
            as2_payload["activity"]["prov:used"][0]["prov:used"].append(
                {"id": "https://github.com/kurtmckee/feedparser"})
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = portal_url
            target["type"] = ["Collection"]
            target["name"] = target_name
            as2_payload["event"]["target"] = target

            pubtime = datetime.strptime(event.get("published"),
                                        "%a, %d %b %Y %H:%M:%S %z")
            pubtime = pubtime.strftime("%Y-%m-%dT%H:%M:%SZ")
            as2_payload["event"]["published"] = pubtime
            as2_payload["event"]["type"] = [
                "Add", "tracker:ArtifactCreation", "tracker:Tracker"
            ]

            obj = {}
            items = [{
                "type": ["Link", "Article", "schema:BlogPosting"],
                "href": event.get("link")
            }]
            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"
            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #11
0
    def on_issue_comment_event(self, event, event_type, actor_id,
                               portal_username, etag):
        """
        Converts the IssueCommentEvent GitHub API event to an AS2 message.

        :param event: (dict) The event from Github API
        :param event_type: (str) the type of event: IssueCommentEvent
        :return: (dict) the converted AS2 message.
        """
        as2_payload = {}
        if not event or not event_type:
            return as2_payload

        as2_payload = template_as2(self.event_base_url,
                                   PORTAL_NAME,
                                   last_token=etag)

        payload = event.get("payload")
        event_type_prop = EVENT_TYPES.get(event_type)
        event_md = payload.get(event_type_prop)

        as2_payload["event"]["actor"] = self.get_actor_md(
            event_md.get("user"), actor_id, portal_username)

        portal_url = self.portal.get("portal_url")
        as2_payload["activity"]["prov:used"].append({"@id": portal_url})

        target = {}
        target["id"] = GithubTracker._make_html_url(
            payload.get("issue", {}).get("repository_url"))
        target["type"] = ["Collection"]
        as2_payload["event"]["target"] = target

        as2_payload["event"]["published"] = event.get("created_at")
        as2_payload["event"]["type"] = [
            "Add", "tracker:ArtifactInteraction", "tracker:Tracker"
        ]

        obj = {}
        items = [{
            "type": ["Link", "Note", "schema:Comment"],
            "href": event_md.get("html_url")
        }]
        obj["totalItems"] = len(items)
        obj["items"] = items
        obj["type"] = "Collection"
        as2_payload["event"]["object"] = obj

        return as2_payload
コード例 #12
0
    def make_as2_payload(self, events: dict, actor_id: str,
                         portal_user_id: str, portal_url: str,
                         prov_api_url: str):

        for event in events.get("items"):

            # skip entries that don't match the given user_id
            if event.get("author", {}).get("id") != portal_user_id:
                continue

            actor = {}
            actor["url"] = event.get("author", {}).get("url")
            actor["type"] = "Person"
            actor["id"] = actor_id
            actor["name"] = event.get("author", {}).get("displayName")

            as2_payload = template_as2(self.event_base_url, self.portal_name)

            as2_payload["activity"]["prov:used"].\
                append({"@id": prov_api_url})
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = portal_url
            target["type"] = ["Collection"]
            as2_payload["event"]["target"] = target

            pubtime = datetime.strptime(
                "".join(event.get("published").rsplit(":", 1)),
                "%Y-%m-%dT%H:%M:%S%z")
            pubtime = pubtime.strftime("%Y-%m-%dT%H:%M:%SZ")
            as2_payload["event"]["published"] = pubtime
            as2_payload["event"]["type"] = [
                "Add", "tracker:ArtifactCreation", "tracker:Tracker"
            ]

            obj = {}
            items = [{
                "type": ["Link", "Article", "schema:BlogPosting"],
                "href": event.get("url")
            }]
            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"
            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #13
0
    def make_as2_payload(self, events: iter, actor_id: str,
                         portal_username: str, prov_api_url: str):
        """
        Converts the Slideshare API response into ActivityStream message.

        :param events: the list of slides from the slideshare api response.
        :yield: a generator list of ActivityStream messages.
        """
        events_list = events.get("query", {}).get("usercontribs")
        if not bool(events_list):
            return []

        for event in events_list:
            as2_payload = template_as2(self.event_base_url, self.portal_name)

            as2_payload["activity"]["prov:used"]\
                .append({"@id": prov_api_url})

            actor = {}
            actor["url"] = "https://wikipedia.org/wiki/User:{}"\
                .format(portal_username)
            actor["type"] = "Person"
            actor["name"] = portal_username
            actor["id"] = actor_id
            as2_payload["event"]["actor"] = actor

            target = {}
            target["id"] = self.portal.get("portal_url")
            target["type"] = ["Collection"]
            as2_payload["event"]["target"] = target

            created_at = event.get("timestamp")
            as2_payload["event"]["published"] = created_at
            if event.get("new"):
                as2_payload["event"]["type"].extend(
                    ["Add", "tracker:ArtifactCreation"])
            else:
                as2_payload["event"]["type"].extend(
                    ["Update", "tracker:ArtifactInteraction"])
            as2_payload["event"]["type"].append("tracker:Tracker")
            obj = {}
            title = event.get("title").replace(" ", "_")
            items = []

            url_params = [{
                "params": "?oldid={}".format(event.get("revid")),
                "source": ""
            }, {
                "params":
                "?diff=prev&oldid={}".format(event.get("revid")),
                "source":
                "?diff=prev"
            }]
            for p in url_params:
                items.append({
                    "type": ["Link", "Article", "schema:Article"],
                    "href":
                    "https://wikipedia.org/wiki/{}{}".format(
                        title, p["params"]),
                    "OriginalResource":
                    "https://wikipedia.org/wiki/{}{}".format(
                        title, p["source"]),
                    "Memento":
                    "https://wikipedia.org/wiki/{}{}".format(
                        title, p["params"]),
                    "mementoDatetime":
                    created_at
                })

            obj["totalItems"] = len(items)
            obj["items"] = items
            obj["type"] = "Collection"

            as2_payload["event"]["object"] = obj

            yield as2_payload
コード例 #14
0
    def on_pull_request_event(self, event, event_type, actor_id,
                              portal_username, etag):
        """
        Converts the PullRequestEvent GitHub API event to an AS2 message.

        Three possible AS2 activities by the actor. Accept, TentativeReject,
        or Offer.

        :param event: (dict) The event from Github API
        :param event_type: (str) the type of event: PullRequestEvent
        :return: (dict) the converted AS2 message.
        """
        as2_payload = {}
        if not event or not event_type:
            return as2_payload

        as2_payload = template_as2(self.event_base_url,
                                   PORTAL_NAME,
                                   last_token=etag)
        portal_url = self.portal.get("portal_url")
        as2_payload["activity"]["prov:used"].append({"@id": portal_url})

        payload = event.get("payload")
        event_type_prop = EVENT_TYPES.get(event_type)
        event_md = payload.get(event_type_prop)
        if event_md.get("user", {}).get("login") == portal_username:
            # User offered pull request
            as2_payload["event"]["actor"] = self.get_actor_md(
                event_md.get("user"), actor_id, portal_username)
            as2_payload["event"]["type"] = [
                "Offer", "tracker:ArtifactInteraction"
            ]
        elif event_md.get("merged_by"):
            # Actor merged a pull request into the repository
            as2_payload["event"]["actor"] = self.get_actor_md(
                event_md.get("merged_by"), actor_id, portal_username)
            as2_payload["event"]["type"] = [
                "Accept", "tracker:ArtifactInteraction"
            ]
        else:
            # Actor closed the issue, Tentatively Rejecting it
            # (could reopen later and be used)
            as2_payload["event"]["actor"] = self.get_actor_md(
                event.get("actor"), actor_id, portal_username)
            as2_payload["event"]["type"] = [
                "TentativeReject", "tracker:ArtifactInteraction"
            ]
        as2_payload["event"]["type"].append("tracker:Tracker")

        target = {}
        target["id"] = GithubTracker._make_html_url(
            event_md.get("base", {}).get("repo", {}).get("html_url"))
        target["type"] = ["Collection"]
        as2_payload["event"]["target"] = target

        as2_payload["event"]["published"] = event.get("created_at")

        obj = {}
        items = [{
            "type": ["Link", "Document", "schema:SoftwareSourceCode"],
            "href": event_md.get("html_url")
        }]
        obj["totalItems"] = len(items)
        obj["items"] = items
        obj["type"] = "Collection"
        as2_payload["event"]["object"] = obj

        return as2_payload