You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

221 lines
8.7 KiB

import random
import traceback
from getopt import GetoptError, getopt
from sys import argv, exit
from time import sleep
from subprocess import check_output
from re import split
from socket import gethostname
from arangodb import db, write_report, backup, report_blocked, get_profile, remove_profile, checked_members, friends_of_user
from classes import Profile, User
from helpers import sleep_, write_error, _print
from scrapers import profile_picture_reactions
def finish():
""" Avslutar: skriver rapport och gör profilerna oanvända """
for profile in profiles:
profile.unused()
write_report(users, list(all_pictures.difference(all_pictures_start)))
exit()
if __name__ == "__main__":
print()
if gethostname() not in ['macbook.local']: # Lägg till för studiodatorn
# Hämta namn för containern där skriptet körs
try:
containers = check_output(['docker', 'container', 'ls']).decode()
container = split('\W\W+', containers.split('\n')[1])[-1]
except FileNotFoundError:
pass
else:
container_name = 'macbook'
# Argument och alternativ
argv = argv[1:]
try:
opts, args = getopt(argv, "bm:u:o:", ['backup=',"mode=", "users=", "other="])
for o, a in opts:
# mode_nr används för hur ofta profile ska roteras
if o in ["-m", "--mode"]:
mode = a
if mode == 'single':
mode_nr = 1.7
elif mode == 'few':
mode_nr = 1.4
elif mode == 'force':
mode_nr = 1
else:
mode = 'all'
mode_nr = 1
for o, a in opts:
if o in ["-u", "--user"]:
try:
users = [
User(str(i).strip(), mode)
for i in [(str(i).strip()) for i in a.split(",")]
]
except StopIteration:
raise Exception
if o in ["-o", "--other"]:
url_other_picture = a
if o in ['-b', '--backup']:
while True:
backup(db)
sleep(21600)
if "users" not in globals():
users = [
User(str(i).strip(), mode)
for i in input("Vem/vilka vill du kolla bilder för? ").split(",")
]
except GetoptError:
users = [
User(str(i).strip(), mode)
for i in input("Vem/vilka vill du kolla bilder för? ").split(",")
]
mode = input("Söka efter alla, första/sida eller första? (all, few, single)? ").lower().strip()
if mode == '':
mode = 'all'
if "url_other_picture" in globals():
users[0].url_other_picture = url_other_picture[url_other_picture.find('facebook.com') + 12:]
print("Kollar profilbilder för:")
for user in users:
print("-", user.username)
print()
if 'container' not in globals():
usernames = [user.username for user in users]
if len(usernames) == 1:
container = usernames[0]
else:
container = '-'.join(usernames)
# Skapa tre olika profiler att besöka Facebook med
profiles = []
for i in range(0, 3):
doc = get_profile()
profile = Profile(doc, container)
profile.browser.open("https://api.ipify.org")
print(f"Profil {profile.name} använder IP-adress {profile.viewing().text}."
)
if profile.logged_in == False:
profile.accept_cookies()
sleep_(2)
profile.login()
profiles.append(profile)
print()
sleep(3)
profile_nr = 1
profile = profiles[profile_nr]
_print(profile.container, user.username, f"Börjar med profilen {profile.name}")
# Gå igenom de användare som efterfrågats
try:
while True:
for user in users:
# Set för kollade bilder och kollade medlemmar
all_pictures = set([doc["_key"] for doc in db.collection("pictures").all()])
all_pictures_start = all_pictures.copy()
members_checked = checked_members()
profile.container = user.username
# Hämta reaktioner för den första användaren
if any([user.username not in members_checked, mode == 'force']):
try:
profile_picture_reactions(profile, user, all_pictures, first_user=True, mode=mode)
except:
_print(profile.container, user.username, traceback.format_exc())
if len(users) == 1:
for profile in profiles:
profile.unused()
friends = friends_of_user(user.username)
friends_unchecked = list(set(friends) - set(members_checked))
_print(profile.container, user.username, f"\nKlar med, {user.username}\n")
_print(profile.container, user.username, f"Vänner som reagerat: {len(friends)}")
_print(profile.container, user.username, "\nVänner att kolla:")
for friend in friends_unchecked:
print(friend)
_print(profile.container, user.username, ', '.join([friend for friend in friends_unchecked]), silent=True)
print()
# Hämta reaktioner för users vänner (som reagerat)
count_friends = 0
for friend in friends_unchecked:
count_friends += 1
user = User(str(friend), mode)
sleep_(2)
try:
profile_picture_reactions(
profile, user, all_pictures, mode=mode
)
except Exception as e: # Fel4
write_error(
4,
e=e,
user=user.username,
profile=profile.container,
traceback=traceback.format_exc(),
soup=profile.viewing(),
)
_print(profile.container, user.username, f"\nFel: {str(user.username)}\n")
sleep_(15)
if profile.blocked == False:
_print(profile.container, user.username, f"Klar med {user.username} \n")
# Rotera fb-profiler
if count_friends > 5 * mode_nr:
if random.randrange(0, 2, 1) == 1:
profile_nr += 1
count_friends = 0
_print(profile.container, user.username, f"Växlar till {profiles[profile_nr].name}")
elif count_friends > 9 * mode_nr:
profile_nr += 1
count_friends = 0
_print(profile.container, user.username, f"Växlar till {profiles[profile_nr].name}")
if profile_nr > len(profiles) - 1:
profile_nr = 0
elif profile.blocked == True:
# Ta bort profilen ur databasen
report_blocked(profile, users)
remove_profile(profile.doc)
# Ta bort från listan på fb-profiler som används
profiles.remove(profile)
# Försök lägga till en ny fb-profil (om det finns en skapad och ledig i databasen)
try:
doc = get_profile()
profiles[profile_nr] = Profile(doc, container)
_print(profile.container, user.username, f"Laddat ny profil: {profiles[profile_nr].name}")
sleep(3)
except e:
_print(profile.container, user.username, "Det behövs nya profiler...")
if len(profiles) == 0:
break
for s in range(0, 1600 / len(profiles)):
print(user, f"Sover {600-s} sekunder till... ", end="\r")
profile_nr += 1
_print(profile.container, user.username, f"Försöker med {profiles[profile_nr].name}.")
profile = profiles[profile_nr]
except:
finish()