def get_friends_by_person(username=Depends(auth_wrapper)): try: with SQLITE_DB.atomic(): person = PersonORM.select().where( PersonORM.username == username).get() friends_list = [] query = (PersonORM.select().join( FriendsORM, on=FriendsORM.to_user).where(FriendsORM.from_user == person)) for friend in query: friend_dict = { "first_name": friend.first_name, "last_name": friend.last_name, "birthday": friend.birthday, "username": friend.username, "user_id": friend.user_id } friends_list.append(friend_dict) return friends_list except Exception as error: raise error
def create(self, person): create_person = Person() create_person.name = person['name'] create_person.state_short = person['state_short'] create_person.city = person['city'] self.database.create(create_person) self.rewrite_list()
def submit_person(): session = Session() person = None if 'id' in request.form: person = session.query(Person).filter(Person.id == request.form['id']).first() else: person = Person() person.name = request.form['name'] if 'location' in request.form: person.location = request.form['location'] if 'image_id' in request.form and request.form['image_id']: person.image_id = request.form['image_id'] elif('cover' in request.files) and request.files['cover'] and request.files['cover'].filename: upload = request.files['cover'] file = File() file.name = secure_filename(upload.filename) file.type = 'image' file.mime = guess_type(file.name)[0] file.data = upload.read() person.image = file session.add(person) session.commit() return redirect(url_for('index'))
def database_signup_handler(response, user, password, bio, base64blob): p = Person.get_user_by_username(user) if p == None: Person.create_user(password, user, bio, base64blob) elif p.name == user: response.redirect('/signup') else: Person.create_user(password, user, bio, base64blob)
def create_test_data(self): create_person = Person() create_person.name = "testovací jméno" create_person.state_short = "testovací stát" create_person.city = "testovací město" self.database.create(create_person) self.rewrite_list()
def process_newest_weibo(): """ 设置定时器: 每隔一段时间调用自身 获取新微博: 先爬取 person 的最新一条微博, 若其已存在与数据库中, 则等待下次执行. 若不存在, 则分析其情绪值, 并存入数据库中, 同时更新数据库中的统计数据 分析情绪值: 若情绪正常, 则等待, 否则, 发送短信 """ # 获取新微博 new_weibo = spider.get_newest_weibo() query = Weibo.select().where( Weibo.weibo_content == new_weibo["weibo_content"]) print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # 对新发出的微博进行处理 if not query.exists(): print('======发现新微博======') analyser = Analyser(user_id) sentiment_value = analyser.get_sentiment_value( new_weibo["weibo_content"]) # 数据库操作 db = DB() db.person = person weibo = db.store_weibo(**new_weibo) db.store_sentiment(weibo, sentiment_value) analyser.calculate() # 更新数据库后, 均值和方差变了, 需要再次更新 MEAN = Person.get(Person.user_id == user_id).mean SD = Person.get(Person.user_id == user_id).std if sentiment_value not in range(MEAN - SD // 2, MEAN + SD // 2): message = '[{person_name}]发了一条[情绪值]为 {sentiment_value} 的微博, 内容为: {content}'.format( person_name=person.username, sentiment_value=sentiment_value, content=new_weibo['weibo_content'], ) print(message) print('============') # 发送短信 send_sms(message) else: # 更新点赞数 转发数 和 评论数 weibo = query[0] if (weibo.up_num != new_weibo['up_num'] or weibo.retweet_num != new_weibo['retweet_num'] or weibo.comment_num != new_weibo['comment_num']): print('微博状态发生变化, 正在更新数据库...') db = DB() db.person = person weibo.up_num = new_weibo['up_num'] weibo.retweet_num = new_weibo['retweet_num'] weibo.comment_num = new_weibo['comment_num'] weibo.save() print('数据库更新完毕') print('======无新微博, 当前输出的是最后一条微博======')
def make_transfer(transfer_schema: TransferSchema, username=Depends(auth_wrapper)): try: with SQLITE_DB.atomic(): person = Person.select().where(Person.username == username).get() friend = Person.select().where(Person.user_id == transfer_schema.friend_id).get() card = Card.select().where(Card.card_id == transfer_schema.billing_card.card_id).get() BankStatement.create( user_id=person, friend_id=friend, value=transfer_schema.total_to_transfer, date=datetime.today().date(), from_card=card ) except Exception as error: raise error
def database_login_handler(username, password, response): person = Person.get_user_by_username(username) if person: if person.password == password: return True else: response.redirect('/login') else: response.redirect('/login')
def profile_handler(response, user): profile_picture = '/static/test.png' person = Person.get_user_by_username(user) if not person: response.write('No user found with that name') return var_dict = {'person': person} rendered = render_file(os.path.join('pages', 'profile.html'), var_dict) response.write(rendered)
def all_bank_statement(username=Depends(auth_wrapper)): try: with SQLITE_DB.atomic(): bs_list = [] query = BankStatementORM.select() for bs in query: person = PersonORM.select().where( PersonORM.id == bs.user_id).get() friend = PersonORM.select().where( PersonORM.id == bs.friend_id).get() card = CardORM.select().where(CardORM.id == bs.from_card).get() bs_dict = dict(user_id=person.user_id, friend_id=friend.user_id, value=bs.value, date=bs.date, from_card=card.card_id) bs_list.append(bs_dict) return bs_list except Exception as error: raise error
def write_to_db(person_name_input, favorite_color_input, favorite_animal_input): # Writes data to database. Returns message passed from database. try: with db.transaction(): ''' Attempt to add the vote. If person_name is taken, the database will raise an IntegrityError ''' Person.create(person_name=person_name_input, favorite_color=favorite_color_input, favorite_animal=favorite_animal_input) return "Thank you for voting!" except db_exceptions['IntegrityError']: return "You have already voted!" except Exception as e: app.logger.error('Exception in write_to_db: {}'.format(str(e))) return "Please try again later."
def bank_statement_by_userid(userid: str, username=Depends(auth_wrapper)): try: with SQLITE_DB.atomic(): person = PersonORM.select().where( PersonORM.user_id == userid).get() query = BankStatementORM.select().where( BankStatementORM.user_id == person.id) bs_person_list = [] for bs in query: friend = PersonORM.select().where( PersonORM.id == bs.friend_id).get() card = CardORM.select().where(CardORM.id == bs.from_card).get() bs_dict = dict(user_id=person.user_id, friend_id=friend.user_id, value=bs.value, date=bs.date, from_card=card.card_id) bs_person_list.append(bs_dict) return bs_person_list except Exception as error: pass
def do_POST(self): global store print "POST %s" % self.path bits = urllib.unquote(self.path).split('/') x = None if bits[1] == "people": clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST people: %s" % j x = Person.of_dict(j) elif bits[1] == 'service': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST service: %s" % j x = Service.of_dict(j) elif bits[1] == 'att': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) mime, pdict = cgi.parse_header(self.headers.getheader('content-type')) c = self.rfile.read(int(clen)) print "POST att: %s" % bits[1] x = Att.insert(unicode(bits[2]), c, unicode(mime)) elif bits[1] == 'thing': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST thing: %s" % j x = Thing.of_dict(j) elif bits[1] == 'credential': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST credential: %s" % j x = Credential.of_dict(j) try: store.commit() except: store = get_store() store.commit() if x: self.send_response(200) else: self.send_response(500) self.end_headers()
def get_token(login: AuthSchema): username, password = login.username, login.password try: with SQLITE_DB.atomic(): query = Person.select().where(Person.username == username) for person in query: if bcrypt.checkpw(bytes(password, "UTF-8"), bytes(person.password, "UTF-8")): jwt_token = jwt_encode(user=username) return dict(access_token=jwt_token, token_type="bearer") except Exception: raise HTTPException(status_code=401, detail="Problem when logging in")
def add_card_to(card_schema: CardSchema, username=Depends(auth_wrapper)): try: with SQLITE_DB.atomic(): query = PersonORM.select().where(PersonORM.username == username) for person in query: CardORM.create(card_id=card_schema.card_id, title=card_schema.title, PAN=card_schema.PAN, expiry_mm=card_schema.expiry_mm, expiry_yyyy=card_schema.expiry_yyyy, security_code=card_schema.security_code, date=card_schema.date, owner=person) except Exception as error: raise error return dict(message="Card successfully registered")
def add_data(name): """add data from html to database""" # use cached data if possible, otherwise go to wikiquote try: html = open("raw/" + name + ".txt", 'r').read() print("loaded", name, 'from disk') except: print("retreiving", name) html = get_person_page(name) if not html: return None with open("raw/" + name + ".txt", 'w') as f: f.write(html) sleep(2.5) return None try: quotes = parse_html(html) except: print("exception parsing", name) quotes = None sources = {} if quotes: for q in quotes: if q.source is None: q.source = "*None*" if q.source in sources: sources[q.source].append(q.quote) else: sources[q.source] = [q.quote] session = Session() person = Person(name=name) for s in sources: if s is not '*None*': source = Source(source=s) person.sources.append(source) for q in sources[s]: quote = Quote(quote=q) person.quotes.append(quote) if s is not '*None*': source.quotes.append(quote) session.add(person) session.commit() session.close()
def get_all_cards_by_person(username=Depends(auth_wrapper)): try: with SQLITE_DB.atomic(): person = PersonORM.select().where( PersonORM.username == username).get() query = (CardORM.select().where(CardORM.owner == person)) cards = [] for card_row in query: card_dict = { "card_id": card_row.card_id, "title": card_row.title, "PAN": card_row.PAN, "expiry_mm": card_row.expiry_mm, "expiry_yyyy": card_row.expiry_yyyy, "security_code": card_row.security_code, "date": card_row.date } cards.append(card_dict) return cards except Exception as error: raise error
def do_GET(self): bits = urllib.unquote(self.path).split('/') try: x = None if bits[1] == "ping": self.send_response(200) self.end_headers() self.wfile.write("pong") elif bits[1] == "people": self.output_json(Person.retrieve(bits[2])) elif bits[1] == "service": self.output_json(Service.retrieve(bits[2],bits[3])) elif bits[1] == "thing": self.output_json(Thing.retrieve(bits[2])) elif bits[1] == "att": x = Att.retrieve(bits[2]) if x: self.send_response(200) self.send_header('Content-type', x.mime) self.send_header('Content-length', x.size) self.end_headers() self.wfile.write(x.body) else: self.send_response(404) self.end_headers() self.wfile.write('404 Not Found') elif bits[1] == "credential": self.output_json(Credential.retrieve(bits[2])) except IndexError: print "GET error! self:%s\n%s" % (self.path, self.headers)
# from flask import Flask # app = Flask(__name__) # @app.route("/") # def hello(): # return "Hello World!" from db import Person from playhouse.shortcuts import model_to_dict, dict_to_model user_obj = Person.select() json_data = json.dumps(model_to_dict(user_obj))
def main(): """ user_id: 用户ID, 查询方法看weiboSpider里的README filter: 值为1时,爬取所有原创微博 my_cookie: 设置cookie值 """ user_id = int(os.environ.get('USER_ID')) or 00000000 filter = 1 my_cookie = os.environ.get('COOKIE') or "your_cookie" # noqa: ignore=E501 # 爬取数据 if not os.path.isfile('weibo.db'): spider = Spider(user_id, filter) spider.set_cookie(my_cookie) spider.start() # 分析数据 person = Person.get(Person.user_id == user_id) if not person.mean: analyser = Analyser(user_id) analyser.start() # 初始化 spider = Spider(user_id, filter) spider.set_cookie(my_cookie) # 处理新微博 def process_newest_weibo(): """ 设置定时器: 每隔一段时间调用自身 获取新微博: 先爬取 person 的最新一条微博, 若其已存在与数据库中, 则等待下次执行. 若不存在, 则分析其情绪值, 并存入数据库中, 同时更新数据库中的统计数据 分析情绪值: 若情绪正常, 则等待, 否则, 发送短信 """ # 获取新微博 new_weibo = spider.get_newest_weibo() query = Weibo.select().where( Weibo.weibo_content == new_weibo["weibo_content"]) print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # 对新发出的微博进行处理 if not query.exists(): print('======发现新微博======') analyser = Analyser(user_id) sentiment_value = analyser.get_sentiment_value( new_weibo["weibo_content"]) # 数据库操作 db = DB() db.person = person weibo = db.store_weibo(**new_weibo) db.store_sentiment(weibo, sentiment_value) analyser.calculate() # 更新数据库后, 均值和方差变了, 需要再次更新 MEAN = Person.get(Person.user_id == user_id).mean SD = Person.get(Person.user_id == user_id).std if sentiment_value not in range(MEAN - SD // 2, MEAN + SD // 2): message = '[{person_name}]发了一条[情绪值]为 {sentiment_value} 的微博, 内容为: {content}'.format( person_name=person.username, sentiment_value=sentiment_value, content=new_weibo['weibo_content'], ) print(message) print('============') # 发送短信 send_sms(message) else: # 更新点赞数 转发数 和 评论数 weibo = query[0] if (weibo.up_num != new_weibo['up_num'] or weibo.retweet_num != new_weibo['retweet_num'] or weibo.comment_num != new_weibo['comment_num']): print('微博状态发生变化, 正在更新数据库...') db = DB() db.person = person weibo.up_num = new_weibo['up_num'] weibo.retweet_num = new_weibo['retweet_num'] weibo.comment_num = new_weibo['comment_num'] weibo.save() print('数据库更新完毕') print('======无新微博, 当前输出的是最后一条微博======') # 循环爬取最新微博, 并处理 while True: try: process_newest_weibo() except: pass finally: time.sleep(300)
def post(self): parsed_args = self.request_parser.parse_args() person = Person() person.type = parsed_args['type'] person.nameLast = parsed_args['nameLast'] person.nameFirst = parsed_args['nameFirst'] person.nameMiddle = parsed_args['nameMiddle'] person.nameExt = parsed_args['nameExt'] person.contactTel = parsed_args['contactTel'] person.contactMobile = parsed_args['contactMobile'] person.contactEmail = parsed_args['contactEmail'] person.updatedAt = time_provider.getTime() person.updatedBy = api_user() db.session.add(person) db.session.commit() return person, 201