def find_name(word, find: int = 0): """0 - name, 1 - author""" data_base = create_connection(DB) books_info = read_query(data_base, read_books) book = None books = [] if find < 0 or find > 1: find = 0 counter = 0 for i in range(len(books_info)): result = re.match(r'' + word.lower(), books_info[i][find].lower()) if result is not None: counter += 1 if find == 0: book = Book(books_info[i][0], books_info[i][1], books_info[i][2], books_info[i][3], books_info[i][4], books_info[i][5]) if find == 1: book = Book(books_info[i][0], books_info[i][1], books_info[i][2], books_info[i][3], books_info[i][4], books_info[i][5]) books.append(book) return books
def convertDictIntoObject(jsonDict, objectType): """Convert dict json format into its corresponding object type. Args: jsonDict (dict): json object to be converted. objectType (object): desired object type (Member, Book, or Borrower). Returns: object: the result of the conversion. """ # set returned object's data from json data: if objectType == type(Member()): objectReturned = Member(jsonDict["id"], jsonDict["name"], jsonDict["address"]) elif objectType == type(Book()): objectReturned = Book(jsonDict["name"], jsonDict["isbn"], jsonDict["author"], jsonDict["publication_date"], jsonDict["publisher"], jsonDict["pages_number"], jsonDict["cover_type"]) elif objectType == type(Borrower()): objectReturned = Borrower(jsonDict["id"], jsonDict["name"], jsonDict["address"], jsonDict["isbn"], jsonDict["borrow_date"], jsonDict["return_date"]) else: print("Invalid Object!\n") return objectReturned
def MenuMember(): while True: Book.clrscreen() print("\t\t\t Member Record Management\n") print("==============================================================") print("1. Add Member Record ") print("2. Display Member Records ") print("3. Search Member Record ") print("4. Delete Member Record ") print("5. Update Book Record ") print("6. Return to Main Menu ") print( "===============================================================") choice = int(input("Enter Choice between 1 to 5-------> : ")) if choice == 1: Member.insertMember() #insertData elif choice == 2: Member.display() elif choice == 3: Member.SearchMember() elif choice == 4: Member.deleteMember() elif choice == 5: print("No such Function") elif choice == 6: return else: print("Wrong Choice......Enter Your Choice again") x = input("Enter any key to continue")
def hello_world(): # shelf=Bookshelf() book1 = Book.Book('To Kill A Mockingbird', ['Harper Lee'], [ 'Novel', 'Bildungsroman', 'Southern Gothic', 'Thriller', 'Domestic Fiction', 'Legal Story' ], '978-0446310789', 'B0000000001') book2 = Book.Book('The Talisman', ['Stephen King', 'Peter Straub'], ['Fantasy', 'Thriller'], '978-1451697216', 'B0000000002') book3 = Book.Book('The Bad President', ['Donald Trump'], ['Impeachment', 'Delusional'], '978-1sdfasdfasdff', 'B0000000003') shelf = bookshelf.Bookshelf() #Adds a Book into the shelf shelf.addBook(book1) shelf.addBook(book2) shelf.addBook(book3) # print(shelf.index[book.getISBN()]) books = shelf.index #Prints every book inside index. for isbn in books: print(isbn) print(books[isbn].getAuthor()) #print(book.getISBN()) print("Hello terminal test") return render_template('index.html', books=books)
def captureFullData(itemType, objectData): """Capture data from ALL the user input fields. Args: itemType (class type): type of the item. objectData (): user input handler. Returns: object: the captured object. """ if itemType == type(Member()): newItem = Member(objectData[0].get(), objectData[1].get(), objectData[2].get()) elif itemType == type(Book()): newItem = Book(objectData[0].get(), objectData[1].get(), objectData[2].get(), objectData[3].get(), objectData[4].get(), objectData[5].get(), objectData[6].get()) elif itemType == type(Borrower()): newItem = Borrower(objectData[0].get(), objectData[1].get(), objectData[2].get(), objectData[3].get(), objectData[4].get(), objectData[5].get()) return newItem
def add(): fileId = request.query.get('fileId') visible = request.query.get('secret') if ("on" == visible): visible = False else: visible = True dirName, fileName = File.Get(fileId) Book.AddLast( \ os.path.join(dirName,fileName), \ request.query.user, \ request.query.comment, \ visible, \ videoInfo.GetDuration(os.path.join(dirName,fileName)), \ request.query.audioIndex, \ False) if ("on" == request.query.get('pause')): Book.AddLast( \ u"空予約", \ request.query.user, \ "", \ True, \ "0", \ 1, \ True) redirect( \ "/" + request.query.back + \ "?user="******"&keyword=" + request.query.keyword + \ "&page=" + request.query.page)
def modify(self,x,y,z): t=int(input("Enter the ISBN number of the book you want to issue:")) with open(x,"rb") as f: while(1): try: obj=pickle.load(f) for i in obj: if i.isbn == t: p=int(input("Enter the number of copies:" )) if(i.cp>=p): i.cp=i.cp-p b=Book() b.Booki(z,y,p,t) else: print("Dont have enough copies!") else: print("Invalid ISBN There is no book") except EOFError: break with open(x,"wb") as f: pickle.dump(obj,f)
def deleteItem(itemType, objectData, masterFrame, messageRow): """Delete specific item. Args: itemType (class type): type of the item. objectData (): user input handler. masterFrame (): the frame that the message will be displayed on it. messageRow (int): the place to display the output message. Returns: object: the deleted object. """ # check JSON file existence: data = checkFileExistence() # capture item's data: searchKeyword, isName = captureData(itemType, objectData) # search item's existence: if itemType == type(Member()): itemType = "members" convertTo = type(Member()) itemToBeDeleted = searchPerson(searchKeyword, itemType, isName) elif itemType == type(Borrower()): itemType = "borrowers" convertTo = type(Borrower()) itemToBeDeleted = searchPerson(searchKeyword, itemType, isName) elif itemType == type(Book()): itemType = "books" convertTo = type(Book()) itemToBeDeleted = searchBook(searchKeyword, isName) else: print("Invalid Object!\n") displayMessage(masterFrame, messageRow, ERROR_MESSAGE, "red") # check the type of returned item: (to validate that it exists) if type(itemToBeDeleted) != convertTo: displayMessage(masterFrame, messageRow, ERROR_MESSAGE, "red") return False # clear specific list from JSON file: newData = data.copy() newData[itemType] = [] saveIntoFile(newData, objectData, masterFrame, messageRow) # add all items again, but skip specific one: for item in data[itemType]: item = convertDictIntoObject(item, convertTo) if item == itemToBeDeleted: continue addItem(item, objectData, masterFrame, messageRow, isDeleting=True) return itemToBeDeleted
def setUp(self) -> None: # setting up testing set of instances self.item1 = Book("Tropic of Cancer", "Henry Miller", "Autobiographical novel", 1934, 318) self.item2 = Book("1984", "George Orwell", "Novel", 1949, 328) self.item3 = Book("Junkie", "William S. Burroughs", "Autobiographical novel", 1953, 166) self.item4 = Book("On the Road", "Jack Kerouac", "Autobiographical novel", 1957, 320) self.item5 = Comics("Deadpool", "Marvel", "Superhero", 2019, 25) self.item6 = Journal("ATT", "AT", "Celebrity", "10.20.2021", 14) self.item7 = Newspaper("NYT", "NY", "Weekly", "04.10.2020", 20) self.item8 = Magazine("Rolling Stone", "Wenner Media LLC", "Musical", "04.10.2020", 18) result = [self.item1, self.item2, self.item3, self.item4, self.item5, self.item6, self.item7] self.manager = BookstoreManager(result)
def getBookAsinList(self): self.bookAsinList = [] html = MyHtml.getHtml( self.url, name=self.tag, crawlDate=self.fetchDate) divProductList = html.xpath(".//div[@class='productList']")[0] trListProductList = divProductList.xpath("./table/tr[@class='small']") for i, tr in enumerate(trListProductList): if i % 2 == 1: continue aUrl = tr.xpath("./td[2]/a")[0].attrib['href'].strip() asin = Book.getAsinFromUrl(aUrl) if asin != '' and asin[0] != 'B': book = Book.loadBookByAsin(asin, self.fetchDate) if self.checkBook(book): self.bookAsinList.append(asin)
def console(): global path global user global comment global pause global duration global audioNum # [[[ 1. Update Playing Video ]]] if 0 != len(Book.List()) and False is pause: # < Can Play > if False is video.CheckPlaying(): # < Not Playing > # [[ 1.1. Lock ]] lock.acquire() try: # [[ 1.2. Get and Update First Reservation ]] bookList = Book.List()[0] path = bookList[2] user = bookList[3] comment = bookList[4] dummy = bookList[8] audioIndex = int(bookList[7]) duration = videoInfo.GetDuration(path) audioNum = videoInfo.GetAudioNum(path) # [[ 1.2. Check Dummy ]] if 0 == dummy: # < Not Dummy > # [ 1.2.1. Play Video ] video.Open(path, vol, audioNum, audioIndex) # [ 1.2.2. Add History ] History.Add(path, user, comment) # [ 1.2.3. Update pause status for playing ] pause = False else: # < Dummy > # [ 1.2.4. Switch HDMI Signal ] hdmi.Switch() # [ 1.2.5. Update pause status for pausing ] pause = True except: import traceback traceback.print_exc() finally: # [[ 1.3. Delete Playing Book ]] Book.Delete(bookList[0]) # [[ 1.4. Unlock ]] lock.release()
def createAgent(self, agentType, numberOfAgents): """ create numberOfAgents agents, instances of the class indicated by agentType str agentType: can be either Firm, Household, Book int numberOfAgents: number of the agents """ for iterator in range(numberOfAgents): if agentType == "Household": randomNumber = np.random.randint(0,100) if randomNumber < common.percentageOfNumbAgents: anAgent = Household(typeOfAgent = "Numb", priceForGoods=np.random.normal(common.initialHouseholdMeanPriceForGoods, common.initialHouseholdStandardDeviationPriceForGoods), thresholdForConsumpition = common.initialThresholdForConsumption, nameOfTheAgent= "Household_" + str(iterator + 1) ) else: anAgent = Household(typeOfAgent = "Trend", priceForGoods=np.random.normal(common.initialHouseholdMeanPriceForGoods, common.initialHouseholdStandardDeviationPriceForGoods), thresholdForConsumpition = common.initialThresholdForConsumption, nameOfTheAgent= "Household_" + str(iterator + 1) ) self.householdList.append(anAgent) elif agentType == "Firm": anAgent = Firm(averagePreviousPriceForGoods = common.firstAveragePreviousPriceForGoods, priceForGoods = np.random.normal(common.initialFirmMeanPriceForGoods, common.initialFirmStandardDeviationPriceForGoods), nameOFTheFirm = "Firm_" + str(iterator + 1) ) aBook = Book(anAgent.nameOFTheFirm) self.firmList.append(anAgent) self.bookList.append(aBook)
def displayItem(itemType, objectData, masterFrame, messageRow): """Display specific item on user input fields. Args: itemType (class type): the object's type to be displayed. objectData (): user input handler. masterFrame (): the frame that the message will be displayed on it. messageRow (int): the place to display the output message. """ # item to be displayed: item = None # capture item's data: searchKeyword, isName = captureData(itemType, objectData) # fetch specific item: if itemType == type(Member()): item = searchPerson(searchKeyword, "members", isName) elif itemType == type(Borrower()): item = searchPerson(searchKeyword, "borrowers", isName) elif itemType == type(Book()): item = searchBook(searchKeyword, isName) else: print("Invalid Object!\n") displayMessage(masterFrame, messageRow, ERROR_MESSAGE, "red") # display item's data and appropriate message: if item: insertFields(objectData, item) displayMessage(masterFrame, messageRow, DONE_MESSAGE, "green") else: displayMessage(masterFrame, messageRow, ERROR_MESSAGE, "red")
def __init__(self,instfile,date,outputdir='./'): generic_decoder.__init__(self) self.prod = {} # Read the products' database and instantiate all the # products and instruments emdi_date = dateutil.parser.parse("{}-{}-{}".format(date[0:4],date[4:6],date[6:8])) for row in csv.DictReader(open(instfile)): # take only active (==0) simple instruments (<2) st = row['SecurityType'] if (st=='O' or st=='F') and int(row['SecurityStatus'])==0: # check date <= maturity maturity = dateutil.parser.parse("{}-{}-{}".format( row['MaturityDate'][0:4], row['MaturityDate'][4:6], row['MaturityDate'][6:8])) if emdi_date <= maturity: pid = row['MarketSegmentID'] if pid not in self.prod: # Create product self.prod[pid] = Product() uid = row['SecurityID'] # make file name filename =( outputdir+'/'+row['MarketSegment']+row['MaturityMonthYear'] +'-'+row['StrikePrice']+'-' + ('Put' if row['PutOrCall']=='P' else 'Call') +'_'+date+'.csv') # create Order Book for each instrument self.prod[pid].inst[uid] = Book(uid,date,5,"level_2",ofile=filename)
def getBookAsinList(self): self.bookAsinList = [] self.list404 = [] self.listnq = [] cnt = 0 #try: # with open('./rand1000List', 'a+') as f: # with open('./newAsinListShuffle', 'r') as f: # with open('./random1000final', 'w') as fout: with open('./list1.txt', 'r') as f: for eachLine in f: if eachLine.strip() in self.bookAsinList: continue if eachLine.strip()[0].isdigit() is False: continue print eachLine book = Book.loadBookByAsin( eachLine.strip(), self.fetchDate) if self.checkBook(book): self.bookAsinList.append(eachLine.strip()) cnt += 1 print 'load {0} of {1}: {2}'.format( cnt, self.numOfBooks, eachLine.strip()) if cnt >= self.numOfBooks: break
def searchBook(name_or_isbn, isName=False): """Search books list using name or ISBN to see if specific book exists or not. Args: name_or_isbn (string): the name or ISBN of the book. isName (bool, optional): Specify if first argument is name or not. Defaults to False. Returns: dict: the required book if exists. """ # check JSON file existence: data = checkFileExistence() # set searching data: searchField = "isbn" if isName: searchField = "name" # find specific book: for book in data["books"]: if book[searchField] == name_or_isbn: print("Book exists.") book = convertDictIntoObject(book, type(Book())) return book print("Book does NOT exist.\n") return False
def parallelCall(m): print("Parallel call started") if cfg['debug']['FLAG']: logging.info("Recommendation started") # m = getOriginalMetadata(clientId) try: recommendations = book.get_recommendations(m) except Exception as e: error = e logging.error("Error in getting Recommendations" + repr(e)) return # recommendationsList = ', '.join(str(x) for x in recommendations.tolist()) # print(recommendationsList) for i in recommendations.index: try: item_id = i recommendations_list = m.at[i, 'recommendations'] conn = connect() cursor = conn.cursor() sql = "INSERT INTO recommendations (item_id, recommendation) values (%s, %s) on DUPLICATE KEY UPDATE recommendation = %s" cursor.execute( sql, (item_id, str(",".join(str(i) for i in recommendations_list)), str(",".join(str(i) for i in recommendations_list)))) conn.commit() conn.close() except Exception as e: logging.error("Error while writing recommendation to the table") logging.error(item_id + ":" + repr(e)) if cfg['debug']['FLAG']: logging.info("Recommendation Ended") print("Paralled call ended")
def getPreBookReviewRanking(self, curReviewID): if self.rID == '': return '0' i = 0 self.preBookID = '' self.preReviewID = '' self.preReviewRanking = '' while i < len(self.allProductLinks): if self.allProductLinks[i][-2] == curReviewID: i = i + 1 break i = i + 1 while i < len(self.allProductLinks): if self.allProductLinks[i][-1] == '1': preBookLink = self.allProductLinks[i][0] if preBookLink != '': self.preBookID = preBookLink.replace( '/ref=cm_cr-mr-title', '') self.preBookID = self.preBookID[-10:] self.preReviewID = self.allProductLinks[i][-2] break else: continue i = i + 1 del i if self.preReviewID != '' and self.preBookID != '': self.preReviewRanking = Book.findReviewRank( self.preBookID, self.preReviewID) return self.preReviewRanking
def __init__(self, eng_name, model): super(TFEngine, self).__init__() self.eng_name = eng_name self.model = model self.book = Book.load_GoGoD_book() self.last_move_probs = np.zeros(( self.model.N, self.model.N, )) self.kibitz_mode = False # build the graph with tf.Graph().as_default(): with tf.device('/cpu:0'): self.feature_planes = tf.placeholder( tf.float32, shape=[None, self.model.N, self.model.N, self.model.Nfeat], name='feature_planes') self.logits = model.inference(self.feature_planes, self.model.N, self.model.Nfeat) saver = tf.train.Saver(tf.trainable_variables()) init = tf.initialize_all_variables() self.sess = tf.Session(config=tf.ConfigProto( log_device_placement=False)) self.sess.run(init) checkpoint_dir = os.path.join(model.train_dir, 'checkpoints') Checkpoint.restore_from_checkpoint(self.sess, saver, checkpoint_dir)
def getRecommondBooks(label): # get the recommonded books label = urllib.quote(label) rand = int(random.uniform(0, 20)) url = u"http://api.douban.com/book/subjects?alt=json&tag=%s&start-index=%d&max-results=6&apikey=%s" % ( label, rand, API_KEY) res_json = urllib.urlopen(url).read() res_dict = simplejson.loads(res_json) books = list() for entry in res_dict["entry"]: title = entry["title"]["$t"] ibsn10 = entry["db:attribute"][0]["$t"] ibsn13 = entry["db:attribute"][1]["$t"] image = entry["link"][2]["@href"] link = entry["link"][1]["@href"] try: author = " ".join([i["name"]["$t"] for i in entry["author"]]) except: author = " " book = Book.Book(title, author, ibsn10, ibsn13, image, link) books.append(book) #for book in books: # print book return books
def read_books(self, fn): if self.num_book == self.capa: return -2 opened = True try: handle = open(fn, 'r') except FileNotFoundError: opened = False return -1 if opened: for i in handle: sep = i.strip().split(',') if sep[0] != '' and sep[1] != '': sep = i.strip().split(',') booktitle = sep[0] bookauthor = sep[1] obj = Book.Book(bookauthor, booktitle) self.booklist.append(obj) self.num_book += 1 if self.num_book == self.capa: return self.capa if i == '': self.num_book += 1 pass return self.num_book
def valid(): try: a = reg_no_field.get() b = password_field.get() if a not in dic.keys(): print("Username doesnt exist") clear() elif b != dic[a]: print("Incorrect paasword") clear() else: Book.main(reg_no_field.get()) except ValueError: print("Enter correct Values") clear()
def __init__(self, stock_input_file, stock_size, output_to_file=None): """ :param str stock_input_file: :param str output_to_file: :param int stock_size: """ self.stock_input_file = stock_input_file self.my_book = Book.ProcessOrder(stock_size, output_to_file)
def checkdup(): directoryPath, fileName = File.Get(request.query.fileId) if directoryPath[-1] is not os.path.sep: directoryPath = directoryPath + os.path.sep for book in Book.List(): if directoryPath + fileName == book[2]: return "{\"dup\":true}" return "{\"dup\":false}"
def get_most_read_book(self): most_read_book = Book('', '') num_times = 0 for book in self.books.keys(): if num_times < self.books[book]: num_times = self.books[book] most_read_book = book return most_read_book
def getBookAsinList(self): self.bookAsinList=[] html=MyHtml.getHtml(self.url,name="NewReleasedBooks",crawlDate=self.fetchDate) divBooksList = html.xpath(".//div[@id='zg_centerListWrapper']")[0] divItemsList=divBooksList.xpath("./div[@class='zg_itemImmersion']") for item in divItemsList: #item=divItemsList[0] #if item: aUrl=item.xpath("./div[2]/div[2]/a")[0].attrib['href'].strip() rank=item.xpath("./div[@class='zg_rankDiv']/span")[0].text.strip() asin=Book.getAsinFromUrl(aUrl) if asin!='': book=Book.loadBookByAsin(asin,self.fetchDate) #if self.checkBook(book,rank): self.bookAsinList.append(asin) print self.bookAsinList
class BookManager: dummy1 = Book.Book('돌이킬 수 없는 약속', '야쿠마루 가쿠', 3) dummy2 = Book.Book('죽고 싶지만 떡볶이는 먹고 싶어', '백세희', 2) dummy3 = Book.Book('열두 발자국', '정재승', 4) books = [dummy1, dummy2, dummy3] # 책이 대출 가능한지 확인해준다. def checkBookAvailable(self, bookId): return self.books[bookId].status # 책의 내용을 변경한다. def updateStatus(self, changes=None, data=None): # 책 등록 if changes == 'register': book = Book.Book(data[0], data[1], data[2]) self.books.append(book) # 책 대출 if changes == 'borrow': if self.checkBookAvailable(data): self.decreaseNumOfBooks(data) else: print('대출 불가능한 책입니다.') return # 책 반납 if changes == 'return': self.increaseNumOfBooks(data) # 저자나 책 이름으로 검색했을 때 결과값을 출력하고 book 의 index 와 book 을 반환한다. def searchData(self, data): for i, book in enumerate(self.books): if (data in book.author) or (data in book.name == data): print('책 이름 :', self.books[i].name) print('저자 :', self.books[i].author) print('대출 가능한 책의 개수 :', self.books[i].numOfBooks) return i, book print('검색 결과가 없습니다.') return False def decreaseNumOfBooks(self, bookIndex): self.books[bookIndex].numOfBooks -= 1 if self.books[bookIndex].numOfBooks <= 0: self.books[bookIndex].status = False def increaseNumOfBooks(self, bookIndex): self.books[bookIndex].numOfBooks += 1
def reserve(): if None is Book.ReserveDetail(request.query.bookId): redirect( \ "/playlist?user=" + request.query.user) else: return template( \ 'reserve', \ name = request.query.user, \ id = request.query.bookId)
def create_book(self, title, isbn): unique = True for book in self.books.keys(): if book.get_isbn() == isbn: print("ISBN {} not unique".format(isbn)) unique = False if unique: new_book = Book(title, isbn) return new_book
def highest_rated_book(self): best_book = Book('', '') best_rating = 0 for book in self.books.keys(): rating = book.get_average_rating() if best_rating < rating: best_rating = rating best_book = book return best_book
def __init__(self, key, height, val=Book()): # Initialize the node with the string key, book object val # and the int height self.key = key self.val = val self.height = height # set the left and right ptr's to null self.left_ptr = None self.right_ptr = None
def readBooks(filename): with open(filename, 'r') as bookFile: try: data = json.load(bookFile) for book in data: b = Book.Book(book["isbn"], book["title"], book["author"], book["text"]) books.append(b) except: print("No books imported")
def pick_book_move(self, color): if self.book: book_move = Book.get_book_move(self.board, self.book) if book_move: print "playing book move", book_move return Move(book_move[0], book_move[1]) print "no book move" else: print "no book" return None
def take_random_book(count): books_ = [] data_base = create_connection(DB) info = read_query(data_base, read_books) for i in range(count): rnd = random.randrange(0, len(info)) book = Book(info[rnd][0], info[rnd][1], info[rnd][2], info[rnd][3], info[rnd][4], info[rnd][5]) books_.append(book) return books_
def solveBook(self): with open("../data/" + self.fetchDate.isoformat() + "/book.txt", "w") as\ fout: ct = CommonTool(fout) fout.write(Book.Book.tableHead) fout.write('\n') fout.flush() # ct.writeln(Book.Book.tableHead) for i, asin in enumerate(self.bookAsinList): print 'solve Book {0} of {1}: {2}'.format( i, self.length(), asin) book = Book.loadBookByAsin(asin) book.addTag(self.tag) book.solveCustomerReview() book.insertBookDataIntoTable() book.printData(ct) Book.saveBook(book) self.reviewList.extend(book.reviewList) with open("./reviewList.txt", "w") as fff: fff.write('\n'.join(self.reviewList))
def __init__(self, eng_name, model): super(TFEngine,self).__init__() self.eng_name = eng_name self.model = model self.book = Book.load_GoGoD_book() self.last_move_probs = np.zeros((self.model.N, self.model.N,)) self.kibitz_mode = False # build the graph with tf.Graph().as_default(): with tf.device('/cpu:0'): self.feature_planes = tf.placeholder(tf.float32, shape=[None, self.model.N, self.model.N, self.model.Nfeat], name='feature_planes') self.logits = model.inference(self.feature_planes, self.model.N, self.model.Nfeat) saver = tf.train.Saver(tf.trainable_variables()) init = tf.initialize_all_variables() self.sess = tf.Session(config=tf.ConfigProto(log_device_placement=False)) self.sess.run(init) checkpoint_dir = os.path.join(model.train_dir, 'checkpoints') Checkpoint.restore_from_checkpoint(self.sess, saver, checkpoint_dir)
def printData(self): with open("../data/" + self.fetchDate.isoformat() + "/dataAll.txt", "w") as fout: ct = CommonTool(fout) fout.write("\t".join(self.tableHeadList)) fout.write('\n') fout.flush() for i, reviewID in enumerate(self.reviewList): if reviewID == '': continue print 'solve review {0} of {1}: {2}'.format( i, len(self.reviewList), reviewID) review = Review.loadReview(reviewID) book = Book.loadBookByAsin(review.asin) reviewer = Reviewer.loadReviewer(review.reviewerID) ct.write(reviewID) ct.write(review.asin) ct.write(review.reviewerID) ct.write(reviewer.rName) ct.write(reviewer.tRev1) ct.write(reviewer.tRev10) ct.write(reviewer.tRev50) ct.write(reviewer.tRev100) ct.write(reviewer.tRev500) ct.write(reviewer.tRev1000) ct.write(reviewer.tRevHall) ct.write(reviewer.vVoice) ct.write(review.verified) ct.write(review.rate) ct.write(review.title) ct.write(review.date) ct.write(review.fetchDate) ct.write(review.reviewBookDate) ct.write(review.elapsedDate) ct.write(review.helpful) ct.write(review.total) ct.write(review.helpfulness) ct.write(review.helpfulRank) ct.write(review.timeRank) #ct.write(review.top1Percent) #ct.write(review.top5Percent) #ct.write(review.top10Percent) ct.write(review.description) ct.write(review.numOfComments) ct.write(review.comment) #ct.write(review.isQuoteTable) ct.write(review.lastReviewRank) ct.write(book.url) ct.write(book.tag) ct.write(book.allowPreview) ct.write(book.binding) ct.write(book.publishDate) ct.write(book.author) ct.write(book.authorInfo) ct.write(book.rate) ct.write(book.numOfReviews) ct.write(book.kindlePrice) ct.write(book.hardcoverPrice) ct.write(book.paperbackPrice) ct.write(book.bookDsc) ct.write(book.listPrice) ct.write(book.pages) ct.write(book.isbn10) ct.write(book.isbn13) ct.write(book.subrank) ct.write(book.hasEditorialReview) ct.write(book.editorialReview) #ct.write(book.hasQuoteTable) ct.write(reviewer.email) ct.write(reviewer.webPage) ct.write(reviewer.hasPhoto) ct.write(reviewer.rNum) ct.write(reviewer.helpRate) ct.write(reviewer.hVote) ct.write(reviewer.tVote) ct.write(reviewer.avgRate) ct.write(reviewer.fRevTime) ct.write(reviewer.lRevTime) ct.write(reviewer.duration) if reviewer.rReal == "N/A": ct.write(0) else: ct.write(1) if reviewer.location == "N/A": ct.write(0) else: ct.write(1) if reviewer.aboutMe == "N/A": ct.write(0) else: ct.write(1) if reviewer.interest == "N/A": ct.write(0) else: ct.write(1) ct.write(review.fromFormat) # if review.fromFormat == "Hardcover": # ct.write(0) # elif review.fromFormat == "Paperback": # ct.write(1) # else: # ct.write(2) if reviewer.rRank == "N/A": ct.write(0) else: ct.write(reviewer.rRank) ct.writeln(book.rank)
def getPreviousReviewedBook(self,reviewerId): initUrl="http://www.amazon.com/gp/cdp/member-reviews/"+reviewerId self.allRevLink=initUrl html=MyHtml.getHtml(initUrl) ftable=html.xpath('.//body/table[2]')[0] pages=ftable.xpath('./tr/td[2]/table[1]/tr[1]/td[2]/b/a[last()]') if pages is not None and len(pages)>0: totalPages=pages[0].text.strip() else: totalPages=1 strPages=str(totalPages) print strPages if "-" in strPages: totalPages=totalPages.split('-') print "totalPages",totalPages totalPages=totalPages[1] else: totalPages=totalPages print totalPages,"totalPages" sortBy='MostRecentReview' j=1 self.counter=0 flag=0 for j in range(1,(int(totalPages)+1)): baseUrl="http://www.amazon.com/gp/cdp/member-reviews/"+reviewerId baseUrl=baseUrl+ \ '?pageNumber={}&sortBy={}'.format(str(j), sortBy) print baseUrl,j,totalPages html=MyHtml.getHtml(baseUrl) ftable=html.xpath('.//body/table[2]')[0] mainTable=ftable.xpath('./tr/td[2]/table[2]/tr[@valign="top"]') for row in mainTable: if row is not None: isBook=row.xpath('./td[5]/table/tr[2]/td/b') if isBook is not None and len(isBook)>0: if isBook[0].text is not None and flag==0: edition=isBook[0].text.strip() if "Edition" in edition: print "got the previous book" flag=1 reviewdate=row.xpath('./following-sibling::*') reviewdate=reviewdate[0].xpath('.//nobr') if reviewdate: reviewdate=reviewdate[0].text.strip() print "got the reviewDate",reviewdate self.previousBookReviewDate=CommonTool.strToDate(reviewdate) #to get link of the previous reviewedbook url=row.xpath('./td[5]/table/tr[1]/td/b/a')[0].attrib['href'] asin=Book.getAsinFromUrl(url) previousBook=Book.loadBookByAsin(asin) print "asinofPrevious",asin print "previousBook",previousBook self.previousBookPublishDate=previousBook.publishDate if j==1 and self.lRevTime=='': reviewdate=mainTable[0].xpath('./following-sibling::*') reviewdate=reviewdate[0].xpath('.//nobr') print reviewdate,"lRevtime" if reviewdate: reviewdate=reviewdate[0].text.strip() self.lRevTime=CommonTool.strToDate(reviewdate) print "value of j",j if j==int(totalPages) and self.fRevTime=='': print "inside frevtime loop" reviewdate=mainTable[-1].xpath('./following-sibling::*') reviewdate=reviewdate[0].xpath('.//nobr') print reviewdate,"fRevtime" if reviewdate: reviewdate=reviewdate[0].text.strip() self.fRevTime=CommonTool.strToDate(reviewdate) # rate rateObj = row.xpath("./following-sibling::*") rate = 'N/A' if len(rateObj) != 0: rateObj1 = rateObj[0].xpath(".//img") title = rateObj1[0].attrib['title'] rate = title.split("out")[0].strip() self.sum = self.sum + float(rate) j=j+1 #end of inner for loop print "sum",self.sum if self.rNum is not 0: self.avgRate=self.sum/self.rNum self.avgRate=round(self.avgRate,2) if self.lRevTime=='' or self.fRevTime=='': duration=0 else : duration = (self.lRevTime-self.fRevTime).days self.duration=int(duration)