Commit
… fetch network ID of a user given either a per-site URL or just their SE-wide profile URL
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,8 @@ | |
from dns.exception import DNSException | ||
import number_homoglyphs | ||
import phone_numbers | ||
# TODO: add 'tldextract' to requirements.txt | ||
import tldextract | ||
|
||
|
||
# TODO: Do we need uid == -2 check? Turn into "is_user_valid" check | ||
|
@@ -53,7 +55,67 @@ def null(): | |
return None | ||
|
||
|
||
def per_site_account_to_network_id(site_uid: int, site: str) -> int: | ||
"""Given a site name and the per-site ID number, fetch the SE ID for said account""" | ||
# TODO: use the API key that we have, also we should likely do something w/ the backoff paramater | ||
return int(requests.get( | ||
"https://api.stackexchange.com/2.3/users/" + str(site_uid) + "/?site=" + site | ||
).json()["items"][0]["account_id"]) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
makyen
Contributor
|
||
|
||
|
||
def getnetid (url): | ||
This comment has been minimized.
Sorry, something went wrong.
makyen
Contributor
|
||
"""Takes a URL for a user account on a network site and returns their network-wide SE ID number""" | ||
This comment has been minimized.
Sorry, something went wrong.
makyen
Contributor
|
||
# TODO: validate the URL we're given somehow | ||
# | ||
# TODO: looks like there is already a regex for this in the function | ||
# get_user_from_url in parsing.py, can some of that logic be re-used | ||
# here even though we also need to be able to extract the user ID from | ||
# an SE URL and not just a per-site account URL | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
makyen
Contributor
|
||
# extact the user ID from the URL | ||
uid: int = int(regex.search(r"(?<=users\/)[0-9]+", url)[0]) | ||
|
||
# if we're given an SE account URL (not a per-site account) | ||
if "stackexchange.com" in url and ".stackexchange.com" not in url: | ||
return uid | ||
# if we're given a per-site account | ||
else: | ||
# extract the URL information | ||
extracted_url_data = tldextract.extract(url) | ||
|
||
# if there is no subdomain, our site has a dedicated domain (i.e., askubuntu.com) | ||
if extracted_url_data.subdomain == '': | ||
sitename = extracted_url_data.domain | ||
else: | ||
# otherwise, our site is a subdomain of SE (i.e., sitename.stackexchange.com) | ||
sitename = extracted_url_data.subdomain | ||
return per_site_account_to_network_id(uid, sitename) | ||
|
||
|
||
# --- Blacklist Functions --- # | ||
# TODO: document these three new commands in the Wiki | ||
@command(str) | ||
This comment has been minimized.
Sorry, something went wrong.
makyen
Contributor
|
||
def isnetblu(url): | ||
# FIXME: This doesn't actually check anything currently (currently it | ||
# just serves to verify that I can correctly add commands and that the | ||
# functinality to lookup network IDs works correctly) | ||
net_id = getnetid(url) | ||
return "Blacklisted network ID " + str(net_id) | ||
# remove that network ID from the blacklist | ||
|
||
|
||
@command(str) | ||
def addnetblu(url): | ||
net_id = getnetid(url) | ||
# add that network ID to the blacklist | ||
|
||
|
||
@command(str) | ||
def rmnetblu(url): | ||
net_id = getnetid(url) | ||
# check if that network ID is on the blacklist | ||
|
||
|
||
# noinspection PyIncorrectDocstring,PyMissingTypeHints | ||
@command(str, whole_msg=True, privileged=True) | ||
def addblu(msg, user): | ||
|
Unfortunately, there is substantially more to using the SE API within SD than just making a
requests.get()
call. Primarily, it's handlingbackoff
. It is, currently, substantially more complex than it should be. I have, partially, written code to be an abstraction for the process, such that it does end up being just a single call which then handlesbackoff
and at least a portion of errors. Finishing that up, even if what I first PR is substantially less than the full handling I'd desire, is a high priority at this point.