Advertisement
Guest User

Untitled

a guest
Oct 8th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. """
  2. Klasa do logowania się do Souvre i pobierania
  3. drzewka z informacjami
  4. """
  5.  
  6. import re
  7. from robobrowser import RoboBrowser
  8. from datetime import datetime
  9. from bs4 import BeautifulSoup, element
  10. import sqlite3
  11. import os
  12. import shutil
  13. from getpass import getpass
  14.  
  15. class Person(object):
  16.  
  17. id = None
  18. leaf = 'Y'
  19.  
  20. def __init__(self, html, parent_id):
  21. self.parse_from_html(html, parent_id)
  22. super().__init__()
  23.  
  24. def parse_from_html(self, html, parent_id):
  25. if type(html) is element.Tag:
  26. #print(html)
  27.  
  28. if html.find(class_='btn collapsed title') != None:
  29. self.id = html.find(class_='btn collapsed title')['aria-controls'][2:]
  30. self.leaf = 'N'
  31. else:
  32. if html.find(class_='name-sub').div.form != None:
  33. self.id = html.find(class_='name-sub').div.form.find(attrs={"name": "messageUserLogin"})['value']
  34. else:
  35. self.id = '-'+str(parent_id)
  36.  
  37. self.name = html.find(class_='name-sub').div.p.text
  38. self.partner_nr = html.find(class_='date2').find_all('p')[0].strong.text
  39. self.rej_date = html.find(class_='date2').find_all('p')[2].strong.text
  40. self.account_type = html.find(class_='date2').find_all('p')[0].text[0]
  41. self.balance = html.find(class_='col-sm-5 col-xs-12 no-padding date2').find_all('p')[0].span.text
  42. self.user_points = html.find(class_='col-sm-5 col-xs-12 no-padding date2').find_all('p')[1].span.text
  43. self.group_points = html.find(class_='col-sm-5 col-xs-12 no-padding date2').find_all('p')[2].span.text
  44. self.group_count = html.p.text
  45. self.parent_id = parent_id
  46. self.level = html.find(class_='dsc-inline').p.text
  47. #osób w grupie html.div.p.text
  48. else:
  49. return None
  50.  
  51. # + self.parent_id \
  52. #+ '\nid \t\t= ' + self.id \
  53. def __str__(self):
  54. return '\nparent_id \t= ' + str(self.parent_id) \
  55. + '\nid \t\t= ' + self.id \
  56. + '\nname \t\t= ' + self.name \
  57. + '\npartner_nr \t= ' + self.partner_nr \
  58. + '\nrej_date \t= ' + self.rej_date \
  59. + '\nbalance \t= ' + self.balance \
  60. + '\nuser_points \t= ' + self.user_points \
  61. + '\ngroup_points \t= ' + self.group_points \
  62. + '\ngroup_count \t= ' + self.group_count \
  63. + '\nlevel \t\t= ' + self.level \
  64. + '\naccount_type \t= ' + self.account_type \
  65. + '\nleaf \t\t= ' + self.leaf
  66.  
  67.  
  68. class Souvre(RoboBrowser):
  69.  
  70. url = 'https://souvre.pl/MLM/panel/login'
  71. year = datetime.now().year
  72. month = datetime.now().month-1
  73. current_root= 1 #134
  74. root_url = 'https://souvre.pl/team_ajax_action/'+str(month)+'/'+str(year)+'/'+str(current_root)
  75. root_data = None
  76.  
  77. def __init__(self, userLogin, password):
  78. super().__init__()
  79. self.userLogin = userLogin
  80. self.password = password
  81. self.user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0'
  82. self.parser = 'html.parser'
  83. self.history = True
  84. self.login()
  85.  
  86. def login(self):
  87. self.open(self.url)
  88. login_title = self.find(class_='login-title')
  89. login_form = self.get_form(action='/MLM/panel/login/validate')
  90. login_form['userLogin'] = self.userLogin
  91. login_form['userPass'] = self.password
  92. self.submit_form(login_form)
  93.  
  94. def set_date(self, year=year, month=month):
  95. self.year = year
  96. self.month = month
  97.  
  98. def set_root(self, root, year=year, month=month):
  99. self.current_root = root
  100. self.year = year
  101. self.month = month
  102. self.root_url = 'https://souvre.pl/team_ajax_action/'+str(month)+'/'+str(year)+'/'+str(root)
  103.  
  104. def get_root_data(self, root=current_root, year=year, month=month):
  105. self.set_root(root, year, month)
  106. self.open(self.root_url)
  107. #clear response
  108. text = self.response.text.replace('{"response":"ok","html":"','').replace('"}','')
  109. text = text.replace('\\r\\n','').replace('<\\','<').replace('\\"','"')
  110. text = text.replace('\\u00f3','ó').replace('\\u00f2','Ó').replace('\\u015b','ś').replace('\\u015a','Ś')
  111. text = text.replace('\\u0105','ą').replace('\\u0104','Ą').replace('\\u0144','ń').replace('\\u0143','Ń')
  112. text = text.replace('\\u0107','ć').replace('\\u0106','Ć').replace('\\u0142','ł').replace('\\u0141','Ł')
  113. text = text.replace('\\u0119','ę').replace('\\u0118','Ę').replace('\\u017c','ż').replace('\\u016c','Ż')
  114. self.root_data = BeautifulSoup(text, features='html.parser')
  115.  
  116. class DB_handler(object):
  117. connection = None
  118. template_path = 'db/template.db'
  119.  
  120. def __init__(self, database_path):
  121. super().__init__()
  122. if not os.path.isfile(database_path):
  123. shutil.copyfile(self.template_path, database_path)
  124. self.database_path = database_path
  125. self.connect(database_path)
  126.  
  127. def connect(self, db):
  128. self.connection = sqlite3.connect(db)
  129.  
  130. def insert_person(self,person):
  131. print('INSERT Person ' + str(person.id))
  132. table_name='Person'
  133. column_names=('id', 'name', 'partner_nr', 'rejestration_date', 'account_type', 'balance', 'user_points', 'group_points', 'group_count', 'parent_id', 'level', 'leaf')
  134. #x=person
  135. 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)
  136. self.connection.execute("INSERT INTO {tn} {cn} VALUES {cv}".\
  137. format(tn=table_name, cn=column_names, cv=column_values))
  138. #self.connection.execute("INSERT INTO {tn} {cn} VALUES {cv}".format(tn=table_name, cn=('id','name'), cv=(person.id, person.name)))
  139. self.connection.commit()
  140.  
  141. db = DB_handler('db/2018_09.db')
  142.  
  143. user = '444091'
  144. #print('Pasword for user ' + user + ': ')
  145. password = getpass('Pasword for user ' + user + ': ')
  146.  
  147. sv = Souvre(user, password)
  148.  
  149. max_level = 2
  150. def get_all(root_id,level):
  151. if level == max_level:
  152. return
  153. level += 1
  154. sv.get_root_data(root=root_id)
  155. s = sv.root_data
  156. for child in s.children:
  157. #print(child)
  158. if type(child) is element.Tag:
  159. if child.div:
  160. p = Person(child, root_id)
  161. db.insert_person(p)
  162. print('level ' + str(level))
  163. #print(p)
  164. print('\n----------------\n')
  165. #get_all(p.id,level)
  166.  
  167. get_all(134,0)
  168. db.connection.close()
  169.  
  170. '''
  171. def main():
  172. s = Souvre('444091','Irbis333!@#')
  173.  
  174. if __name__ == "__main__":
  175. main()
  176. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement