Advertisement
Guest User

runserver.py

a guest
Aug 1st, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import sys
  6. import logging
  7. import time
  8.  
  9. from threading import Thread
  10. from flask import Flask, jsonify, render_template, request
  11. from flask_cors import CORS
  12.  
  13. from pogom import config
  14. from pogom.app import Pogom
  15. from pogom.utils import get_args, insert_mock_data
  16. from pogom.search import search_loop, create_search_threads, fake_search_loop
  17. from pogom.models import init_database, create_tables, Pokemon, Pokestop, Gym
  18.  
  19. from pogom.pgoapi.utilities import get_pos_by_name
  20. from pogom.pgoapi.auth_google import AuthGoogle
  21. from pogom.pgoapi.auth_ptc import AuthPtc
  22.  
  23. logging.basicConfig(format='%(asctime)s [%(module)14s] [%(levelname)7s] %(message)s')
  24. log = logging.getLogger()
  25.  
  26. app = Pogom(__name__)
  27.  
  28. def initMap() :
  29.     args = get_args()
  30.  
  31.     if args.debug:
  32.         log.setLevel(logging.DEBUG);
  33.     else:
  34.         log.setLevel(logging.INFO);
  35.  
  36.     # These are very noisey, let's shush them up a bit
  37.     logging.getLogger("peewee").setLevel(logging.INFO)
  38.     logging.getLogger("requests").setLevel(logging.WARNING)
  39.     logging.getLogger("pogom.pgoapi.pgoapi").setLevel(logging.WARNING)
  40.     logging.getLogger("pogom.pgoapi.rpc_api").setLevel(logging.INFO)
  41.     logging.getLogger('werkzeug').setLevel(logging.ERROR)
  42.  
  43.     config['parse_pokemon'] = not args.no_pokemon
  44.     config['parse_pokestops'] = not args.no_pokestops
  45.     config['parse_gyms'] = not args.no_gyms
  46.  
  47.     # Turn these back up if debugging
  48.     if args.debug:
  49.         logging.getLogger("requests").setLevel(logging.DEBUG)
  50.         logging.getLogger("pgoapi").setLevel(logging.DEBUG)
  51.         logging.getLogger("rpc_api").setLevel(logging.DEBUG)
  52.  
  53.     db = init_database()
  54.     create_tables(db)
  55.  
  56.     position = get_pos_by_name(args.location)
  57.     if not any(position):
  58.         log.error('Could not get a position by name, aborting.')
  59.         sys.exit()
  60.  
  61.     log.info('Parsed location is: {:.4f}/{:.4f}/{:.4f} (lat/lng/alt)'.
  62.              format(*position))
  63.     if args.no_pokemon:
  64.         log.info('Parsing of Pokemon disabled.')
  65.     if args.no_pokestops:
  66.         log.info('Parsing of Pokestops disabled.')
  67.     if args.no_gyms:
  68.         log.info('Parsing of Gyms disabled.')
  69.  
  70.     config['ORIGINAL_LATITUDE'] = position[0]
  71.     config['ORIGINAL_LONGITUDE'] = position[1]
  72.     config['LOCALE'] = args.locale
  73.     config['CHINA'] = args.china
  74.  
  75.     if not args.only_server:
  76.         # Gather the pokemons!
  77.         if not args.mock:
  78.             log.debug('Starting a real search thread and {} search runner thread(s)'.format(args.num_threads))
  79.             create_search_threads(args.num_threads)
  80.             search_thread = Thread(target=search_loop, args=(args,))
  81.         else:
  82.             log.debug('Starting a fake search thread')
  83.             insert_mock_data()
  84.             search_thread = Thread(target=fake_search_loop)
  85.  
  86.         search_thread.daemon = True
  87.         search_thread.name = 'search_thread'
  88.         search_thread.start()
  89.  
  90.     if args.cors:
  91.         CORS(app);
  92.  
  93.     config['ROOT_PATH'] = app.root_path
  94.     config['GMAPS_KEY'] = args.gmaps_key
  95.     config['REQ_SLEEP'] = args.scan_delay
  96.  
  97.  
  98. @app.route('/ajaxTryStart', methods=['POST'])
  99. def ajaxTryStart() :
  100.     username = request.form['username']
  101.     password = request.form['password']
  102.     location = request.form['location']
  103.     gmapsKey = request.form['gmapsKey']
  104.  
  105.     steps = '5'
  106.     scanDelay = '5'
  107.  
  108.     authType = 'ptc'
  109.     authProvider = AuthPtc()
  110.     if '@gmail' in username :
  111.         authProvider = AuthGoogle()
  112.         authType = 'google'
  113.  
  114.     if authProvider.login(username, password):
  115.         mapConfig['authType'] = authType
  116.         mapConfig['username'] = username
  117.         mapConfig['password'] = password
  118.         mapConfig['location'] = location
  119.         mapConfig['gmapsKey'] = gmapsKey
  120.         mapConfig['steps'] = steps
  121.         mapConfig['scanDelay'] = scanDelay
  122.         app.mapConfig = mapConfig
  123.         initMap()
  124.         #return jsonify(errors=False,success='Inloggning lyckades')
  125.     else :
  126.         return jsonify(errors='Fel inloggningsuppgifter',success=False)
  127.  
  128.  
  129. @app.route('/')
  130. def controlPanel() :
  131.     return render_template('index.html')
  132.  
  133.  
  134. if __name__ == '__main__':
  135.     app.run(threaded=True, use_reloader=False, debug=False, host='127.0.0.1', port='5000')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement