Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: healthy user checks and minimum followers #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions GithubAPIBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from urllib3.util import Retry


user_cache = {}

class GithubAPIBot:
# Constructor
def __init__(
Expand All @@ -24,6 +26,8 @@ def __init__(
sleepMinute=None,
sleepTime=None,
maxAction=None,
minFollowers=0,
healthyUser=False
):
if not isinstance(username, str):
raise TypeError("Missing/Incorrect username")
Expand All @@ -42,6 +46,8 @@ def __init__(
self.__maxAction = maxAction
self.__usersToAction = []
self.__followings = []
self.__minFollowers = minFollowers
self.__healthyUser = healthyUser

# Requests' headers
HEADERS = {
Expand Down Expand Up @@ -164,6 +170,29 @@ def followings(self):
def followings(self, value):
self.__followings = value

def getUser(self, username):
user = user_cache.get(username)
if user is None:
user = self.session.get("https://api.github.com/users/" + username).json()
user_cache.update({ username: user })
return user

def isHealtyUser(self, username):
user = self.getUser(username)
if user["followers"] == 0 or user["following"] == 0:
return False

# If the user has a low follow ratio, he is not a healthy user
follow_ratio = user["followers"] / user["following"]
if follow_ratio <= 1 or user["public_repos"] <= 10:
return False

return True

def hasMinimumFollowers(self, username):
user = self.getUser(username)
return user["followers"] >= self.__minFollowers

def getUsers(self, url="", maxAction=None, following=False):
users = []

Expand Down Expand Up @@ -200,14 +229,23 @@ def getUsers(self, url="", maxAction=None, following=False):
if len(users) >= int(maxAction):
break

# Add username if it's not being followed already
if (
not following
and not (user["login"] in self.followings)
or following
and (user["login"] in self.followings)
):
users.append(user["login"])

should_append = True;
if self.__minFollowers > 0 and self.hasMinimumFollowers(user["login"]) == False:
should_append = False

if self.__healthyUser and self.isHealtyUser(user["login"]) == False:
should_append = False

if should_append:
users.append(user["login"])


# Check if we already have enough usernames
if maxAction != None:
Expand Down Expand Up @@ -335,4 +373,4 @@ def sleepUntil(hour, minute):
for second in sleepSecondsBar:
time.sleep(1)

print(f'\nWaking up...')
print(f'\nWaking up...')
6 changes: 6 additions & 0 deletions bot_follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
parser.add_argument("-sh", "--sleep-hour", help="Hour for the bot to go to sleep")
parser.add_argument("-sm", "--sleep-minute", help="Minute for the bot to go to sleep")
parser.add_argument("-st", "--sleep-time", help="Total time (in hours) for the bot to sleep")
parser.add_argument("-mf", "--min-followers", help="Minimum number of followers the user should have to be followed")
parser.add_argument("-hl", "--healthy-user", help="Follow only active and 'healthy' users", action="store_true")
args = parser.parse_args()

sleepSecondsActionMin = int(args.sleep_min or 20)
sleepSecondsActionMax = int(args.sleep_max or 120)
sleepSecondsLimitedMin = int(args.sleep_min_limited or 600)
sleepSecondsLimitedMax = int(args.sleep_max_limited or 1500)
minFollowers = int(args.min_followers or 0)
healthyUser = bool(args.healthy_user or False)

load_dotenv()
USER = os.getenv("GITHUB_USER")
Expand All @@ -46,6 +50,8 @@
args.sleep_minute,
args.sleep_time,
args.max_follow,
minFollowers,
healthyUser
)


Expand Down