Advertisement
Guest User

streetmobster - fonts 1

a guest
Sep 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. from __future__ import unicode_literals
  2.  
  3. import json
  4. import sys
  5. from random import randint
  6. from requests import Session
  7. from bs4 import BeautifulSoup
  8. import logging
  9.  
  10. from .Inventory import Item
  11. from .Map import MapAction, MapStreet
  12.  
  13.  
  14. if sys.version_info >= (3, 0):
  15. from urllib.parse import urljoin
  16. ustr = str
  17. else:
  18. from urlparse import urljoin
  19. ustr = unicode
  20.  
  21.  
  22. class LoginError(Exception):
  23. pass
  24.  
  25.  
  26. class StreetMobster(object):
  27. def __init__(self, login, pw='', proxies=None, useragent=None):
  28. logger = logging.getLogger('StreetMobster.{}'.format(login))
  29. self.logger = logger
  30.  
  31. self.login = login
  32. self.__pw = pw
  33. self.load_page_hooks = []
  34.  
  35. sess = Session()
  36. if proxies is not None:
  37. sess.proxies = proxies
  38. if useragent is not None:
  39. sess.headers.update({'User-agent': useragent})
  40.  
  41. login_page = sess.get('http://streetmobster.com')
  42. dom_login_page = BeautifulSoup(login_page.text, "html.parser")
  43. form_el = dom_login_page.find('form')
  44. form = dict([(lambda i: (i['name'], i['value']))(x) for x in form_el.find_all('input')])
  45. form['login[usr]'] = login
  46. form['login[pwd]'] = pw
  47. after_login_page = sess.post(urljoin('http://streetmobster.com', form_el['action']), form)
  48. dom_after_login_page = BeautifulSoup(after_login_page.text, "html.parser")
  49. if dom_after_login_page.find(class_="mm-street") is None:
  50. merr = dom_after_login_page.find(class_="merr")
  51. if merr is None:
  52. raise LoginError()
  53. else:
  54. raise LoginError(merr.text)
  55. self.sess = sess
  56. self.last_dom = dom_after_login_page
  57. self.last_url = 'http://streetmobster.com/auth/login'
  58.  
  59. def log_messages_hook(sm, pg):
  60. log = sm.logger.getChild('Messages')
  61. for i in pg(class_=['minfo', 'zinfo']):
  62. log.info(i.text)
  63. for i in pg(class_='merr'):
  64. log.error(i.text)
  65.  
  66. self.register_page_load_hook(log_messages_hook)
  67. self.load_page('map?_lng=en')
  68.  
  69. def register_page_load_hook(self, hook):
  70. assert callable(hook)
  71. self.load_page_hooks.append(hook)
  72.  
  73. def unregister_page_load_hook(self, hook):
  74. if hook in self.load_page_hooks:
  75. self.load_page_hooks.remove(hook)
  76. return True
  77. else:
  78. return False
  79.  
  80. def logout(self):
  81. self.load_page('auth/exit')
  82.  
  83. @property
  84. def req_pair(self):
  85. return ustr(self.last_dom).split("req_pair: 'z=")[1].split("'")[0]
  86.  
  87. def load_page(self, addr, post=False):
  88. addr = urljoin('http://streetmobster.com', addr)
  89. self.sess.cookies['clickcoords'] = ustr(randint(0, 1200)*10000 + randint(0, 1600))
  90. if isinstance(post, dict):
  91. pdata = {'z': self.req_pair}
  92. pdata.update(post)
  93. data = self.sess.post(addr, pdata, headers={'Referer': self.last_url}).text
  94. else:
  95. data = self.sess.get(addr + ('&' if '?' in addr else '?') + 'z={}'.format(self.req_pair), headers={'Referer': self.last_url}).text
  96. pg = BeautifulSoup(data, "html.parser")
  97. self.last_dom = pg
  98. self.last_url = addr
  99. for i in self.load_page_hooks:
  100. i(self, pg)
  101. return pg
  102.  
  103. @property
  104. def level(self):
  105. return int(self.last_dom.find(class_='exp-level').text)
  106.  
  107. @property
  108. def risk(self):
  109. return int(self.last_dom.find(class_='p-risk').parent.text)
  110.  
  111. @property
  112. def stamina(self):
  113. return int(self.last_dom.find(class_='p-stamina').parent.text)
  114.  
  115. @property
  116. def money(self):
  117. return int(self.last_dom.find(class_='ucash').findChild().text.replace(u'\xa0', ''))
  118.  
  119. @property
  120. def in_jail(self):
  121. user_cls = self.last_dom.find(class_='user-box')['class']
  122. return 'u-jl' in user_cls or 'fu-jl' in user_cls
  123.  
  124. @property
  125. def in_riot(self):
  126. if not self.in_jail:
  127. return False
  128. pg = self.load_page('jail')
  129. return pg.find(class_="jail_riot_1") is not None
  130.  
  131. def find_bar(self, item, price):
  132. pass
  133.  
  134. def go_bar(self):
  135. # url = self.last_dom.find(class_='p-stamina').parent['href'].split('?')[0]
  136. # if 'top10' in url:
  137. # url = self.load_page(url).find(class_='body')('td')[1].next['href'].split('?')[0]
  138. # url = 'http://streetmobster.com/bar//78672'
  139. url = '/bar//734/'
  140. self.load_page(url)
  141.  
  142. # 505 = whiskey
  143. def use_in_bar(self, type_id):
  144. type_id = ustr(type_id)
  145.  
  146. def find(x):
  147. try:
  148. return json.loads(x)['type_id'] == type_id
  149. except (ValueError, TypeError):
  150. return False
  151.  
  152. item = self.last_dom.find(rel=find).parent
  153. assert item
  154. pg = self.load_page(item['href'].split('?')[0])
  155. assert pg.find(class_='minfo') is not None
  156.  
  157. def need_stamina(self, stamina, load_back=True):
  158. if self.stamina <= stamina:
  159. back = self.last_url
  160. self.go_bar()
  161. while self.stamina <= stamina:
  162. self.use_in_bar(505)
  163. if load_back:
  164. self.load_page(back)
  165. return True
  166.  
  167. def do_map_action(self, action):
  168. assert isinstance(action, MapAction)
  169. assert action.stamina <= self.stamina
  170. pg = self.load_page(action.url)
  171. assert pg.find(class_='zinfo') is not None
  172.  
  173. @property
  174. def riot_required(self):
  175. pg = self.load_page('jail/chat')
  176. return int(list(pg.find(class_='layout').findChild().children)[4].findChild().text) if len(list(pg.find(class_='layout').findChild().children)) == 5 else -1
  177.  
  178. def join_riot(self):
  179. if not self.in_jail or self.in_riot:
  180. return False
  181. pg = self.load_page('jail')
  182. pg = self.load_page(pg.find(class_="jail_riot_0")['href'].split('?')[0])
  183.  
  184. if pg.find(class_='merr', text=lambda x: "You are too late." == x):
  185. return self.join_riot()
  186.  
  187. return bool(pg.find(class_='minfo', text=lambda x: "You're in!" == x) or pg.find(class_='minfo', text=lambda x: "rebel" in ustr(x)))
  188.  
  189. def go_jail(self):
  190. actions = (("sign3", "kick"), ("bmw", "sell"), ("dealer", "rob"), ("robbery", "kick"), ("ambulance", "rob"),
  191. ("phone", "crash"), ("dealer", "beat"), ("bmw", "crash2"), ("pimp", "rob"), ("ambulance", "crash"),
  192. ("robbery", "rob"), ("family", "thief"), ("bmw", "crash"), ("bicyclist", "rob"))
  193.  
  194. def bribe_hook(sm, pg):
  195. if pg.find(class_="escape") is not None:
  196. sm.load_page('cop/bribe', {"cop[escape]": sm.last_dom.find(class_="escape")['value']})
  197.  
  198. self.register_page_load_hook(bribe_hook)
  199.  
  200. for a in actions:
  201. if 'city' not in self.last_url:
  202. self.load_page('city')
  203. if self.in_jail:
  204. break
  205. map_ = MapStreet.from_dom(self.last_dom)
  206. spot = map_.get_spot(a[0])
  207. if spot is None:
  208. continue
  209. action = spot.get_action(a[1])
  210. if action is None:
  211. continue
  212. self.need_stamina(action.stamina + 10)
  213. self.do_map_action(action)
  214.  
  215. self.unregister_page_load_hook(bribe_hook)
  216.  
  217. def go_riot(self):
  218. if not self.in_jail:
  219. self.go_jail()
  220. return self.join_riot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement