Last active
February 2, 2023 07:44
-
-
Save pernydev/d009e2189151cdb5338a0085d46b49c7 to your computer and use it in GitHub Desktop.
Link filter for OpenAI Discord (In Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| def get_domains_from_string(string_with_urls): | |
| whitelist = ["openai.com", "stackoverflow.com"] | |
| urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string_with_urls) | |
| domains = [re.search("(?P<domain>[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,})", url).group("domain") for url in urls] | |
| filtered_domains = [domain for domain in domains if domain in whitelist] | |
| return filtered_domains |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment