class Mg: def __init__(self): self.con = MongoClient("mongodb://localhost:27017/")["book_metadata"][ "metadata"] #["yunyi_mgtest"]["bookinf"] #mg=Mg().con def get_all_info(self, param): a = self.con.find({"asin": param}) ls = [] for i in a: ls.append(str(i)) return ls def get_total(self): a = self.con.find().count() print(str(a)) def get_all_books(self): a = self.con.find() ls = [] for i in a: ls.append(i) return ls def get_all(self): a = self.con.distinct("asin") ls = [] for i in a: ls.append(str(i)) return ls def get_category(self, param): a = self.con.find({"category": param}) ls = [] for i in a: ls.append(str(i)) return ls def get_sorted_title(self): pass
class Mg: def __init__(self): self.con = MongoClient( "mongodb://localhost:27017/")["book_metadata"]["metadata"] self.log = MongoClient("mongodb://localhost:27017/")["book_log"]["log"] def get_bestsellers(self): a = self.con.find({"salesRank": {'$exists': 1}}) #,{"asin":1,"salesRank":1} ls = [] for i in a: ls.append(i) return ls def get_all_info(self, param): a = self.con.find({"asin": param}) ls = [i for i in a] return ls def search_book(self, keyword): query = { '$or': [{ 'title': { "$regex": keyword, "$options": "i" } }, { 'author': { "$regex": keyword, "$options": "i" } }, { 'brand': { "$regex": keyword, "$options": "i" } }, { 'asin': { "$regex": keyword, "$options": "i" } }, { 'categories': { '$elemMatch': { '$elemMatch': { "$regex": keyword, "$options": "i" } } } }] } cursor = self.con.find(query) results = [book for book in cursor] return results def add_book(self, asin, title=None, price=None, imUrl=None, category=[], salesRank={}, brand=None, also_bought=[], also_viewed=[], buy_after_viewing=[], bought_together=[]): # asin, title, brand, imUrl: string # salesRank={category: integer} # category: list of list cursor = self.con.find({'asin': asin}) if cursor.count() > 0: raise Exception('Book with given asin already exists.') else: toInsert = { 'asin': asin, 'title': title, 'price': price, 'imUrl': imUrl, 'related': { 'also_bought': also_bought, 'also_viewed': also_viewed, 'buy_after_viewing': buy_after_viewing, 'bought_together': bought_together }, 'categories': category, 'salesRank': salesRank, 'brand': brand } x = self.con.insert_one(toInsert) def get_all_books(self, skip, category): if (category == "all"): param = {} else: param = { 'categories': { '$elemMatch': { '$elemMatch': { '$in': [category] } } } } a = self.con.find(param).limit(100).skip(100 * (skip - 1)) ls = [i for i in a] return ls def get_all(self): a = self.con.distinct("asin") ls = [i for i in a] return ls def get_category(self, param): p = {'categories': {'$elemMatch': {'$elemMatch': {'$in': [param]}}}} a = self.con.find(p) ls = [i for i in a] return ls def get_sorted_title(self): pass def insert_query(self, query): self.log.insert_one(query) def get_highest_rank_books(self, category): categories = [ "Mystery, Thriller & Suspense", "Science Fiction & Fantasy", "Action & Adventure", "Love & Romance", "Business & Money", "Health, Fitness & Dieting", "Professional & Technical", "Administration & Policy", "Dictionaries & Thesauruses", "Biographies & Memoirs" ] if category not in categories: raise Exception("No such category") else: temp = 'salesRank.' + category a = self.con.find({temp: {'$exists': True}}).limit(10) ls = [i for i in a] ls.insert(0, category) return ls
class Mg: def __init__(self): mongo_addr = "mongodb://*****:*****@" + MONGO_IP + "/book_log" self.con = MongoClient(mongo_addr)["book_metadata"]["metadata"] self.log = MongoClient(mongo_addr)["book_log"]["log"] # self.con = MongoClient("mongodb://*****:*****@3.234.153.108/book_log")["book_metadata"]["metadata"] # self.log = MongoClient("mongodb://*****:*****@3.234.153.108/book_log")["book_log"]["log"] # self.con = MongoClient("mongodb://localhost:27017/")["book_metadata"]["metadata"] # self.log = MongoClient("mongodb://localhost:27017/")["book_log"]["log"] def mongo_to_df(self, query={}): a = self.log.find(query) df = pd.DataFrame(list(a)) #print(df.columns) df = df.drop( ['userid', 'query_type', 'query', 'response', 'user_type'], axis=1) df["date"] = df.apply(lambda row: time.strftime( '%Y-%m-%d', time.localtime(row.time_stamp)), axis=1) re = pd.DataFrame({ "cnt": df.groupby(['date']).size() }).reset_index().to_numpy() return re[:, 0], re[:, 1] def get_highest_viewed_books(self, k=5): # For admin all_query = list(self.log.find({}, {'query': 1})) asins = [ d['query'].split('/')[-1] for d in all_query if d['query'].split('/')[-1].startswith('B') ] most_common_asins = [item for item, c in Counter(asins).most_common(k)] books = [next(self.con.find({"asin": o})) for o in most_common_asins] return books def get_highest_viewed_books_by_user(self, userid, k=5): # For user all_query = list(self.log.find({'user_id': userid}, {'query': 1})) asins = [ d['query'].split('/')[-1] for d in all_query if d['query'].split('/')[-1].startswith('B') ] most_common_asins = [item for item, c in Counter(asins).most_common(k)] books = [next(self.con.find({"asin": o})) for o in most_common_asins] return books def get_bestsellers(self): a = self.con.find({"salesRank": {'$exists': 1}}) #,{"asin":1,"salesRank":1} ls = [] for i in a: ls.append(i) return ls def get_all_info(self, param): a = self.con.find({"asin": param}) ls = [i for i in a] return ls def search_book(self, keyword): query = { '$or': [{ 'title': { "$regex": keyword, "$options": "i" } }, { 'author': { "$regex": keyword, "$options": "i" } }, { 'brand': { "$regex": keyword, "$options": "i" } }, { 'asin': { "$regex": keyword, "$options": "i" } }, { 'categories': { '$elemMatch': { '$elemMatch': { "$regex": keyword, "$options": "i" } } } }] } cursor = self.con.find(query) results = [book for book in cursor] return results def add_book(self, asin, title=None, price=None, imUrl=None, category=[], salesRank={}, brand=None, also_bought=[], also_viewed=[], buy_after_viewing=[], bought_together=[]): # asin, title, brand, imUrl: string # salesRank={category: integer} # category: list of list cursor = self.con.find({'asin': asin}) if cursor.count() > 0: raise Exception('Book with given asin already exists.') else: toInsert = { 'asin': asin, 'title': title, 'price': price, 'imUrl': imUrl, 'related': { 'also_bought': also_bought, 'also_viewed': also_viewed, 'buy_after_viewing': buy_after_viewing, 'bought_together': bought_together }, 'categories': category, 'salesRank': salesRank, 'brand': brand } x = self.con.insert_one(toInsert) def get_all_books(self, skip, category): if (category == "all"): param = {} else: param = { 'categories': { '$elemMatch': { '$elemMatch': { '$in': [category] } } } } a = self.con.find(param).limit(100).skip(100 * (skip - 1)) ls = [i for i in a] return ls def get_all(self): a = self.con.distinct("asin") ls = [i for i in a] return ls def get_category(self, param): p = {'categories': {'$elemMatch': {'$elemMatch': {'$in': [param]}}}} a = self.con.find(p) ls = [i for i in a] return ls def insert_query(self, query): self.log.insert_one(query) def get_related_books(self, asin, feature): a = self.con.find({"asin": asin}) try: ls = [i["related"][feature] for i in a] except KeyError: ls = [] return ls z = self.con.find({'asin': {'$in': ls[0]}}) l = [i for i in z] return l def get_book_log(self): self.log.find({}) def get_highest_rank_books(self, category): categories = [ "Mystery, Thriller & Suspense", "Science Fiction & Fantasy", "Action & Adventure", "Love & Romance", "Business & Money", "Health, Fitness & Dieting", "Professional & Technical", "Administration & Policy", "Dictionaries & Thesauruses", "Biographies & Memoirs" ] if category not in categories: raise Exception("No such category") else: temp = 'salesRank.' + category a = self.con.find({temp: {'$exists': True}}).limit(10) ls = [i for i in a] ls.insert(0, category) return ls
import requests from pymongo import MongoClient from multiprocessing import Pool db = MongoClient()["just"]["transactions"] db1 = MongoClient()["just"]["ref"] address_all = db.distinct('raw_data.contract.parameter.value.owner_address') s = requests.Session() url = "https://api.trongrid.io/wallet/triggersmartcontract" def runPool(f, arr): with Pool() as pool: pool.map(f, arr) def go(address): refS = 0 for i in range(6): ## 0-5级邀请 parameter = address.rjust(64, '0') + str(i).rjust(64, '0') data = { "contract_address": "41e3cf5eefe3a2abf35a344ae8a3b2f4bb29810cbd", "owner_address": "410e5034e571bea5eced424290afa5835a282cb7c4", "function_selector": "referralDataOf(address,uint256)", "parameter": parameter, "call_value": 0, "fee_limit": 1000000000, } z1 = s.post(url, json=data)
db = "dataViz" collection = "amendements-2" client = MongoClient(mongo_url, retryWrites=False)[db][collection] # nombre d'amendement par sort, etat et sous etat # res = client.aggregate([ # { # '$group': { # '_id': { # 'sort': '$sort', # 'etat': '$etat', # 'sousEtat': '$sousEtat' # }, # 'count': { # '$sum': 1 # } # } # }, { # '$project': { # '_id': 0, # 'sort': '$_id.sort', # 'etat': '$_id.etat', # 'sousEtat': '$_id.sousEtat', # 'count': 1 # } # } # ]) res = client.distinct("texteLegislatifRef") print(list(res))
#!/usr/bin/python import fetch import sys from pymongo import MongoClient db = MongoClient().ppa.ticket for loc in db.distinct('location'): print loc sys.exit(1) ''' Getting the count for each date for date in sorted(db.distinct('issueDate')): print "%s: %s" % (date, db.find({'issueDate':date}).count()) ''' min_id = db.find().sort([('_id', 1)]).limit(1)[0]['_id'] - 1 max_id = db.find().sort([('_id', -1)]).limit(1)[0]['_id'] + 1 i = (min_id % 1000) % 200 block_start = min_id - i + 1 _total = 0 _resolved = 0 _unresolved = 0 _missing = 0 while block_start < max_id : block_end = block_start + 199 total = db.find({'_id':{'$gte':block_start, '$lte':block_end}}).count()