Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKalopsia committed Nov 5, 2024
2 parents e844aa6 + a5c52b8 commit 5df4159
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 161 deletions.
90 changes: 35 additions & 55 deletions analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(
fields=None,
moved=False,
limited=False,
suspended=False,
):
self.id = id
self.username = username
Expand Down Expand Up @@ -83,6 +84,7 @@ def __init__(
self.fields = fields
self.moved = moved
self.limited = limited
self.suspended = suspended


def split(s):
Expand Down Expand Up @@ -485,26 +487,21 @@ def fetch_users(user_ids, api, cache):

def analyze_following(user_id, list_id, api, cache):
following_ids = []
max_id = None

for _ in range(MAX_GET_FOLLOWING_IDS_CALLS):
try:
if list_id is not None:
accounts = api.list_accounts(id=list_id, max_id=max_id)
else:
accounts = api.account_following(id=user_id, max_id=max_id)
except Exception as exc:
print(f"An error occurred: {exc}")
return []
if list_id is not None:
accounts = api.list_accounts(id=list_id, limit=80)
else:
accounts = api.account_following(id=user_id, limit=80)

for _ in range(MAX_GET_FOLLOWING_IDS_CALLS):
if not accounts:
break

following_ids.extend([account.id for account in accounts])

if len(accounts) > 1:
max_id = accounts[-1].id - 1
else:
accounts = api.fetch_next(accounts)

if accounts is None:
break

if not following_ids:
Expand All @@ -524,23 +521,18 @@ def analyze_following(user_id, list_id, api, cache):

def analyze_followers(user_id, api, cache):
follower_ids = []
max_id = None

for _ in range(MAX_GET_FOLLOWER_IDS_CALLS):
try:
accounts = api.account_followers(id=user_id, max_id=max_id)
except Exception as exc:
print(f"An error occurred: {exc}")
return []
accounts = api.account_followers(id=user_id, limit=80)

for _ in range(MAX_GET_FOLLOWER_IDS_CALLS):
if not accounts:
break

follower_ids.extend([account.id for account in accounts])

if len(accounts) > 1:
max_id = accounts[-1].id - 1
else:
accounts = api.fetch_next(accounts)

if accounts is None:
break

if not follower_ids:
Expand All @@ -567,31 +559,24 @@ def analyze_followers(user_id, api, cache):
def analyze_timeline(user_id, list_id, api, cache):
# Timeline-functions are limited to 40 statuses
timeline_ids = []
max_id = None

if list_id is not None:
statuses = api.timeline_list(id=list_id, limit=40)
else:
statuses = api.timeline_home(limit=40)

# Max 400 toots, 40 at a time.
for _ in range(MAX_TIMELINE_CALLS):
try:
if list_id is not None:
statuses = api.timeline_list(
id=list_id, max_id=max_id, limit=40
)
else:
statuses = api.timeline_home(max_id=max_id, limit=40)
except Exception as exc:
print(f"An error occurred: {exc}")
return []

if not statuses:
break

timeline_ids.extend(
[s.account.id for s in statuses if s.account.id != user_id]
)

if len(statuses) > 1:
max_id = statuses[-1].id - 1
else:
statuses = api.fetch_next(statuses)

if statuses is None:
break

if not timeline_ids:
Expand All @@ -610,30 +595,25 @@ def analyze_timeline(user_id, list_id, api, cache):


def analyze_my_timeline(user_id, api, cache):
# Timeline-functions are limited to 40 statuses
try:
statuses = api.timeline(limit=40)
except Exception as exc:
print(f"An error occurred: {exc}")
return []

if not statuses:
print("No statuses returned from the API.")
return []
timeline_ids = []

max_id = None
# Timeline-functions are limited to 40 statuses
statuses = api.timeline_home(limit=40)

# Max 400 toots, 40 at a time.
for _ in range(1, MAX_TIMELINE_CALLS):

if max_id == statuses[-1].id - 1:
if not statuses:
break

max_id = statuses[-1].id - 1
try:
statuses += api.timeline(limit=40, max_id=max_id)
except Exception as exc:
print(f"An error occurred: {exc}")
timeline_ids.extend(
[s.account.id for s in statuses if s.account.id != user_id]
)

statuses = api.fetch_next(statuses)

if statuses is None:
break

reblog_ids = []
reply_ids = []
Expand Down
41 changes: 2 additions & 39 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<script type="text/javascript" async="" src="https://www.googletagmanager.com/gtag/js?id=G-LQK86MYSVJ&amp;cx=c&amp;_slc=1"></script><script async="" src="https://www.google-analytics.com/analytics.js"></script><script type="application/javascript">

function is_valid_handle(input) {
// Only accept full Mastodon handle (ie alexkalopsia@mastodon.social)
// Only accept full Mastodon handle (ie username@mastodon.social)
const regex = /^[a-zA-Z0-9_]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

return regex.test(input)
Expand All @@ -63,19 +63,15 @@
let button;
let warning;

console.log("VALIDATING")

if (document.getElementById('login_acct')) {
input = document.getElementById('login_acct').value.trim()
button = document.getElementById('login-button');
warning = document.getElementById("login-warning");
console.log("LOGIN")


} else if (document.getElementById('analyze_acct')) {
input = document.getElementById('analyze_acct').value.trim()
button = document.getElementById('analyze-button');
warning = document.getElementById("analyze-warning");
console.log("ANALYZE")

const user = document.getElementById('analyze_acct').value;
const logged_user = document.body.getAttribute('data-mastodon-user');
Expand All @@ -91,7 +87,6 @@
warning.style.display = "none";
button.classList.remove('disabled');
button.removeAttribute('aria-disabled');
console.log("VALID!")
return true;
} else {
if (input != "") {
Expand All @@ -107,10 +102,7 @@
function handle_login_submit(event) {
event.preventDefault();

console.log("LOGIN SUBMIT")

if (validate_input()) {
console.log("SUBMITTING LOGIN!")
login_submit()
}
}
Expand Down Expand Up @@ -148,8 +140,6 @@
var loading = document.getElementById("login-loading");
loading.style.display = 'block';

console.log("GOOO")

document.getElementById('login-form').submit();
}

Expand Down Expand Up @@ -181,25 +171,6 @@
alerts[i].classList.add('alert-container-disappear');
}
}

document.addEventListener('DOMContentLoaded', function() {
var userid = document.getElementById("acct");
var lst = document.getElementById("lst");
if (userid === null || lst === null) {
// Not logged in to Mastodon.
return;
}

userid.onkeyup = function() {
/* if the auth'ed user has Mastodon lists but a different user name is in
* the text box, hide the lists */
if (lst !== null) {
lst.disabled = (userid.value !== "");
}
};

setTimeout(close_alerts, 8000);
}, false);
</script>

<div class="container-fluid">
Expand All @@ -214,15 +185,7 @@ <h1 class="lead">Estimate the gender distribution of your followers and those yo
</p>
<p>I want you to be able to do this, too. Estimate the distribution of those you follow and see if there's room to improve!</p>
<hr>


<a href="#" class="btn btn-primary">Log in with Mastodon</a>






</div>
</div>
<div class="row">
Expand Down
5 changes: 1 addition & 4 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def login():

try:
response = requests.get(
f"https://{instance}/.well-known/openid-configuration"
f"https://{instance}/.well-known/oauth-authorization-server"
)
response.raise_for_status()
metadata = response.json()
Expand Down Expand Up @@ -151,9 +151,6 @@ def oauth_authorized():
f"https://{instance}/api/v1/accounts/verify_credentials"
)

print("Response status:", response.status_code)
# print("Response text:", response.text)

try:
profile = response.json()
except ValueError as e:
Expand Down
Loading

0 comments on commit 5df4159

Please sign in to comment.