-
-
Save apendleton/e86073220c5ce6a1296c to your computer and use it in GitHub Desktop.
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
var ocemail = function(url, domain) { | |
var capitalize = function(s) { return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase(); } | |
// url parser from http://jsperf.com/url-parsing | |
var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/; | |
var hostname = urlParseRE.exec(url)[11]; | |
if (hostname) { | |
var domainMatch = /^(?:www[.])?([-a-z0-9]+)[.](house|senate)[.]gov$/; | |
var match = domainMatch.exec(hostname.toLowerCase()); | |
var nameish = match[1], | |
chamber = match[2]; | |
var prefix = chamber == 'senate' ? 'Sen' : 'Rep'; | |
return [capitalize(prefix), ".", capitalize(nameish), "@", domain ? domain : "opencongress.org"].join(""); | |
} | |
} |
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 | |
try: | |
from urlparse import urlparse # python 2 | |
except: | |
from urllib.parse import urlparse # python 3 | |
def ocemail(url, domain='opencongress.org'): | |
parts = urlparse(url) | |
if parts.hostname: | |
match = re.match(r'^(?:www[.])?([-a-z0-9]+)[.](house|senate)[.]gov$', parts.hostname.lower(), re.I) | |
nameish, chamber = match.groups() | |
prefix = 'Sen' if chamber == 'senate' else 'Rep' | |
return "%s.%s@%s" % (prefix.capitalize(), nameish.capitalize(), domain) |
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
def ocemail (website) | |
pattern = /^(?:www[.])?([-a-z0-9]+)[.](house|senate)[.]gov$/i | |
url = URI.parse(website) | |
return nil if url.host.nil? | |
match = pattern.match(url.host.downcase) | |
return nil if match.nil? | |
nameish, chamber = match.captures | |
prefix = (chamber.downcase == 'senate') ? 'Sen' : 'Rep' | |
return "#{prefix.capitalize}.#{nameish.capitalize}@#{Settings.email_congress_domain}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment