def send_feedback(update, text, keyboard=None): logger.info("into send_feedback") if not keyboard: pass update.effective_message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard)
def get_feedback(update, context): logger.info("into get_feedback") chat = update.effective_chat threading.Thread(target=user_collect, args=(chat, ), daemon=True).start() threading.Thread(target=command_collect, args=("feedback", ), daemon=True).start() context.bot.send_chat_action(chat_id=chat.id, action=ChatAction.TYPING) # ONLY send help in PM if chat.type != chat.PRIVATE: context.args = ['feedback'] get_help(update, context) else: # checks if command has arguments if context.args: feedback = " ".join(context.args) try: feedback_collect(chat, feedback) msg = "ThankYou for Feedback \U0001F4E9" send_feedback(update=update, text=msg, keyboard=None) except Exception as e: pass else: context.args = ['feedback'] get_help(update, context)
def run(self): sock = self.__create_socket() info("Serveur lancé !") sock.listen() while True: conn, address = sock.accept() info("Nouvelle connexion" + str(conn) + " / " + str(address))
def _login(): """Login""" from server import logger logger.info("Running _login") logger.info("Request {}".format(request.args.to_dict())) bag = { "query_params": request.args.to_dict(), "response_body": {}, } validate_query_params(bag) db = Database() cursor, conn = db.connect() sql = """select email, id, name, phone from user where email='{}' and password='******' """.format( request.args['email'], request.args['password'] ) cursor.execute(sql) data = cursor.fetchone() db.close() if not data: raise ResourceNotFound() return send_response(data)
def get_qpapers(update, context): logger.info("into get_qpapers") chat = update.effective_chat threading.Thread(target=user_collect, args=(chat, ), daemon=True).start() threading.Thread(target=command_collect, args=("qpapers", ), daemon=True).start() context.bot.send_chat_action(chat_id=chat.id, action=ChatAction.TYPING) # ONLY send help in PM if chat.type != chat.PRIVATE: context.args = ['qpapers'] get_help(update, context) else: # called default button_list = [] for module, tag in COURSES_LIST.items(): callback_data = 'qa={}'.format(tag) text = "{}".format(module) button_list.append( InlineKeyboardButton( text=text, callback_data=callback_data, )) reply_markup_keyboard = InlineKeyboardMarkup( button_menu.build_menu(button_list, n_cols=2)) send_qpapers(update=update, text="Select Course", keyboard=reply_markup_keyboard)
def _registration(): """Register""" from server import logger bag = { 'request_body': request.get_json(), 'response_body': {}, } logger.info(bag) request_body = bag.get('request_body') validate_request_body(bag) _id = get_uuid() db = Database() try: cursor, conn = db.connect() cursor.execute( """insert into user(id, email, name, password, phone) values(%s, %s, %s, %s, %s)""", ( _id, request_body['email'], request_body['name'], request_body['password'], request_body['phone'] ) ) except pymysql.err.IntegrityError as e: logger.error(e) raise DuplicateEntry else: pass return send_response( data={'id': _id}, message='Resource Created Successfully', status_code=201 )
def send_help(update, text, keyboard=None): logger.info("into send_help") if not keyboard: # add keyboard here # keyboard = None pass update.effective_message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard)
def run(): logger.info(f'Starting {name} %s!', __version__) app = build_app("spatialite://db.sqlite3") loop = asyncio.get_event_loop() aiomonitor.start_monitor(loop=loop, locals={"app": app}, monitor=Tap2GoMonitor) web.run_app(app)
def course_branch_sem(text): button_list = [] colm = 1 course_full = branch_full = '' course_in, branch_in, sem_in = text for module, tag in COURSES_LIST.items(): if course_in == tag: course_full = module break for branch, key in BRANCHES_COURSE[course_full].items(): if branch_in == key: branch_full = branch break word = """Selected Course: `{}` \nSelected Branch: `{}` \nSelected Sem: `{}` \nSelect Subject :""". \ format(course_full, branch_full, sem_in) # we don't use 'papers' here just use subs subs, papers = qpaper_utils.collect_subs_n_papers(course_full, branch_full, sem_in) pre = [course_in, branch_in, sem_in] if len(subs) == 0: word += '\n _Unavailable_ \U0001F615' query_log = { 'level': 3, 'course': course_full, 'branch': branch_full, 'semester': sem_in, 'subject': "", 'available': False } threading.Thread(target=query_collect, args=(query_log, ), daemon=True).start() else: for sub in subs: sub_ = ''.join(sub.split(' ')) callback_str = "qa={}".format('+'.join(pre)) callback_str += "+" + sub_ if len(callback_str) > 64: logger.error("".join(text) + "=>" + callback_str + "#" + str(len(callback_str))) callback_str = callback_str[:64] logger.info("serialized to =>" + callback_str) button_list.append( InlineKeyboardButton( text="{}".format(sub), callback_data=callback_str, )) # back_button data back_data = "qa={}".format("+".join([course_in, branch_in])) return button_list, word, colm, back_data
def google(): access_token_url = 'https://www.googleapis.com/oauth2/v4/token' people_api_url = 'https://www.googleapis.com/oauth2/v3/userinfo' tokeninfo_url = 'https://www.googleapis.com/oauth2/v3/tokeninfo' logger.debug('google request =>') logger.debug(request.json) payload = dict(client_id=request.json['clientId'], redirect_uri=request.json['redirectUri'], client_secret=app.config['GOOGLE_CLIENT_SECRET'], code=request.json['code'], grant_type='authorization_code') logger.debug('Google Payload =>') logger.debug(payload) # Step 2. Exchange authorization code for access token. r = requests.post(access_token_url, data=payload) token = json.loads(r.text) logger.debug('Access Token =>') logger.debug(token) # Step 2. Retrieve information about the current user. # create user if not exists one headers = {'Authorization': 'Bearer {0}'.format(token['access_token'])} r = requests.get(people_api_url, headers=headers) profile = json.loads(r.text) logger.info('Login as => %s' % profile['email']) logger.debug(profile) if security.is_valid_email(profile['email']): # Step 4. Create a new account or return an existing one. r = requests.get('%s?access_token=%s' % (tokeninfo_url, token['access_token'])) token_info = json.loads(r.text) logger.debug('Tokeninfo =>') logger.debug(token_info) # u = User(id=profile['sub'], provider='google', # display_name=profile['name']) # Step 5. Create a new account or return an existing one. payload = { 'sub': profile['sub'], 'iat': datetime.utcnow(), 'exp': token_info['exp'], 'access_token': token['access_token'] } jwt = security.create_token(payload) return jsonify(token=jwt) else: return not_authorized( 403, 'Invalid email domain. Please sign with ciandt.com acccount')
async def start_background_tasks(app: Application): """Starts the background tasks.""" logger.info("Starting Background Tasks") loop = asyncio.get_event_loop() app['ticket_cleaner'] = loop.create_task( BikeSocketView.open_tickets.remove_all_expired(timedelta(hours=1))) loop.create_task(app['reservation_sourcer'].run()) loop.create_task(app['statistics_reporter'].run()) if isinstance(app['token_verifier'], FirebaseVerifier): loop.create_task(app['token_verifier'].get_keys(timedelta(days=1)))
def setup_database(): global db MONGO_URL = secrets.get_database_credentials() if MONGO_URL and False: # Heroku client = MongoClient(MONGO_URL) else: # Local (Development) client = MongoClient() logger.info("* Using LOCAL database") db = client['keylogger']
def init_database(self) -> None: """ Se connecte à la base de données. :return: """ try: self.db = Database("127.0.0.1", "root", "", "tipe") info("Connecté à la base de données") except: error( "Une erreur est survenue lors de la connexion à la base de données" )
def register_views(app: Application, base: str): """ Registers all the API views onto the given router at a specific root url. :param app: The app to register the views to. :param base: The base URL. """ cors = aiohttp_cors.setup(app) for view in views: logger.info("Registered %s at %s", view.__name__, base + view.url) view.register_route(app, base) view.enable_cors(cors)
def get_line_plot(names): df = convert_date_to_pandas(get_time_series_from_db()) df = df.replace({'name': get_aliases()}) # Groupby common names in the same month df = df.groupby([df.index, 'name']).sum().reset_index(level=1) filtered = filter_df(df, names) try: pivoted = pivot_df(filtered) logger.info("Obtained DataFrame with unique name/count pairs.") fig = plot_lines(pivoted) except Exception as e: # Failsafe in case all values from dropdown are deleted fig = {} # logger.error("Monthly trends app error:", e) return fig
def get_help(bot: Bot, update: Update, args: List[str]): logger.info("into get_help") chat = update.effective_chat logger.info(args) bot.send_chat_action(chat_id=chat.id, action=ChatAction.TYPING) # ONLY send help in PM if chat.type != chat.PRIVATE: send_help( update, "Contact me in PM to get the list of possible commands.", InlineKeyboardMarkup([[ InlineKeyboardButton(text="Help", url="t.me/{}?start=help".format( bot.username)) ]])) return elif len(args) >= 1 and any(args[0].lower() == x for x in HELPER_SCRIPTS): print(args) print(HELPER_SCRIPTS) module = args[0].lower() text = "Here is the available help for the *{}* module:\n{}".format( module, HELPER_SCRIPTS[module]) send_help( update, text, InlineKeyboardMarkup([[ InlineKeyboardButton(text="Back", callback_data="help_back") ]])) else: button_list = [] for module in HELPER_SCRIPTS: button_list.append( InlineKeyboardButton( text="/{}".format(module), callback_data="help_action={}".format(module), )) reply_markup_keyboard = InlineKeyboardMarkup( Utils.build_menu(button_list, n_cols=2)) send_help(update=update, text=HELP_STRINGS.format(bot.first_name, ), keyboard=reply_markup_keyboard)
def get_sources_and_people(people, sources): """Collect sources and people mentioned in the text based on their gender.""" people_genders = get_genders(people) sources_and_people = dict() temp = dict() for val in ['female', 'male', 'unknown']: temp[val] = [person for person, gender in people_genders.items() if gender == val and person in sources] temp[val] = list(filter(bool, temp[val])) sources_and_people['sources'] = temp temp = dict() for val in ['female', 'male', 'unknown']: temp[val] = [person for person, gender in people_genders.items() if gender == val] temp[val] = list(filter(bool, temp[val])) sources_and_people['people'] = temp logger.info(sources_and_people) return sources_and_people
def qpapers_button(update, context): chat = update.effective_chat context.bot.send_chat_action(chat_id=chat.id, action=ChatAction.TYPING) query = update.callback_query logger.info(query.data) course_match = re.match(r"qa=(.+?)", query.data) back_button_match = re.match(r"qa_back", query.data) try: if course_match: text = query.data.split('=', 1)[1].split('+') logger.info(text) back_data = "qa_back" if len(text) == 1: # course selected => select branch button_list, word, colm = course(text) elif len(text) == 2: # course, branch selected => select sem button_list, word, colm, back_data = course_branch(text) elif len(text) == 3: # course, branch, sem selected => select subject button_list, word, colm, back_data = course_branch_sem(text) elif len(text) == 4: # course, branch, sem, subject selected => send the links here button_list, word, colm, back_data = course_branch_sem_sub( text) else: word = "Some Unknown Error\n Use /feedback to send feedback about this error)" send_qpapers(update, text=word, keyboard=None) return # adding back button for easy traversing footer_button = [ InlineKeyboardButton(text="[Back]", callback_data=back_data) ] reply_markup_keyboard = InlineKeyboardMarkup( button_menu.build_menu(button_list, n_cols=colm, footer_buttons=footer_button)) send_qpapers(update, text=word, keyboard=reply_markup_keyboard) elif back_button_match: get_qpapers(update, context) # ensure no spinning white circle context.bot.answer_callback_query(query.id) query.message.delete() except BadRequest as e: if e.message == "Message is not modified": pass elif e.message == "Query_id_invalid": pass elif e.message == "Message can't be deleted": pass else: logger.exception("Exception : %s", str(query.data))
def get_lollipop_plot(stats): """Create pie chart subplot figure objects for dash""" # df = get_sources_dataframe(stats, limit=set_limit) df = get_sources_dataframe(stats) minval, maxval = df['count'].min(), df['count'].max() # Separate into female and male dataframes female_df = df[df['gender'] == 'F'].reset_index() male_df = df[df['gender'] == 'M'].reset_index() logger.info(f"Top sources: Obtained female dataframe of length {len(female_df)} " f"and male dataframe of length {len(male_df)}.") # Female dots data1 = go.Scatter( x=female_df['count'], y=female_df.index, text=female_df['name'], mode='markers+text', textposition='bottom left', cliponaxis=False, marker=dict(size=10, color='rgb(175, 24, 88)'), hoverinfo='x+text', hovertemplate='%{text}<br>%{x} quotes<extra></extra>', name='Female sources', ) # Male dots data2 = go.Scatter( x=male_df['count'], y=male_df.index, text=male_df['name'], mode='markers+text', textposition='top right', cliponaxis=False, marker=dict(size=10, color='rgb(0, 77, 114)'), hoverinfo='x+text', hovertemplate='%{text}<br>%{x} quotes<extra></extra>', name='Male sources', ) # Horizontal line connector shapes = [dict( type='line', x0=female_df['count'].loc[i], y0=female_df.index[i], x1=male_df['count'].loc[i], y1=male_df.index[i], layer='below', line=dict( color='rgb(200, 200, 200)', width=2 )) for i in range(len(female_df)) ] # Pass shapes to layout layout = go.Layout(shapes=shapes) # Figure object settings fig = go.Figure([data1, data2], layout) fig['layout'].update( height=40 * NUM_SOURCES_TO_SHOW + 200, # width=900, legend=dict(orientation='h', x=0.27, y=1.07, font=dict(size=15)), paper_bgcolor='rgba(0, 0, 0, 0)', plot_bgcolor='rgba(102, 204, 204, 0.05)', xaxis=dict( showgrid=True, zeroline=True, title_text='# Articles in which quoted', range=[minval - 100, maxval + 100], ticks='outside', tickfont=dict(size=18), automargin=True, gridcolor='rgb(240, 240, 240)', zerolinecolor='rgba(240, 240, 240, 0.7)', ), yaxis=dict( showgrid=False, zeroline=False, automargin=True, showticklabels=False ), margin=dict(l=80, r=80, t=30, b=30), modebar=dict( orientation='v', bgcolor='rgba(255, 255, 255, 0.7)', ), ) return fig
async def close_connections(self): if self._bike_connections: logger.info("Closing all open bike connections") for connection in self._bike_connections.values(): await connection.close(code=WSCloseCode.GOING_AWAY) self._bike_connections = {}
def logout(user): logger.info('Logout by %s' % user['email']) access_token = user['oauth_token'] security.revoke_token(access_token) return "Logout Success", 200
async def get(self): """ Initiates the websocket connection between the bike and the server. Requires an open ticket (which can be created by posting) to succeed. """ socket = web.WebSocketResponse() await socket.prepare(self.request) remote = self.request.remote public_key = await socket.receive_bytes(timeout=0.5) signature = await socket.receive_bytes(timeout=0.5) try: ticket = self.open_tickets.pop_ticket(remote, public_key) except KeyError: await socket.send_str("fail:no_ticket") return socket # verify the signed challenge try: verify_key = VerifyKey(ticket.bike.public_key, encoder=RawEncoder) verify_key.verify(ticket.challenge, signature) except BadSignatureError: await socket.send_str("fail:invalid_sig") return socket logger.info("Bike %s connected", ticket.bike.id) await self.bike_connection_manager.add_connection(ticket.bike, socket) ticket.bike.socket = socket await socket.send_str("verified") status = await socket.receive_json() if "locked" in status: self.bike_connection_manager.update_locked(ticket.bike.id, status["locked"]) try: async for msg in socket: msg: WSMessage = msg try: data = msg.json() except JSONDecodeError: continue else: if "method" in data: valid_data = JsonRPCRequest().load(data) if "id" not in valid_data and valid_data[ "method"] == "location_update": point = Point(valid_data["params"]["long"], valid_data["params"]["lat"]) await self.bike_connection_manager.update_location( ticket.bike.id, point) self.bike_connection_manager.update_battery( ticket.bike.id, valid_data["params"]["bat"]) else: valid_data = JsonRPCResponse().load(data) await self.bike_connection_manager.resolve_command( ticket.bike.id, valid_data["id"], valid_data["result"]) finally: logger.info("Bike %s disconnected", ticket.bike.id) await socket.close() del self.bike_connection_manager._bike_connections[ticket.bike.id] del ticket del socket
""" Handles connections """ import logging from pymongo import MongoClient from redis import Redis from config.loader import CONFIG from lib.utils import alerts from server import logger logging.basicConfig(level=logging.DEBUG) try: # MongoDB connection logger.info('Establishing MongoDB Connection') mongo_client = MongoClient( f'mongodb+srv://{CONFIG["mongo_atlas"]["username"]}:{CONFIG["mongo_atlas"]["password"]}@{CONFIG["mongo_atlas"]["host"]}/{CONFIG["mongo_atlas"]["db_name"]}', CONFIG['mongo_atlas']['port']) mongo_client = mongo_client[CONFIG['mongo_atlas']['db_name']]['items'] # Redis connection logger.info('Establishing Redis Connection') redis_client = Redis( host=CONFIG['redis']['host'], port=CONFIG['redis']['port'], db=0)
def log_client(self): """Log client.""" from server import logger logger.info('Client-IP: ' + self.arg.remote_addr) logger.info('User-Agent: ' + self.arg.headers.get('User-Agent'))
def build_app(db_uri=None): """Sets up the app and installs uvloop.""" app = web.Application( middlewares=[SentryMiddleware(), validate_token_middleware]) uvloop.install() # decide which payment handler to use if stripe_key is None: logger.warn("No stripe key provided! Not charging customers.") payment_manager = DummyPaymentManager else: payment_manager = PaymentManager initialize_firebase() if server_mode == "development": verifier = DummyVerifier() else: verifier = FirebaseVerifier("dragorhast-420") # keep a track of all open bike connections app['payment_manager'] = payment_manager(stripe_key) app['bike_location_manager'] = BikeConnectionManager() app['rental_manager'] = RentalManager(app['payment_manager']) app['reservation_manager'] = ReservationManager( app['bike_location_manager'], app['rental_manager']) app['reservation_sourcer'] = ReservationSourcer(app['reservation_manager']) app['statistics_reporter'] = StatisticsReporter(app['rental_manager'], app['reservation_manager']) app['database_uri'] = db_uri if db_uri is not None else 'spatialite://:memory:' app['token_verifier'] = verifier # set up the background tasks register_signals(app) # register views register_views(app, api_root) app.router.add_get("/", redoc) app.router.add_get("/logo.svg", logo) setup_aiohttp_apispec( app=app, title=name, version=__version__, url=f"{api_root}/schema", servers=[{ "url": "http://api.tap2go.co.uk" }], info={"x-logo": { "url": "/logo.svg", "altText": "tap2go logo" }}, externalDocs={ "description": "Tap2Go Software Docs", "url": "https://tap2go-server.netlify.com/" }, components={ "securitySchemes": { "FirebaseToken": { "type": "http", "description": "A valid firebase token JWT", "scheme": "bearer", "bearerFormat": "JWT", } } }, ) # set up sentry exception tracking if server_mode != "development": logger.info("Starting Sentry Logging") sentry_sdk.init( dsn="https://[email protected]/1296249", environment=server_mode, release=f"server@{__version__}") if server_mode == "development" or server_mode == "testing": asyncio.get_event_loop().set_debug(True) return app