Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Klasa do logowania się do Souvre i pobierania
- drzewka z informacjami
- """
- import re
- from robobrowser import RoboBrowser
- from datetime import datetime
- from bs4 import BeautifulSoup, element
- import sqlite3
- import os
- import shutil
- from getpass import getpass
- class Person(object):
- id = None
- leaf = 'Y'
- def __init__(self, html, parent_id):
- self.parse_from_html(html, parent_id)
- super().__init__()
- def parse_from_html(self, html, parent_id):
- if type(html) is element.Tag:
- #print(html)
- if html.find(class_='btn collapsed title') != None:
- self.id = html.find(class_='btn collapsed title')['aria-controls'][2:]
- self.leaf = 'N'
- else:
- if html.find(class_='name-sub').div.form != None:
- self.id = html.find(class_='name-sub').div.form.find(attrs={"name": "messageUserLogin"})['value']
- else:
- self.id = '-'+str(parent_id)
- self.name = html.find(class_='name-sub').div.p.text
- self.partner_nr = html.find(class_='date2').find_all('p')[0].strong.text
- self.rej_date = html.find(class_='date2').find_all('p')[2].strong.text
- self.account_type = html.find(class_='date2').find_all('p')[0].text[0]
- self.balance = html.find(class_='col-sm-5 col-xs-12 no-padding date2').find_all('p')[0].span.text
- self.user_points = html.find(class_='col-sm-5 col-xs-12 no-padding date2').find_all('p')[1].span.text
- self.group_points = html.find(class_='col-sm-5 col-xs-12 no-padding date2').find_all('p')[2].span.text
- self.group_count = html.p.text
- self.parent_id = parent_id
- self.level = html.find(class_='dsc-inline').p.text
- #osób w grupie html.div.p.text
- else:
- return None
- # + self.parent_id \
- #+ '\nid \t\t= ' + self.id \
- def __str__(self):
- return '\nparent_id \t= ' + str(self.parent_id) \
- + '\nid \t\t= ' + self.id \
- + '\nname \t\t= ' + self.name \
- + '\npartner_nr \t= ' + self.partner_nr \
- + '\nrej_date \t= ' + self.rej_date \
- + '\nbalance \t= ' + self.balance \
- + '\nuser_points \t= ' + self.user_points \
- + '\ngroup_points \t= ' + self.group_points \
- + '\ngroup_count \t= ' + self.group_count \
- + '\nlevel \t\t= ' + self.level \
- + '\naccount_type \t= ' + self.account_type \
- + '\nleaf \t\t= ' + self.leaf
- class Souvre(RoboBrowser):
- url = 'https://souvre.pl/MLM/panel/login'
- year = datetime.now().year
- month = datetime.now().month-1
- current_root= 1 #134
- root_url = 'https://souvre.pl/team_ajax_action/'+str(month)+'/'+str(year)+'/'+str(current_root)
- root_data = None
- def __init__(self, userLogin, password):
- super().__init__()
- self.userLogin = userLogin
- self.password = password
- self.user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0'
- self.parser = 'html.parser'
- self.history = True
- self.login()
- def login(self):
- self.open(self.url)
- login_title = self.find(class_='login-title')
- login_form = self.get_form(action='/MLM/panel/login/validate')
- login_form['userLogin'] = self.userLogin
- login_form['userPass'] = self.password
- self.submit_form(login_form)
- def set_date(self, year=year, month=month):
- self.year = year
- self.month = month
- def set_root(self, root, year=year, month=month):
- self.current_root = root
- self.year = year
- self.month = month
- self.root_url = 'https://souvre.pl/team_ajax_action/'+str(month)+'/'+str(year)+'/'+str(root)
- def get_root_data(self, root=current_root, year=year, month=month):
- self.set_root(root, year, month)
- self.open(self.root_url)
- #clear response
- text = self.response.text.replace('{"response":"ok","html":"','').replace('"}','')
- text = text.replace('\\r\\n','').replace('<\\','<').replace('\\"','"')
- text = text.replace('\\u00f3','ó').replace('\\u00f2','Ó').replace('\\u015b','ś').replace('\\u015a','Ś')
- text = text.replace('\\u0105','ą').replace('\\u0104','Ą').replace('\\u0144','ń').replace('\\u0143','Ń')
- text = text.replace('\\u0107','ć').replace('\\u0106','Ć').replace('\\u0142','ł').replace('\\u0141','Ł')
- text = text.replace('\\u0119','ę').replace('\\u0118','Ę').replace('\\u017c','ż').replace('\\u016c','Ż')
- self.root_data = BeautifulSoup(text, features='html.parser')
- class DB_handler(object):
- connection = None
- template_path = 'db/template.db'
- def __init__(self, database_path):
- super().__init__()
- if not os.path.isfile(database_path):
- shutil.copyfile(self.template_path, database_path)
- self.database_path = database_path
- self.connect(database_path)
- def connect(self, db):
- self.connection = sqlite3.connect(db)
- def insert_person(self,person):
- print('INSERT Person ' + str(person.id))
- table_name='Person'
- column_names=('id', 'name', 'partner_nr', 'rejestration_date', 'account_type', 'balance', 'user_points', 'group_points', 'group_count', 'parent_id', 'level', 'leaf')
- #x=person
- column_values=(person.id, person.name, person.partner_nr, person.rej_date, person.account_type, person.balance, person.user_points, person.group_points, person.group_count, person.parent_id, person.level, person.leaf)
- self.connection.execute("INSERT INTO {tn} {cn} VALUES {cv}".\
- format(tn=table_name, cn=column_names, cv=column_values))
- #self.connection.execute("INSERT INTO {tn} {cn} VALUES {cv}".format(tn=table_name, cn=('id','name'), cv=(person.id, person.name)))
- self.connection.commit()
- db = DB_handler('db/2018_09.db')
- user = '444091'
- #print('Pasword for user ' + user + ': ')
- password = getpass('Pasword for user ' + user + ': ')
- sv = Souvre(user, password)
- max_level = 2
- def get_all(root_id,level):
- if level == max_level:
- return
- level += 1
- sv.get_root_data(root=root_id)
- s = sv.root_data
- for child in s.children:
- #print(child)
- if type(child) is element.Tag:
- if child.div:
- p = Person(child, root_id)
- db.insert_person(p)
- print('level ' + str(level))
- #print(p)
- print('\n----------------\n')
- #get_all(p.id,level)
- get_all(134,0)
- db.connection.close()
- '''
- def main():
- s = Souvre('444091','Irbis333!@#')
- if __name__ == "__main__":
- main()
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement