def main(site=None, date=None): d = { 'site':site or 'finance', 'random':random(), 'date':date or str(datetime.date.today()), 'page':1 } request = urllib2.Request(url=urlTemplate%d, headers=header) request.add_header('Referer', request.get_full_url()) request.add_header('Host', re.search(r'([a-z]+\.)+[a-z]+', request.get_full_url()).group()) res = urllib2.urlopen(request).read().decode('gbk') data = None try: data = json.loads(res)['data'] except Exception as e: print sys.exc_traceback() return page = 1 jobs = [] while page <= data['count']: print 'page:%d'%page d['page'] = page d['random'] = random() page += 1 request = urllib2.Request(url=urlTemplate%d, headers=header) request.add_header('Referer', request.get_full_url()) request.add_header('Host', re.search(r'([a-z]+\.)+[a-z]+', request.get_full_url()).group()) jobs.append(gevent.spawn(fetch_each_page, request)) gevent.joinall(jobs) ret = True for j in jobs: for i in j.value: ret = ret and not i return ret
def deleteDiscussion(self, discussionId): print "Delete Discussion with ID = ", discussionId # splitting the announcement Id to get the Team name and announcement id in string format splitDisId = discussionId.split(":") disObjId = splitDisId[1] Team = splitDisId[0] from bson.objectid import ObjectId try: objectId = ObjectId(disObjId) except: print "error: discussion Id is Invalid - MONGO.py", sys.exc_traceback() respcode = 400 abort(400, respcode) try: disCount = self.dc.find({"_id": objectId}) if disCount > 0: self.dc.remove({"_id": objectId}) print "Delete discussion successful" return {"success": True} else: print "error: discussion not found - MONGO.py" respcode = 404 abort(404, respcode) except: print "error: discussion Internal server error - MONGO.py", sys.exc_traceback() respcode = 500 abort(500, respcode)
def deleteAnnouncement(self, annId): print "Delete Announcement with ID = ", annId # splitting the announcement Id to get the Team name and announcement id in string format splitAnnId = annId.split(":") annObjId = splitAnnId[1] Team = splitAnnId[0] from bson.objectid import ObjectId try: objectId = ObjectId(annObjId) except: print "error: announcement Id is Invalid - MONGO.py", sys.exc_traceback() respcode = 400 abort(400, respcode) try: deleteCount = self.ac.find({"_id": objectId}).count() if deleteCount > 0: self.ac.remove({"_id": objectId}) print "Delete successful" return {"success": True} else: print "error: Announcement not found - MONGO.py" respcode = 404 abort(404, respcode) except: print "error: announcement Internal server error - MONGO.py", sys.exc_traceback() respcode = 500 abort(500, respcode)
def updateCourse(self, jsonData, courseId): print "Update Course with courseId" # splitting the course Id into Team Name and course id string format splitCourseId = courseId.split(":") Team = splitCourseId[0] courseStrId = splitCourseId[1] print "String Course Id = ", courseStrId + "Team Name", Team from bson.objectid import ObjectId try: obj_id = ObjectId(courseStrId) except: print "Error: Not a valid Object ID", sys.exc_traceback() respCode = 500 abort(500, respCode) print "Converted object Id from string to object = ", obj_id checkCourseEntry = self.cc.find({"_id": obj_id}).count() print "course entry = ", checkCourseEntry if checkCourseEntry > 0: try: self.cc.update({"_id": obj_id}, {"$set": {"category": jsonData['category'], "term": jsonData['term'],"Description": jsonData['Description'], "title": jsonData['title'], "section": jsonData['section'],"days": jsonData['days'], "hours": jsonData['hours'], "dept": jsonData['dept'], "instructor": jsonData['instructor'], "attachment": jsonData['attachment'], "year": jsonData['year']}}) #"instructor": jsonData['instructor'] print "Course details updated successfully" return jsonData except: print "error: course Id or Json is invalid", sys.exc_traceback respCode = 400 abort(400, respCode) else: print "error: course not found" respCode = 404 abort(404, respCode)
def addDiscussion(self, jsonObj): print "Add discussion ---> mongo.py" # This will update the postDate of the user discussion localtime = time.asctime(time.localtime(time.time())) # Splitting the course id to obtain the Team name and CourseId in string format splitCourseId = jsonObj['courseId'].split(":") courseObjId = splitCourseId[1] Team = splitCourseId[0] from bson.objectid import ObjectId try: objectId = ObjectId(courseObjId) except: print "Error: Course Id is invalid", sys.exc_traceback() respCode = 400 abort(400, respCode) checkCourseEntry = self.cc.find({"_id": objectId}).count() if checkCourseEntry > 0: try: additionalInfo = {"created_at": localtime, "updated_at": localtime} print "Created at and updated at ", additionalInfo finalJsonObj = dict(jsonObj.items() + additionalInfo.items()) self.dc.insert(finalJsonObj) print "Discussion added successfully" #objectIdStr = str(finalJsonObj['_id']) discussionId = Team + "discussion" + ":" + courseObjId Id = {"discussionId": discussionId} responsediscussion = self.dc.find_one({"_id": finalJsonObj['_id']}) if len(responsediscussion) > 0: del responsediscussion['_id'] finalResponse = dict(Id.items() + responsediscussion.items()) return finalResponse else: print "error: Discussion Entry Id invalid" respCode = 400 abort(400, respCode) except: print "error in adding discussion: ", sys.exc_value() respCode = 500 abort(500, respCode) else: print "Error: Either course is not present or no more offered" respCode = 404 abort(404, respCode)
def ListenThread(c, addr): global clients isConnected = True helper = csn_server_helper.ServerHelper(c, addr) clients.append(helper) while isConnected: try: data = c.recv(1024) #print(data) if (len(data) > 0): #print(data) helper.CheckPacket(data) except: print("Unexpected error:", sys.exc_info(), sys.exc_traceback()) try: clients.remove(helper) except: print("Unexpected error:", sys.exc_info()) return
def getOwnedCourses(self, email): print "Get owned courses with email - ", email from bson.objectid import ObjectId isUserPresent = self.uc.find({"email": email.strip("'")}).count() if isUserPresent > 0: ownCourseList = self.uc.find_one({"email": email}) ownCourseId = ownCourseList['own'] print ownCourseId totalOwnIdCount = len(ownCourseId) count = 0 jsonData = [] while count < totalOwnIdCount: # converting the arrays of owned Id into object iD try: numberValueOfId = ownCourseId[count] print numberValueOfId # splitting the obtained course ID to get Team name and object Id in string format splitCourseId = numberValueOfId.split(":") print splitCourseId[1] obj_id = ObjectId(splitCourseId[1]) except: print "Id from own user collection is invalid", sys.exc_traceback() respcode = 400 abort(400, respcode) # fetching details of courses with own course ID obtaied from usercollection own field courseDetails = self.cc.find_one({"_id": obj_id}) courseIdFromCC = str(courseDetails['_id']) Team = splitCourseId[0] ownCourseIdWithTeam = Team + ":" + courseIdFromCC # creating the course Id Json with Team name appended finalId = {'courseId': ownCourseIdWithTeam} del courseDetails['_id'] finalCourseDetails = dict(finalId.items() + courseDetails.items()) jsonData.append(finalCourseDetails) count = count + 1 finalJsonData = json.dumps(jsonData) return finalJsonData else: print "error: user not found", sys.exc_traceback respcode = 404 abort(404, respcode)
continue if row[0] == 'gid': continue try: addtional_data = os.path.split(filename)[1].split('.')[0] + ';' addtional_data = addtional_data + SEASON + ';' row = addtional_data + row insertArray(cur, "daily_stats", row.split(';')) except lite.Error: print(sys.exc_info()[1]) #print(sys.exc_traceback()) sys.exit(1) con.commit() con = None cur = None try: con = lite.connect('bball.db') cur = con.cursor() except lite.Error: print(sys.exc_info()[1]) print(sys.exc_traceback()) sys.exit(1) create_daily_stats_table(cur) for filename in glob.glob("files/*"): loadFile(filename)
try: data = conn.recv(1024) #print(data) if (len(data) > 0): print(data) if (data[0] == 1): TriggerAlarm(s, data[1]) if (data[0] == 2): Disarm(s) connected = False conn.close() else: connected = False except: print("Error receiving data") print(sys.exc_traceback()) ''' disarm = bytearray() disarm.append(1) #disarm length. Non-flexible disarm.append(2) s.send(disarm) ''' #bytez =b'0' #bytez +=b'10' #s.send(bytez + bytes("testtestte".encode("utf8"))) ''' while True: bytez = b'' bytez +=b'0' bytez +=b'10'
def addQuiz(self, jsonObj): print "Add quiz ---> mongo.py" # splitting the obtained course Id from the front end to split into Team Name and splitCourseId = jsonObj['courseId'].split(":") # splitCourseId is a array that contains Team name at 0th position and courseId at 1th position Team = splitCourseId[0] from bson.objectid import ObjectId try: courseObjectId = ObjectId(splitCourseId[1]) #print courseObjectId except: print "Invalid course Id " print sys.exc_traceback() respcode = 400 abort(400, respcode) print "Is the course still available online" ifFoundCourse = self.cc.find({"_id": courseObjectId}).count() if ifFoundCourse > 0: findCourseInQuiz = self.qc.find_one({"courseId": jsonObj['courseId'].strip("'")}) print "Quiz details - When quizzes are already present of a course", findCourseInQuiz if findCourseInQuiz != None: try: print "Course entry with quiz details already present" self.qc.update({"courseId": jsonObj['courseId'].strip("'")}, {"$addToSet": {"questions": jsonObj['questions'][0]}}) self.qc.update({"courseId": jsonObj['courseId'].strip("'")}, {"$addToSet": {"questions": jsonObj['questions'][1]}}) print "details updated in already present courseId and quiz" quizDetails = self.qc.find_one({"courseId": jsonObj['courseId'].strip("'")}) objectId = quizDetails['_id'] objectIdStr = str(objectId) Obj_id = Team + "quiz" + ":" + objectIdStr del quizDetails["_id"] additionInfo = {"quizId": Obj_id, "success": True} finalResponseQuiz = dict(additionInfo.items() + quizDetails.items()) return finalResponseQuiz except: print "Error: In adding quiz details", sys.exc_traceback respcode = 500 abort(500, respcode) else: try: print "This course never had a quiz added - Adding it for first time" self.qc.insert(jsonObj) objectId = jsonObj['_id'] objectIdStr = str(objectId) Obj_id = Team + "quiz" + ":" + objectIdStr del jsonObj["_id"] additionInfo = {"quizId": Obj_id, "success": True} finalResponseQuiz = dict(additionInfo.items() + jsonObj.items()) return finalResponseQuiz except: print "Error: In adding quiz details", sys.exc_traceback respcode = 500 abort(500, respcode) else: print "error: Course not found" respcode = 404 abort(404, respcode)