Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove inactive followers on SensCritique
import requests
from bs4 import BeautifulSoup
def get_my_secret_cookie():
my_secret_cookie = {
"SC_AUTH_UID": "30993",
"SC_DEVICE_CATEGORY": "desktop",
"SC_AUTH": "MY_SECRET_COOKIE", # TODO
"__cfduid": "MY_SECRET_COOKIE", # TODO
"SC_ID_TOKEN": "MY_SECRET_COOKIE", # TODO
"SC_SESSIONS_ID": "MY_SECRET_COOKIE", # TODO
"SC_REFRESH_TOKEN": "MY_SECRET_COOKIE", # TODO
}
return my_secret_cookie
def get_request_headers():
request_headers = {
"Origin": "https://www.senscritique.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Referer": "https://www.senscritique.com/wok/contacts",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"Content-Length": "13",
"Connection": "keep-alive",
}
return request_headers
def get_my_user_id():
my_user_id = 30993 # 'wok'
return my_user_id
def get_url(page_no=1):
if page_no == 1:
url = "https://www.senscritique.com/scouts.ajax"
else:
url = "https://www.senscritique.com/sc2/scouts/index/page-{}.ajax".format(
page_no
)
return url
def get_params(my_user_id=None):
if my_user_id is None:
my_user_id = get_my_user_id()
params = {"user-id": my_user_id, "order": "action", "filter": "abonnes"}
return params
def get_soup(page_no=1, my_user_id=None):
soup = BeautifulSoup(
requests.post(
url=get_url(page_no=page_no), data=get_params(my_user_id=my_user_id)
).content,
"lxml",
)
return soup
def parse_user_ids_from_soup(soup):
parsed_data = soup.find_all("div", {"class": "d-button-group"})
user_ids = [int(user_data.attrs["data-sc-user-id"]) for user_data in parsed_data]
return user_ids
def gather_all_user_ids(page_range, my_user_id=None, verbose=True):
if page_range is None:
page_range = [1]
all_user_ids = set()
for page_no in page_range:
soup = get_soup(page_no=page_no, my_user_id=my_user_id)
new_user_ids = parse_user_ids_from_soup(soup)
if verbose:
print(
"Page n°{} (length={}): {}".format(
page_no, len(new_user_ids), new_user_ids
)
)
all_user_ids.update(new_user_ids)
if my_user_id in all_user_ids:
print("Removing my user id ({}) from the set.".format(my_user_id))
all_user_ids.remove(my_user_id)
if verbose:
print("Total (length={}): {}".format(len(all_user_ids), all_user_ids))
return all_user_ids
def block_user_id(user_id, my_secret_cookie=None, request_headers=None):
if my_secret_cookie is None:
my_secret_cookie = get_my_secret_cookie()
if request_headers is None:
request_headers = get_request_headers()
response = requests.post(
url="https://www.senscritique.com/sc2/scouts/block.json",
headers=request_headers,
data={"userId": str(user_id)},
cookies=my_secret_cookie,
)
if response.status_code == 200:
print("UserID: {}\t; OK".format(user_id))
else:
print("UserID: {}\t; Status code: {}".format(user_id, response.status_code))
return
def main():
first_page_no_to_remove = 21
last_page_no_to_remove = 24
page_range = list(range(first_page_no_to_remove, last_page_no_to_remove + 1))
all_user_ids = gather_all_user_ids(
page_range=page_range, my_user_id=get_my_user_id(), verbose=True
)
for counter, user_id in enumerate(all_user_ids):
block_user_id(user_id=user_id, my_secret_cookie=get_my_secret_cookie())
return True
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment