Exemple #1
0
    def DiffImage(self: Any, source: Dict[str, Any]) -> None:
        """Diff the provided image data source."""

        filename: str = source["filename"]
        url: str = source["url"]
        allowRevert: bool = source.get("allowRevert", True)

        # Append the current timestamp to the end of the URL as an
        # attempt to prevent the Discord CDN from serving previously
        # cached versions of an image.
        timestamp: str = str(int(datetime.utcnow().timestamp()))
        imageUrl: str = f"{url}?{timestamp}"

        older: Dict[str, Any] = source["older"]
        old: Dict[str, Any] = source["old"]
        new: Dict[str, Any] = source["new"]

        if old["raw"] == new["raw"]:
            logger.info(f"No difference found in {filename} ({url})")

            return
        elif (allowRevert is False) and (older["raw"] == new["raw"]):
            logger.info(f"Ignored revert found in {filename} ({url})")

            return

        source["urlTrim"] = Utility.Truncate(self, url, 256)
        old["size"] = Utility.Base64Size(self, old["raw"])
        new["size"] = Utility.Base64Size(self, new["raw"])

        success: bool = SitRep.Notify(
            self,
            {
                "title": source["urlTrim"],
                "description": None,
                "url": url,
                "filename": source["filename"],
                "imageUrl": imageUrl,
                "size": Utility.CountRange(self, new["size"], old["size"]) + " bytes",
                "diffUrl": source["old"]["gist"].html_url + "/revisions",
            },
        )

        # Ensure no changes go without notification
        if success is True:
            Utility.UpdateGist(self, source)
Exemple #2
0
    def DiffText(self: Any, source: Dict[str, Any]) -> None:
        """Diff the provided text data source."""

        filename: str = source["filename"]
        url: str = source["url"]
        allowRevert: bool = source.get("allowRevert", True)

        older: Dict[str, Any] = source["older"]
        old: Dict[str, Any] = source["old"]
        new: Dict[str, Any] = source["new"]

        if allowRevert is False:
            older["hash"] = Utility.MD5(self, older["raw"])

        old["hash"] = Utility.MD5(self, old["raw"])
        new["hash"] = Utility.MD5(self, new["raw"])

        if old["hash"] == new["hash"]:
            logger.info(f"No difference found in {filename} ({url})")

            return
        elif (allowRevert is False) and (older["hash"] == new["hash"]):
            logger.info(f"Ignored revert found in {filename} ({url})")

            return

        diff: Iterator[str] = Differ().compare(
            old["raw"].splitlines(), new["raw"].splitlines()
        )

        desc: str = ""
        additions: int = 0
        deletions: int = 0

        for line in diff:
            if line.startswith("+ "):
                additions += 1
                desc += f"{line}\n"
            elif line.startswith("- "):
                deletions += 1
                desc += f"{line}\n"

        desc = Utility.Truncate(self, desc, 4048, split="\n")
        source["urlTrim"] = Utility.Truncate(self, url, 256)

        success: bool = SitRep.Notify(
            self,
            {
                "title": source["urlTrim"],
                "description": f"```diff\n{desc}```",
                "url": url,
                "filename": source["filename"],
                "additions": f"{additions:,}",
                "deletions": f"{deletions:,}",
                "diffUrl": source["old"]["gist"].html_url + "/revisions",
            },
        )

        # Ensure no changes go without notification
        if success is True:
            Utility.UpdateGist(self, source)