def send_json(): to = '' message = '' if request.method != 'POST': return ErrorMethodNotAllowed('This route serve only POST method') log.debug('Content-Type: {}'.format(request.content_type)) json = request.get_json(force=True, silent=True) if not json: return ErrorBadRequest('Failed to parce JSON input') try: if 'to' in json: to = json['to'] if 'message' in json: message = json['message'] except Exception as e: log.error('{}'.format(e)) return ErrorBadRequest(e) if isBase64(message): message = base64.b64decode(message).decode('utf-8') return send_message(to, message)
def getBasicMatchInfo(match_id): log.debug('getBasicMatchInfo('+str(match_id)+')', 'tmatch') match_info = list(db.query(""" SELECT match_id, date, strftime("%d/%m/%Y", date) as pretty_date, confirmed FROM tmatch WHERE match_id = $match_id """, vars={'match_id':match_id}))[0] match_info['match_scores'] = list(db.query(""" SELECT p.player_id, p.name, ms.won, ms.frames_won, ms.total_points FROM tmatchscore ms, tplayer p WHERE ms.match_id = $match_id AND ms.player_id=p.player_id ORDER BY p.player_id """, vars={'match_id':match_id})) # if match_info['winner_frame_count'] == match_info['loser_frame_count']: # match_info['headline'] = match_info['winner_name']+' beats '+match_info['loser_name']+' on points scored' # else: # match_info['headline'] = match_info['winner_name']+' beats '+match_info['loser_name']+' '+str(match_info['winner_frame_count'])+'-'+str(match_info['loser_frame_count']) match_info['headline'] = '' return match_info
def contact_list(): errormsg = '' if 'error' in session: errormsg = session['error'] session.pop('error') users = [] contacts = client(GetContactsRequest(0)) log.debug('{}'.format(contacts)) for user in contacts.users: user_profile = dict() user_profile['id'] = user.id user_profile['access_hash'] = user.access_hash user_profile['first_name'] = user.first_name user_profile['last_name'] = user.last_name user_profile['phone'] = user.phone if user.photo: filename = 'app/contacts/static/tmp/{}.jpg'.format( user.photo.photo_id) if not Path(filename).is_file(): log.info('Downloading profile photo {}_{} to {}'.format( user.first_name, user.last_name, filename)) client.download_profile_photo(user, file=filename) user_profile['photo'] = '{}.jpg'.format(user.photo.photo_id) users.append(user_profile) output = render_template('contact_list.html', contacts=sorted(users, key=lambda k: k['first_name']), errormsg=errormsg) return output
def send_message(to, message): if not to: return ErrorBadRequest('Missing parameter To') if not message: return ErrorBadRequest('Missing parameter message') log.debug('Sending message to {}: {}'.format(to, message)) msglog.info('Sending message to {}: {}'.format(to, message)) try: client.send_message(to, message) except Exception as e: log.error('{}'.format(e)) return ErrorInternalError('{}'.format(e)) return ErrorGeneralOK('Send succesfull')
def contact_list(): errormsg = '' if 'error' in session: errormsg = session['error'] session.pop('error') users = [] contacts = client.send(functions.contacts.GetContacts(0)) log.debug('{}'.format(contacts)) for user in contacts.users: user_profile = dict() user_profile['id'] = user.id user_profile['access_hash'] = user.access_hash user_profile['first_name'] = user.first_name user_profile['last_name'] = user.last_name user_profile['phone'] = user.phone if user.photo: filename = 'app/contacts/static/tmp/{}.jpg'.format( user.photo.photo_id) if not Path(filename).is_file(): log.info('Downloading profile photo {}_{} to {}'.format( user.first_name, user.last_name, filename)) photos = client.get_user_profile_photos(user.id) # log.debug('photos: {}'.format(photos)) for photo in photos: log.debug('Hello') if photo.width == 640: user_profile['photo'] = client.download_media( photo.file_id, file_name='{}.jpg'.format(user.photo.photo_id)) # user_profile['photo'] = '{}.jpg'.format(user.photo.photo_id) users.append(user_profile) # locale.setlocale(locale.LC_ALL, "") # output = render_template('contact_list.html', contacts=sorted(users, key=lambda k: k['first_name']), errormsg=errormsg) # output = render_template('contact_list.html', contacts=sorted(users, key=lambda k: -int(k['first_name'].replace(",", ""))), errormsg=errormsg) # output = render_template('contact_list.html', contacts=users.sort(key='first_name'), errormsg=errormsg) output = render_template('contact_list.html', contacts=users, errormsg=errormsg) return output
def send(): to = '' message = '' if request.method != 'POST': return ErrorMethodNotAllowed('This route serve only POST method') log.debug('Content-Type: {}'.format(request.content_type)) to = request.args.get('to') if request.content_type == 'application/x-www-form-urlencoded': message = request.get_data(as_text=True) if isBase64(message): message = base64.b64decode(message).decode('utf-8') else: message = request.args.get('message') return send_message(to, message)
def getMatch(match_id): log.debug('getMatch('+str(match_id)+')', 'tmatch') match_info = getBasicMatchInfo(match_id) frames = list(db.query(""" SELECT frame_id FROM tframe WHERE match_id = $match_id """, vars={'match_id':match_id})) frames_info = [] for frame in frames: frames_info.append(tframe.getBasicFrameInfo(frame['frame_id'])) match_info['frames'] = frames_info match_info['stats'] = createMatchStats(match_info) return match_info
def contact_del(): log.debug('{}'.format(request.form)) input_user = InputUser(int(request.form['userid']), 0) try: user = client(GetUsersRequest([input_user])) log.debug('{}'.format(user)) if user[0].photo: filename = 'app/contacts/static/tmp/{}.jpg'.format( user[0].photo.photo_id) if Path(filename).is_file(): log.info('Removing profile photo {}_{} to {}'.format( user[0].first_name, user[0].last_name, filename)) os.remove(filename) result = client(DeleteContactRequest(input_user)) log.debug('{}'.format(result)) session['error'] = 'Контакт {}_{} успешно удален.'.format( result.user.first_name, result.user.last_name) except Exception as e: session['error'] = 'Ошибка удаления контакта {}'.format(e) return redirect(url_for('contacts.contact_list'))
def contact_add(): log.debug('{}'.format(request.form)) first_name = request.form['first_name'] last_name = request.form['last_name'] phone_number = request.form['phone_number'] if request.form['contactChange'] == '1': action = 'изменен' else: action = 'добавлен' log.info('Adding user {}_{} {}'.format(first_name, last_name, phone_number)) input_contact = InputPhoneContact(0, phone_number, first_name, last_name) contact = client(ImportContactsRequest([input_contact])) if contact.users: errormsg = '' log.debug('Contacts found: {}'.format(contact)) for user in contact.users: errormsg += 'Контакт {}_{} ({}) успешно {}'.format( user.first_name, user.last_name, user.phone, action) session['error'] = errormsg else: log.debug('Contacts not found: {}'.format(input_contact)) session['error'] = 'Пользователь {} не найден.'.format(phone_number) return redirect(url_for('contacts.contact_list'))
def ping(): try: user = client.get_me() log.debug('{}'.format(user)) except Exception as e: log.error('{}'.format(e)) log.error('Trying to reconnect telegram') try: client.connect() except Exception as e: log.error('Reconnect failed: {}'.format(e)) return ErrorInternalError('{}'.format(e)) try: user = client.get_me() log.debug('Reconnect successed: {}'.format(user)) client.is_connected() except Exception as e: return ErrorInternalError('{}'.format(e)) return ErrorGeneralOK('{}'.format('Pong'))
from flask import Flask from app.utils import log from app.utils import IAM log.debug('Got IAM: {}'.format(IAM().get())) app = Flask(__name__)
def contact_del(): log.debug('{}'.format(request.form))