async def signup(user: UserSignup): try: if MongoDB.user_lookup(user.email) is not True: new_user = MongoDB.signup(user.username, user.password, user.email) token = jwt.encode( { 'user': new_user, 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1) }, SECRET_KEY, 'HS512') return jsonable_encoder({'token': token.decode('UTF-8')}) else: return Response(json.dumps({"message": "User already Exists"}), 409) except KeyError: return Response(json.dumps({"message": "missing fields"}), 409)
class ArticleService: mysql = MySQL() mongodb = MongoDB() category_service = CategoryService() def get_articles_by_current_user_id(self, current_user_id: int): categories = self.category_service.get_category_by_current_user_id( user_id=current_user_id) category_names = [i.name for i in categories] valid_session_id = self.mysql.get_valid_session_id() number_of_articles_per_category = get_number_of_articles_per_category( Config.NUMBER_OF_ARTICLES, len(categories)) article_scores = [] for i in range(len(categories)): articles = self.mysql.fetch_articles_ranking( session_id=valid_session_id, category=category_names[i], limit=number_of_articles_per_category[i]) article_scores.extend(articles) result = [] for i in article_scores: article_mongo = self.mongodb.get_article_by_uuid(i.article_id) article_show = ArticleShow(id=article_mongo.id, url=article_mongo.url, domain=article_mongo.domain, title=article_mongo.title, category=article_mongo.category, time=article_mongo.time, content=article_mongo.content, audio_path=i.audio_path) result.append(article_show) return result def get_articles_no_login(self): category_names = Config.CATEGORIES valid_session_id = self.mysql.get_valid_session_id() number_of_articles_per_category = get_number_of_articles_per_category( Config.NUMBER_OF_ARTICLES, len(category_names)) article_scores = [] for i in range(len(category_names)): articles = self.mysql.fetch_articles_ranking( session_id=valid_session_id, category=category_names[i], limit=number_of_articles_per_category[i]) article_scores.extend(articles) result = [] for i in article_scores: article_mongo = self.mongodb.get_article_by_uuid(i.article_id) article_show = ArticleShow(id=article_mongo.id, url=article_mongo.url, domain=article_mongo.domain, title=article_mongo.title, category=article_mongo.category, time=article_mongo.time, content=article_mongo.content, audio_path=i.audio_path) result.append(article_show) return result
def getBookCollections(self): results = self.service.GetBookCollections(self.api.url) if results: collections, count, user = results else: return False if not user or not collections: return False collection_datas = [] for collection in collections: collection_data = collection.__dict__.get('_data') for key, value in collection_data.items(): if value is None: collection_data.pop(key) collection_data['author'] = DBRef('user', user.id) if collection.book: #添加该书到book_tag集合 db = MongoDB.getConnection('mining') for tag in collection.tags: db.book_tag.update({'title':tag}, {'$addToSet':{'books':collection.book.bid}, '$inc':{'count':1}}, upsert=True) collection_data['book'] = DBRef('book', collection.book.id) collection_datas.append(collection_data) if collection_datas: ids = self.db.book_collection.insert(collection_datas, check_keys=False) for index, object_id in enumerate(ids): collections[index].id = object_id try: user_book_related = None user_book_related, created = UserBookRelated.objects.get_or_create(\ uid=self.api.api_id, auto_save=False, defaults=\ {'uid':self.api.api_id, 'book_collections':collections}) if not created: user_book_related.book_collections.extend(collections) try: user_book_related.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s when save %s at api:%s" %(error, \ error.to_dict(), user_book_related, self.api.url)) except Exception, error: logger.debug("[ERROR]%s: UserBookRelated.objects.get_or_create(uid=%s ..."\ %(error, self.api.api_id)) if user_book_related and created: try: user.user_book_related = user_book_related user.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s when save %s at api:%s" \ %(error, error.to_dict(), user, self.api.url))
async def login(credentials: HTTPBasicCredentials = Depends(security)): username = credentials.username password = credentials.password check_user = MongoDB.login(username, password) if check_user["authenticated"]: token = jwt.encode( { 'user': check_user, 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1) }, SECRET_KEY, algorithm='HS512') return jsonable_encoder({'token': token.decode('UTF-8')}) else: return Response(json.dumps({"message": 'login failed'}), 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
def getBookTags(self): results = self.service.GetBookTags(self.api.url) if results: book_tags, count = results else: return False book = None books = Book.objects(bid=self.api.api_id) if books: book = books[0] else: return False if not book_tags: return False else: db = MongoDB.getConnection('mining') for tag in book_tags: db.book_tag.update({'title':tag.title}, {'$addToSet':{'books':book.bid}, '$inc':{'count':tag.count}}, upsert=True) try: book_related = None book_related, created = BookRelated.objects.get_or_create(\ bid=book.bid, auto_save=False, defaults=\ {'bid':book.bid, 'book':book, 'tags':book_tags}) if not created: book_related.tags.extend(book_tags) try: book_related.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s when save %s at api:%s" %(error, \ error.to_dict(), book_related, self.api.url)) except Exception, error: logger.debug("[ERROR]%s: BookRelated.objects.get_or_create(uid=%s ..."\ %(error, self.api.api_id))
def __init__(self, api, key='book'): self.api = api self.api_operation = ApiOperation(key, api) self.service = self.api_operation.getService() self.db = MongoDB.getConnection()
def __init__(self, api_type): self.thread_stop = False self.sleep_count = 0 self.db = MongoDB.getConnection() self.api_type = api_type
def __init__(self, uid): threading.Thread.__init__(self) self.uid = uid self.mining_db = MongoDB.getConnection('mining') self.recommend_db = MongoDB.getConnection('recommend')
class ScoreService: mongo = MongoDB() mysql = MySQL() keyword = Config.SCORE_KEYWORD lsh = LSH() def check_contains_keyword(self, text: str, key_list: List[str]): for i in key_list: if i in text: return True return False def score_by_category(self, category: str): print( '\n------------------------------------------------------------------------' ) print('category: ', category) articles_by_category = self.mongo.get_articles_by_category( category=category) print('size: ', len(articles_by_category)) article_contents = [ article.content for article in articles_by_category ] matrix = self.lsh.init_matrix(list_docs=article_contents) min_hash = self.lsh.min_hashing(matrix=matrix, n_permutation=100) results = [] invalid_len_articles = [] for article in articles_by_category: print('article: ', article) score = 0 if len(article.content) < 500 or len(article.content) > 5000: score = -99999 invalid_len_articles.append(article.id) else: time_second = (datetime.datetime.now() - article.time).total_seconds() time_second_max = (datetime.datetime.now() - datetime.datetime.now().replace( day=datetime.datetime.now().day - 1, hour=17, minute=0, second=0)).total_seconds() time_score = (time_second_max - time_second) / time_second_max * 50 score = score + time_score if self.check_contains_keyword(article.title, self.keyword[category]): # print(article.title) score = score + 10 index = self.lsh.get_index_of_doc(article.content, article_contents) for i in range(len(articles_by_category)): if i != index: sim = self.lsh.jaccard_signature( min_hash[:, index], min_hash[:, i]) # sim = self.lsh.jaccard_signature(matrix[:, index], matrix[:, i]) if sim > Config.PROBABILITY_MIN_HASHING: if articles_by_category[ index].time < articles_by_category[i].time: score = score + 20 elif articles_by_category[index].time > articles_by_category[i].time and \ articles_by_category[i].id in invalid_len_articles: score = score + 20 else: score = score - 99999 print('------------------------') print('sim = ', sim) print(articles_by_category[index]) print(articles_by_category[i]) print('------------------------') score_insert = ScoreInsert(article_id=article.id, url=article.url, category=article.category, domain=article.domain, score=score) results.append(score_insert) return results def check_audio(self, audio: Audio): if audio.result == "Success": print("create gtts successfully with uuid: ", audio.uuid) path = Config.BASE_AUDIO_DIR + audio.uuid + ".mp3" self.mysql.add_audio_path(audio.uuid, path) else: print("create gtts failed with uuid: ", audio.uuid)