Пример #1
0
	def handle(self, *args, **options):
		dt_begin = datetime.date.today() - datetime.timedelta(days = 1)
		db = _mysql.connect('mvts.db.1.2.3.4', 'mvts', 'mvts', 'pass')
		while dt_begin < datetime.date.today():
			tabname = '%d%02d' % (dt_begin.year, dt_begin.month)
			q = 'SELECT in_zone, dp_name, SUM(elapsed_time)/1000, SUM(elapsed_time > 0), SUM(elapsed_time IS NULL) FROM mvts_cdr_%s WHERE cdr_date >= "%s 00:00:00" AND cdr_date <= "%s 23:59:59" GROUP BY in_zone, dp_name' % (tabname, dt_begin.isoformat(), dt_begin.isoformat())
			self.say('%s\r\n' % q)
			db.query(q)
			r = db.store_result()
			o = r.fetch_row(maxrows = 0)
			for os in o:
				if os[2]:
					rep = LcrMVTSReport(mvts_id = 1)
					rep.dt_day = dt_begin
					rep.seconds = int(float(os[2]))
					rep.customer = os[0]
					rep.destination = os[1]
					if os[3]:
						rep.calls = os[3]
					else:
						rep.calls = 0
					if os[4]:
						rep.failed = os[4]
					else:
						rep.failed = 0
					if rep.customer and rep.destination:
						rep.save()	
			dt_begin += datetime.timedelta(days = 1) 
		db.close()
Пример #2
0
def event_day(request, year=None, month=None, day=None, **kwargs):
    """Displays the list of calendar for the given day.
    """
    current_day = _current_day(year, month, day)
    previous_day = current_day - datetime.timedelta(1)
    next_day = current_day + datetime.timedelta(1)

    return archive_day(
        request,
        year=year,
        month=month,
        day=day,
        queryset=Event.objects.for_user(request.user),
        date_field="start",
        month_format="%m",
        allow_empty=True,
        allow_future=True,
        template_name="calendar/event_day.html",
        extra_context={
            'previous_day': previous_day,
            'next_day': next_day,
            'current_day': current_day
        },
        **kwargs
    )
Пример #3
0
def when(time):
    """Formats now() - time in human readable format."""
    import datetime
    from pylons.i18n import ungettext
    difference = datetime.datetime.utcnow() - time
    if datetime.timedelta(seconds=60) > difference:
        num = difference.seconds
        return ungettext("%(num)s second ago",
                         "%(num)s seconds ago",
                         num) % {'num': num}
    elif datetime.timedelta(seconds=3600) > difference:
        num = difference.seconds / 60
        return ungettext("%(num)s minute ago",
                         "%(num)s minutes ago",
                         num) % {'num': num}
    elif datetime.timedelta(1) > difference:
        num = difference.seconds / 3600
        return ungettext("%(num)s hour ago",
                         "%(num)s hours ago",
                         num) % {'num': num}
    elif datetime.timedelta(5) > difference:
        num = difference.days
        return ungettext("%(num)s day ago",
                         "%(num)s days ago",
                         num) % {'num': num}
    else:
        return time.strftime("%Y-%m-%d")
Пример #4
0
	def test_runRecurring(self):
		import datetime
		import time

		scheduler = TaskScheduler()

		scheduler.startScheduler()

		class TaskChecker(ITask):
			stack = []

			def __init__(self, minutes = 0, recurring = False):
				super(TaskChecker, self).__init__(minutes = minutes, recurring = recurring)

			def run(self, time):
				TaskChecker.stack.push(time)

		task = TaskChecker(minutes = 1, recurring = True)
		t = datetime.datetime.now() + datetime.timedelta(seconds = 60)
		scheduler.addTask(task)

		for i in range(3):
			time.sleep(75) # Give it some time to work.

			self.assertEquals(len(TaskChecker.stack), i + 1)
			self.assertAlmostEqual(t, TaskChecker.stack[i], datetime.timedelta(15))

			t += datetime.timedelta(seconds = 60)

		scheduler.stopScheduler()
Пример #5
0
	def _get_working_days(self,cr,uid,context=None):
		# getting previous month start date
		today = datetime.date.today()
		print "today-----------", today
		first = datetime.date(day=1, month=today.month, year=today.year)
		mon = today.month - 1
		if mon == 0:
			mon = 12
		else:
			mon = mon
		tot_days = calendar.monthrange(today.year,mon)[1]
		print "tot_days-------------------------->>>>",tot_days
		test = first - datetime.timedelta(days=tot_days)
		start = test.strftime('%Y-%m-%d')
		print "start---------------",start
		
		last = test - datetime.timedelta(days=1)
		print "yesterday............",last
		# getting previous month end date
		
		today = datetime.date.today()
		pre_last= first - datetime.timedelta(days=1)
		pre_mon_last_date = pre_last.strftime('%Y-%m-%d')
		print "pre_mon...............",pre_mon_last_date
		
		# getting no of working days
		
		daygenerator = (last + timedelta(x + 1) for x in xrange((pre_last - last).days))
		res= sum(1 for day in daygenerator if day.weekday() < 6)
		print "res..........",res
		return res
Пример #6
0
def compBDep(request):
    import datetime
    days = [] #Getting the dates of the week
    date = datetime.date.today()
    #date = datetime.datetime.strptime("2015-01-27", "%Y-%m-%d").date()
    start_week = date - datetime.timedelta(date.weekday())
    for i in range(7):
        days.append(str(start_week + datetime.timedelta(i)))

    def avr_calc(dep, jours):
        avr = []
        obsv = Observation.objects.select_related('idStation').filter(dateDebut__range=[start_week, jours[6]]).order_by('dateDebut')
        for cal in jours:
            qtObsv = 0
            sumObsv = 0
            for lect in obsv:
                if lect.idStation.idSiteSeninnelle.sectionCommunale.commune.departement.departement == dep and str(lect.dateDebut) == cal:
                    qtObsv += 1
                    sumObsv += float(lect.quantitePluie)
            if qtObsv == 0:
                avr.append(0.0)
            else:
                avr.append((sumObsv/qtObsv))
        print(avr)
        return avr
    rsltF = []
    deps = Departement.objects.all()
    for dep in deps:
        rsltF.append({'nomDep': dep.departement, 'moyDep': avr_calc(dep.departement, days)})
    return JsonResponse({'jr': days, 'table':rsltF})
Пример #7
0
def prepare_voucher(code='COUPONTEST', _range=None, start_datetime=None, end_datetime=None, benefit_value=100,
                    benefit_type=Benefit.PERCENTAGE, usage=Voucher.SINGLE_USE, max_usage=None):
    """ Helper function to create a voucher and add an offer to it that contains a product. """
    if _range is None:
        product = ProductFactory(title='Test product')
        _range = RangeFactory(products=[product, ])
    elif _range.num_products() > 0:
        product = _range.all_products()[0]
    else:
        product = ProductFactory(title='Test product')

    if start_datetime is None:
        start_datetime = now() - datetime.timedelta(days=1)

    if end_datetime is None:
        end_datetime = now() + datetime.timedelta(days=10)

    voucher = VoucherFactory(code=code, start_datetime=start_datetime, end_datetime=end_datetime,
                             usage=usage)
    benefit = BenefitFactory(type=benefit_type, range=_range, value=benefit_value)
    condition = ConditionFactory(value=1, range=_range)
    if max_usage:
        offer = ConditionalOfferFactory(
            name='PrepareVoucherOffer',
            benefit=benefit,
            condition=condition,
            max_global_applications=max_usage
        )
    else:
        offer = ConditionalOfferFactory(name='PrepareVoucherOffer', benefit=benefit, condition=condition)
    voucher.offers.add(offer)
    return voucher, product
Пример #8
0
    def populate(self):
        # clear the existing cache
        for x in range(0, len(self)):
            self.pop()

        if self.location != 'global':
            location = Location.get(self.location)
            if location.is_region:
                hubs = location.has_hubs
                profile_select = User.select(AND(IN(User.q.homeplaceID, hubs),
                    User.q.public_field==1,
                    User.q.active==1,
                    User.q.description != u"",
                    User.q.modified > datetime.datetime.now() - datetime.timedelta(days=365))).orderBy('modified').reversed()[:30]
            else:
                profile_select = User.select(AND(User.q.homeplaceID==location,
                    User.q.public_field==1,
                    User.q.active==1,
                    User.q.description != u"",
                    User.q.modified > datetime.datetime.now() - datetime.timedelta(days=365))).orderBy('modified').reversed()[:30]
        else:
            profile_select = User.select(AND(User.q.public_field==1,
                    User.q.active==1,
                    User.q.description != u"",
                    User.q.modified > datetime.datetime.now() - datetime.timedelta(days=365))).orderBy('modified').reversed()[:30]
        for profile in profile_select:
            cache_obj = self.objectcache_factory(profile)
            self.append(cache_obj)
    def findStartTimeAndEndTime(self, eventList, typeOfEventToSearchFor, startTime, endTime, duration):

        if startTime != None:
            startTimeAsDateTime = datetime.strptime(startTime, DATE_TIME_FORMAT)

        if endTime != None:
            endTimeAsDateTime = datetime.strptime(endTime, DATE_TIME_FORMAT)

        if startTime == None:
            startTimeAsDateTime = eventList[0][START_TIME_KEY]

        if endTime == None and duration == None:
            endTimeAsDateTime = eventList[len(eventList) - 1][START_TIME_KEY]

        elif endTime == None and duration >= 0:
            timeDelta = datetime.timedelta(days=duration)
            endTimeAsDateTime = startTimeAsDateTime + timeDelta

        elif endTime == None and duration < 0:
            timeDelta = datetime.timedelta(days=duration)
            endTimeAsDateTime = startTimeAsDateTime + timeDelta
            # now swap start time and end time since we have a negative duration
            tempTime = endTimeAsDateTime
            endTimeAsDateTime = startTimeAsDateTime
            startTimeAsDateTime = tempTime

        return (startTimeAsDateTime, endTimeAsDateTime)
Пример #10
0
def compute_speedmeter_params():
    current_date = timezone.now()
    date_7_days_ago = current_date - datetime.timedelta(days=7)
    reports_last_seven = Report.objects.filter(creation_time__gte=date_7_days_ago).filter(
        creation_time__lte=current_date)

    date_intervals = []
    days = 7
    while days >= 0:
        date_intervals.append(current_date - datetime.timedelta(days=days))
        days -= 1

    results = []
    for idx, val in enumerate(date_intervals):
        if idx + 1 >= len(date_intervals):
            break
        r = Report.objects.filter(creation_time__gte=date_intervals[idx]).filter(
            creation_time__lte=date_intervals[idx + 1])
        results.append(len(r))

    total = 0
    for result in results:
        total = total + result
    avg = total / len(results)

    data = {'reports_last_seven': len(reports_last_seven), 'avg_last_seven': avg}
    return data
Пример #11
0
def previous_calendar_week_v2(date_now):
    if not date_now.weekday() == 2:
        last_wednesday = date_now + (datetime.timedelta((2-date_now.weekday())%7) - (datetime.timedelta(days=7)))
    else:
        last_wednesday = date_now
    end_date = last_wednesday + datetime.timedelta(days=7)
    return (last_wednesday, end_date)
Пример #12
0
 def generateStartTimeBasedOnFreq(self, busLine, capacity, frequency, startTime):
     """ Generate all the trips within a time slice given a single starting time
     
     Args: 
          busLine: an integer representing the bus line ID
          frequency: the headway in minutes between successive buses
          startTime: a datetime object representing the start time within the time slice
     
     Return: 
          an array containing all the starting times for the bus trips within the corresponding time slice.
     """
     # we make sure the starting time is in between the upper and lower bound of our time slices
     startTimeArray = []
     lineTimes = {}
     for x in DB.timeSliceArray:
         start = datetime.datetime.combine(Fitness.yesterday, datetime.time(x[0], 0, 0))
         end = datetime.datetime.combine(Fitness.yesterday, datetime.time(x[1], 59, 59))
         if start <= startTime <= end:
             nextStartTime = startTime + datetime.timedelta(minutes=frequency)
             nextStartTime2 = startTime - datetime.timedelta(minutes=frequency)
             startTimeArray.append([startTime, capacity])
             if nextStartTime <= end:
                 startTimeArray.append([nextStartTime, capacity])
             if nextStartTime2 >= start:
                 startTimeArray.append([nextStartTime2, capacity])
             while nextStartTime <= end:
                 nextStartTime = nextStartTime + datetime.timedelta(minutes=frequency)
                 if nextStartTime <= end:
                     startTimeArray.append([nextStartTime, capacity])
             while nextStartTime2 >= start:
                 nextStartTime2 = nextStartTime2 - datetime.timedelta(minutes=frequency)
                 if nextStartTime2 >= start:
                     startTimeArray.append([nextStartTime2, capacity])
     return sorted(startTimeArray) 
Пример #13
0
 def vacancy_deadline(self, cr, uid, context=None):
     request = self.pool.get('res.request')
     control = self.pool.get('hr.vacancy.control')
     control_ids = control.search(cr,uid,[('time','!=',0)])
     for control_id in control_ids:
         control_data = control.browse(cr, uid, control_id)
         vacancy_ids = self.search(cr, uid, [('state', '=', control_data.state)])
         for item in vacancy_ids:
             data = self.browse(cr, uid, item)
             # create date is used when control date is not defined
             # timedelta is a difference between current_date and date of state have been changed
             if not data.control_date: 
                 create_date = datetime.strptime(data.create_date, '%Y-%m-%d %H:%M:%S')
             else:
                 create_date = datetime.strptime(data.control_date, '%Y-%m-%d %H:%M:%S')
             current_date = datetime.now(pytz.utc).strftime('%Y-%m-%d %H:%M:%S')
             timedelta = current_date - create_date
             # preparing timedelta with custom time type
             if control_data.time_type == 'hours':
                 deadline_time = datetime.timedelta(hours=control_data.time)
             elif control_data.time_type == 'minutes':
                 deadline_time = datetime.timedelta(minutes=control_data.time)
             elif control_data.time_type == 'days':
                 deadline_time = datetime.timedelta(days=control_data.time)
             # deadline timedelta is ready
             if timedelta > deadline_time:
                 # preparing message text
                 message_text = control_data.message
                 replace_dict = [(u"ДОЛЖНОСТЬ", data.job_id.name),
                                 (u"АВТОР", data.user_id.name),
                                 (u"ЭТАП", STATES_LIST[control_data.state]),
                                 (u"ДАТА СОЗДАНИЯ",data.create_date)]
                 for item in replace_dict:
                     message_text = message_text.replace(item[0],item[1])
                 # message text is ready
                 # preparing act_to users
                 users = []
                 if control_data.send_to == 'director':
                     users.append(data.department_id.responsible_directors.id)
                 elif control_data.send_to == 'manager':
                     users.append(data.department_id.manager_id.user_id.id)
                 elif control_data.send_to == 'users':
                     for user in control_data.users:
                         users.append(user.id)
                 # users is ready
                 message = {
                     'body' :  message_text,
                     'name' : unicode("Необработанная заявка", "utf-8"),
                     'state' : 'waiting',
                     'priority' : '1',
                     'act_from' : uid,
                     'active': True,
                     'act_to': False,
                     }
                 for user in users:
                     message['act_to'] = user
                     request_id = request.search(cr,uid,['&',('body','=',message['body']),('act_to','=',message['act_to'])])
                     if not request_id:
                         request.create(cr, uid, message)
     return False
Пример #14
0
def generar_horarios(centro, fecha_asistencia):
	fecha_asistencia_date = datetime.datetime.strptime(fecha_asistencia, '%Y-%m-%d').date()
	now = datetime.datetime.today()

	#Para establecer el tiempo promedio
	tiempo_atencion = centro.tiempo_atencion
	tiempo_viejo = CentrosTiemposAtencion.objects.filter(fecha = fecha_asistencia_date).first()
	if tiempo_viejo:
		tiempo_atencion = tiempo_viejo.tiempo_atencion

	if now.date()>=fecha_asistencia_date and now.time() > centro.hora_apertura_manana:
		hora_inicial_manana = now.time()
	else:
		hora_inicial_manana = centro.hora_apertura_manana

	if now.date()>=fecha_asistencia_date and now.time() > centro.hora_apertura_tarde:
		hora_inicial_tarde = now.time()
	else:
		hora_inicial_tarde = centro.hora_apertura_tarde
	# hora_inicial_manana = centro.hora_apertura_manana if now < centro.hora_apertura_manana else now
	# hora_inicial_tarde = centro.hora_apertura_tarde if now < centro.hora_apertura_tarde else now
	
	if hora_inicial_manana < centro.hora_cierre_manana:
		cantidad_minutos_manana = diff_times_in_minutes(hora_inicial_manana,centro.hora_cierre_manana)
	else:
		cantidad_minutos_manana = 0

	if hora_inicial_tarde < centro.hora_cierre_tarde:
		cantidad_minutos_tarde = diff_times_in_minutes(hora_inicial_tarde,centro.hora_cierre_tarde)
	else:
		cantidad_minutos_tarde = 0

	cantidad_bloques_manana = cantidad_minutos_manana / tiempo_atencion
	cantidad_bloques_tarde = cantidad_minutos_tarde / tiempo_atencion
	
	lista_bloques = []
	contador_horas = hora_inicial_manana
	# print "Bloq man",cantidad_bloques_manana,"bloq tard",cantidad_bloques_tarde

	for i in range(0,cantidad_bloques_manana):
		datetime_aux = datetime.datetime(2014,1,1,contador_horas.hour,contador_horas.minute)
		proxima_hora = (datetime_aux + datetime.timedelta(minutes = tiempo_atencion)).time()
		bloque = Bloque(contador_horas, proxima_hora)
		cantidad_citas = NumeroOrden.objects.filter(hora_atencion = contador_horas, fecha_atencion = fecha_asistencia).exclude(solicitud_inspeccion__estatus__codigo = 'solicitud_cancelada').count()
		bloque.capacidad = centro.peritos.filter(activo=True).count() - cantidad_citas
		lista_bloques.append(bloque)
		contador_horas = proxima_hora

	contador_horas = hora_inicial_tarde
	for i in range(0,cantidad_bloques_tarde):
		datetime_aux = datetime.datetime(2014,1,1,contador_horas.hour,contador_horas.minute)
		proxima_hora = (datetime_aux + datetime.timedelta(minutes = tiempo_atencion)).time()
		bloque = Bloque(contador_horas, proxima_hora)
		cantidad_citas = NumeroOrden.objects.filter(hora_atencion = contador_horas, fecha_atencion = fecha_asistencia).exclude(solicitud_inspeccion__estatus__codigo = 'solicitud_cancelada').count()
		bloque.capacidad = centro.peritos.filter(activo=True).count() - cantidad_citas
		lista_bloques.append(bloque)
		contador_horas = proxima_hora

	return lista_bloques
def getRangeDates(dt,weekdaysList):
    dateWeekDay = calendar.day_name[dt.weekday()].upper()
    result = []
    #print "Date Range: ", dt, weekdaysList, dateWeekDay
    if len(weekdaysList) == 1:
        if dateWeekDay != weekdaysList[0]:
            # create dates [dateWeekDay,weekdaysList[0]]
            start = dayOfWeeks[dateWeekDay]
            end = dayOfWeeks[weekdaysList[0]]
            if start > end :
                diff = end + 7 - start
            else:
                diff = end - start
            start = 0
            while(start <= diff ):
                newDt = dt + datetime.timedelta(days=start) 
                result.append(newDt)
                start+=1
            return result
        else:
            result=[]
            result.append(dt)
            return result
    #elif len(weekdaysList) == 2 and weekdaysList[0]==weekdaysList[1]:   
    elif len(weekdaysList) == 2 :
        fromRangeDay =  weekdaysList[0]
        toRangeDay =  weekdaysList[1]
        start = dayOfWeeks[fromRangeDay]
        end = dayOfWeeks[toRangeDay]
        if start > end :
            diff = end + 7 - start
        else:
            diff = end - start
        start = 0
        while(start <= diff ):
            newDt = dt + datetime.timedelta(days=start) 
            result.append(newDt)
            start+=1
        return result
    # FROM MONDAY AFTERNOON THROUGH WEDNESDAY EVENING  ALTHOUGH IT WILL BE HOT THIS AFTERNOON AND AGAIN ON SUNDAY
    elif len(weekdaysList) ==3:
        #print "\n\n====================== ", weekdaysList , " ===========================\n\n"
        fromRangeDay =  weekdaysList[0]
        toRangeDay =  weekdaysList[1]
        start = dayOfWeeks[fromRangeDay]
        end = dayOfWeeks[toRangeDay]
        if start > end :
            diff = end + 7 - start
        else:
            diff = end - start
        start = 0
        while(start <= diff ):
            newDt = dt + datetime.timedelta(days=start) 
            result.append(newDt)
            start+=1
        newDt = getDateOfOneSpecificDay(dt,weekdaysList[2])
        result.append(newDt)
        return result
Пример #16
0
    def end_of_the_day(self,thedate):
        newdate = thedate+datetime.timedelta(days=1)
        newdatetime = datetime.datetime(\
            newdate.year,\
            newdate.month,\
            newdate.day,0,0,0)
        newdatetime = newdatetime-datetime.timedelta(hours=config._local_timezone)

        return(newdatetime)
Пример #17
0
def previous_calendar_month_week_chunks():
    end_date = datetime.datetime.now()
    start_date = end_date - datetime.timedelta(29)
    month_in_fours = []
    for i in range(4):
        start_date = start_date + datetime.timedelta(7)
        if start_date < end_date:
            month_in_fours.append(list(previous_calendar_week_v2(start_date))) #might have to miss out on the thursdays???
    return month_in_fours
Пример #18
0
def get_allmsgweekandmonth(lab_id):
	allmsg = []
        u = User.objects.filter(lab=lab_id)
	for i in range(len(u)):
        	mem  = {}
		mem['name'] = u[i].username
       		mem['lab'] = lab_id
		#获取本周总在线时长和本周平均在线时长
		d = datetime.datetime.now()
		now_week = d.weekday()
		monday = datetime.date.today() - datetime.timedelta(days=now_week)
		weekend = []
		thisweekalltime = 0;
	
		db = MySQLdb.connect("localhost","root","900706","mydb" )
                cursor = db.cursor()	
		
		for j in range(0,7):
			w_day = monday + datetime.timedelta(days=j)

			sql = "select times from myapp_daydata where mac='%s' and days='%s' " % (u[i].mac, w_day)
			cursor.execute(sql)
			userdata = cursor.fetchall()

			if len(userdata) == 1:
				thisweekalltime += float(userdata[0][0])	
	
        	mem['weekalltime'] = round(thisweekalltime / 60.0, 2)
        	mem['weekpingtime'] = round(thisweekalltime / 60.0 / ( now_week + 1 ), 2)
        	
		lastdate = datetime.date.today()
		thisweekalltime = 0
                thismonthdays = 0
		if lastdate.day == 1:
			thisweekalltime = 0
	                thismonthdays = 0

		else: 
			startdatetime = datetime.datetime(lastdate.year, lastdate.month, 1, 0, 0, 0)
			enddatetime = datetime.datetime(lastdate.year, lastdate.month, lastdate.day - 1, 23, 59, 59)
			sql = """select * from myapp_daydata where mac = '%s' and days between '%s' and '%s'""" % (u[i].mac, startdatetime, enddatetime)
			cursor.execute(sql)
                	userdata = cursor.fetchall()
			#thisweekalltime = 0
			#thismonthdays = 0
			for row in userdata:
				thismonthdays += 1
				thisweekalltime += float(row[3])
		mem['monthalltime'] = round(thisweekalltime / 60.0, 2)
		if thismonthdays == 0:
			mem['monthpingtime'] = 0
		else:
        		mem['monthpingtime'] = round(thisweekalltime / 60.0 / ( lastdate.day - 1), 2)
        
		allmsg.append(mem)
        allmsg.sort(key=lambda obj:obj.get('monthalltime'), reverse=True)
	return allmsg
Пример #19
0
def set_to_default():
    import datetime
    set(NICETIES_OPEN, datetime.timedelta(days=14))
    set(CLOSING_TIME, datetime.time(18, 0))
    set(CLOSING_BUFFER, datetime.timedelta(minutes=30))
    set(CACHE_TIMEOUT, datetime.timedelta(days=7))
    set(INCLUDE_FACULTY, False)
    set(INCLUDE_RESIDENTS, False)
    db.session.commit()
Пример #20
0
def getUpdatesJson(request):
    url = request.get_full_path()
    params = url.split('?')[1]
    request_dict = urllib.parse.parse_qs(params)
    updates_dict = {'updates': []}
    updates_list = defaultdict(list)
    sessions = []
    if ('id' in request_dict.keys()) and ('type' in request_dict.keys()):
        obj_ids = request_dict['id']
        obj_type = request_dict['type'][0]
        if obj_type == "session":
            for obj_id in obj_ids:
                session = Session.objects.get(id=int(obj_id))
                sessions.append(session)
        elif obj_type == "network":
            for obj_id in obj_ids:
                network = Network.objects.get(id=int(obj_id))
                for session in network.related_sessions.all():
                    sessions.append(session)
        elif obj_type == "device":
            for obj_id in obj_ids:
                device = DeviceInfo.objects.get(id=int(obj_id))
                for user in device.users.all():
                    for session in user.sessions.all():
                        sessions.append(session)
        else:
            for obj_id in obj_ids:
                node = Node.objects.get(id=int(obj_id))
                for session in node.related_sessions.all():
                    sessions.append(session)

        for session in sessions:
            for update in session.updates.all():
                updates_list[update.timestamp].append({'x': update.timestamp.strftime("%Y-%m-%d %H:%M:%S"), 'y': update.qoe, 'group':update.session_id})

        tses = sorted(updates_list.keys())
        for ts in tses:
            for update_obj in updates_list[ts]:
                updates_dict['updates'].append(update_obj)

        if ('anomaly' in request_dict.keys()):
            anomaly_id = int(request_dict['anomaly'][0])
            anomaly = Anomaly.objects.get(id=anomaly_id)
            anomaly_time = anomaly.timestamp
            update_start_window = anomaly_time - datetime.timedelta(minutes=5)
            update_end_window = anomaly_time + datetime.timedelta(minutes=5)
        else:
            update_end_window = tses[-1]
            update_start_window = update_end_window - datetime.timedelta(minutes=10)
        updates_dict['start'] = update_start_window.strftime("%Y-%m-%d %H:%M:%S")
        updates_dict['end'] = update_end_window.strftime("%Y-%m-%d %H:%M:%S")

        #output = json.dumps(updates_dict, indent=4, sort_keys=True)
        #return HttpResponse(output, content_type="application/json")
        return JsonResponse(updates_dict)
    else:
        return JsonResponse({})
def get_freq(fr):
  if fr == '1h':
    return datetime.timedelta(hours=1)
  elif fr == '1d':
    return datetime.timedelta(days=1)
  elif fr == '1w':
    return datetime.timedelta(weeks=1)
  elif fr == '1y':
    return datetime.timedelta(years=1)
Пример #22
0
  def post(self):
    chat = models.ChatMessage()

    author = users.get_current_user()
    if author:
      chat.author = author
    
    chat.content = self.request.get('content')
    
    
    cfs = ['', 'I love you! I have always loved you. There. I said it.', '', 
      ' I sometimes wear a bowtie in the shower.', '', 'My spirit animal is a nudist pirate named Karl.', 
      '', 'Just shut up. I am so sick and tired of you. SERIOUSLY. ', '', 'my ding ding hurts', '', 
      ' Do u w4nna cyb3r?', '', '', '', '' 'I have eight husbands and a one legged parakeet.', 
      '', 'I like making sweet love to Peruvian lamas.', 'HAI! I CAN HAZ A HUGZ?'
      'You had me at "Hello World."', 'How about we go home and you handle my exception?'
      ] 
    import random
    if False:
        c = random.choice(cfs)
        if c: chat.content += (' ' + c)
    
    
    chat_url = urllib2.unquote(self.request.get('url'))
    chat.session = models.ChatSession.get(chat_url, author)
    chat.put()
    
    chatsString = memcache.get(self.MEMCACHE_KEY + str(chat.session.key()))
    if chatsString is None:
      chatsList = []
    else:
      chatsList = pickle.loads(chatsString)
      if chatsList and len(chatsList) >= 100:
        chatsList.pop(0)
    chatsList.insert(0, chat)
    
    import datetime
    last_time = datetime.datetime.fromtimestamp(0)
    for msg in chatsList:
        logging.info(last_time + datetime.timedelta(seconds=120))
        logging.info(msg.date - (last_time + datetime.timedelta(seconds=120)))
        if msg.date > last_time + datetime.timedelta(seconds=120):
            msg.natural_date = (msg.date - datetime.timedelta(hours=7)).strftime("%I:%M%p - %A %B %d, %Y")
        last_time = msg.date
    if not memcache.set(self.MEMCACHE_KEY + str(chat.session.key()), pickle.dumps(chatsList)):
        logging.debug("Memcache set failed:")  
    
    template_values = {
      'chats': chatsList,
    }
    
    template = self.generate('chats.html', template_values)
    if not memcache.set(self.MEMCACHE_TEMPLATE + str(chat.session.key()), template):
        logging.debug("Memcache set failed:")          
    
    self.response.out.write(template)
Пример #23
0
def ListOnWeek(request):
    today =date.today()
    start_monday = today - datetime.timedelta(days=today.weekday())
    end_monday = today + datetime.timedelta(days=-today.weekday(), weeks=1)
    #task_list=Tasks.objects.order_by('-task_Due')[:50]
    template=loader.get_template('polls/ListOnWeek.html')
    #task_list=Tasks.objects.filter('date__range=[start_monday, end_monday]')[:50]
    task_list=Tasks.objects.filter(task_Due__range=(start_monday, end_monday))
    context={'task_list':task_list}
    return HttpResponse(template.render(context,request))
Пример #24
0
def eval_density():
    if len(tracker) > 1:
        times = tracker.keys()
        last30 = [k for k in times if k >= (datetime.utcnow() - datetime.timedelta(minutes=30)).isoformat()]
        last15 = [k for k in last30 if k >= (datetime.utcnow() - datetime.timedelta(minutes=15)).isoformat()]
        if len(last15) >= len(tracker) / 4:
            return 2
        if len(last30) >= len(tracker) / 2:
            return 1
    return 0
Пример #25
0
    def has_repo(self, repo_id):
        if hasattr(self, "_has_repo_last_run"):
            timediff = datetime.now() - self._has_repo_last_run
            if timediff < datetime.timedelta(seconds=self.githubApiInterval):
                wait_seconds = (datetime.timedelta(seconds=self.githubApiInterval) - timediff).seconds
                time.sleep(wait_seconds)

        ret = self.anon.repository(*self._split_repo_id(repo_id)) is not None
        self._has_repo_last_run = datetime.now()
        return ret
Пример #26
0
def checkDatebase(request):
    timezone.now()
    startdate = datetime.strptime("2016-02-05", '%Y-%m-%d')
    for i in range(0, 20):
        enddate = startdate + datetime.timedelta(days=1)
        news = NewsFeed.objects.filter(created_time__range=[startdate, enddate])
        print startdate, len(news)
        startdate = startdate - datetime.timedelta(days=1)

    return render_to_response('index.html')
Пример #27
0
 def plot_twilight(self,Ephem):
     '''
     Plot vertical lines on the astronomical twilights
     '''
     self.thegraph_time.axvline(\
      Ephem.twilight_prev_set+datetime.timedelta(hours=config._local_timezone),\
      color='k', ls='--', lw=2, alpha=0.5, clip_on=True)
     self.thegraph_time.axvline(\
      Ephem.twilight_next_rise+datetime.timedelta(hours=config._local_timezone),\
      color='k', ls='--', lw=2, alpha=0.5, clip_on=True)
Пример #28
0
def filterByDate(option,day,month,year,jsonObj):
	dateA = date(year, month, day)
	if(option == "week"):
		dateB = dateA + datetime.timedelta(days=-7)
	elif(option == "year"):
		dateB = dateA + datetime.timedelta(days=-365)
	elif(option == "month"):
		dateB = dateA + datetime.timedelta(days=-30)
	jsonObj = jsonObj.filter(file__published_date__range=[dateB,dateA])
	jsonObj = Json.dumps(convertValuesToMap(jsonObj), default=jdefault, indent=4)	
	return jsonObj
Пример #29
0
def find_last_day_of_month(year, month):
    """Finds the last day of a month.

    :param year: year as integer
    :param month: month as integer
    :return: last day of month as integer
    """
    import datetime
    next_month = datetime.datetime(year, month, 28) + datetime.timedelta(days=4)
    last_date = next_month - datetime.timedelta(days=next_month.day)
    return last_date.day
Пример #30
0
    def test_timedelta(self):
        import datetime

        conn = self.connect()
        cur = conn.cursor()

        self.assert_roundtrips(cur, datetime.timedelta(-1))
        self.assert_roundtrips(cur, datetime.timedelta(days=15, seconds=5874))
        self.assert_roundtrips(cur, datetime.timedelta(414))

        conn.close()
Пример #31
0
    def sms(self):
        dr = self.driver
        yes_time = datetime.datetime.now() + datetime.timedelta(days=-1)
        # date = yes_time.strftime('20%y-%m-%d')
        WebDriverWait(
            dr,
            10).until(lambda d: d.find_element_by_name("XposeHook")).click()
        time.sleep(1)
        WebDriverWait(
            dr, 10).until(lambda d: d.find_element_by_name("修改数据")).click()
        time.sleep(1)
        WebDriverWait(
            dr, 10).until(lambda d: d.find_element_by_name("本地数据")).click()
        time.sleep(1)
        local_text = "0条"
        for i in range(15):
            dr.swipe(300, 1000, 300, 400)
            time.sleep(1)
            try:
                dr.find_element_by_name(
                    datetime.datetime.now().strftime('20%y-%m-%d'))
                local_num = dr.find_elements_by_id(
                    "com.meiriq.xposehook:id/actv_count")
                local_text = local_num[local_num.__len__() - 2].text
                time.sleep(1)
                dr.press_keycode(4)
                time.sleep(1)
                dr.press_keycode(4)
                time.sleep(1)
                dr.press_keycode(4)
                time.sleep(1)
                break
            except:
                pass
        with open("/sdcard/1/timeshanghai.log", 'r', encoding='utf-8') as f:
            data_run = f.read()
        with open("/sdcard/1/timeshanghai2.log", 'r', encoding='utf-8') as f:
            data_run2 = f.read()
        with open("/sdcard/1/usershanghai.log", 'r', encoding='utf-8') as f:
            data_sign = f.read()
        with open("/sdcard/device.txt", 'r', encoding='utf-8') as f:
            selectuser = f.read()
        match_local = re.search(r'(\d+)', local_text)
        match_run = re.findall(
            r'%s.%s' % (str(int(
                yes_time.strftime('%m'))), str(int(yes_time.strftime('%d')))),
            data_run)
        match_run2 = re.findall(
            r'%s.%s' % (str(int(
                yes_time.strftime('%m'))), str(int(yes_time.strftime('%d')))),
            data_run2)
        match_sign = re.findall(
            r'%s.%s' % (str(int(
                yes_time.strftime('%m'))), str(int(yes_time.strftime('%d')))),
            data_sign)
        device_num = re.search(r'(\d+)', selectuser).group(1)
        biaoti = "上海观察" + str(device_num)
        neirong = yes_time.strftime('%m/%d,') + "本地:" + match_local.group(
            1) + "注册:" + str(len(match_sign)) + "体验激活:" + str(
                len(match_run)) + "体验留存:" + str(len(match_run2))
        mail_Info = {
            "from": "*****@*****.**",
            "to": "*****@*****.**",
            "hostName": "smtp.qq.com",
            "userName": "******",
            "password": "******",
            "mailSubject": "%s" % biaoti,
            "mailText": "%s" % neirong,
            "mailEncoding": "utf-8"
        }
        smtp = SMTP_SSL(mail_Info["hostName"])
        #ssl登录
        smtp.set_debuglevel(1)
        smtp.ehlo(mail_Info["hostName"])
        smtp.login(mail_Info["userName"], mail_Info["password"])

        msg = MIMEText(mail_Info["mailText"], "html",
                       mail_Info["mailEncoding"])
        msg["Subject"] = Header(mail_Info["mailSubject"],
                                mail_Info["mailEncoding"])
        msg["From"] = mail_Info["from"]
        msg["To"] = mail_Info["to"]
        smtp.sendmail(mail_Info["from"], mail_Info["to"], msg.as_string())
        smtp.quit()
Пример #32
0
            and timespan['end_t'] > cur_timeStamp:
            my_Calc_table.extend([one_table[0]])
        elif timespan['end_t'] == cur_timeStamp:
            break

    if len(my_Calc_table) <= 0:
        msg = time_tag + 'no table found for my_Calc_table data'
        common.warn(my_log, log_tag, msg)

    #now_time = datetime.datetime.now()
    now_time = datetime.now()
    shifen = now_time.strftime("%H%M")

    if int(shifen) < 0030:
        for i in range(0, 3):
            yes_time = now_time + datetime.timedelta(days=(-3 + i))
            ymd = yes_time.strftime('%Y%m%d')
            #ymd = '201407'+ymd[6:8]
            for idx, one_table in enumerate(my_channel_table):
                if ymd in one_table:
                    Calc_table_3day.extend([one_table])
    else:
        for i in range(0, 3):
            #yes_time = now_time + datetime.timedelta(days=(-2 + i))
            yes_time = now_time + timedelta(days=(-2 + i))
            ymd = yes_time.strftime('%Y%m%d')
            #ymd = '201407'+ymd[6:8]
            for idx, one_table in enumerate(my_Calc_table):
                if ymd in one_table:
                    Calc_table_3day.extend([one_table])
Пример #33
0
    def _onchange_date_from(self):
    
        def daterange(start_date, end_date):
            for n in range(int((end_date - start_date).days)):
                yield start_date + datetime.timedelta(n)

        if not self._context.get('no_recompute_days', False):
            super(working_hour, self)._onchange_date_from()

        self.working_hour = 0

        if not self.date_from or not self.date_to or not self.employee_id:
            return
        contract = self.employee_id.contract_ids
        if not contract:
            return
        day_from = datetime.datetime.strptime(self.date_from, '%Y-%m-%d %H:%M:%S')
        day_to = datetime.datetime.strptime(self.date_to, '%Y-%m-%d %H:%M:%S')

        for single_date in daterange(day_from.date(), day_to.date() + datetime.timedelta(days=1)):
            single_date = datetime.datetime.strptime(str(single_date)+" 00:00:00" ,'%Y-%m-%d %H:%M:%S')
            current_contracts = self._get_contract(single_date,contract)
            hh_start = 0
            hh_end = 0.0

            if not current_contracts:
                continue
            calendar_attendance = current_contracts.working_hours.attendance_ids.filtered(lambda att: att.dayofweek == str(single_date.weekday()))

            #se il permesso è di un solo giorno il day from e il day_to combaciano con il single date
            if single_date.date() == day_from.date():
                hh_start = day_from.time()
            if single_date.date() == day_to.date():
                hh_end = day_to.time()

            #primo gg e 1 gg di ferie
            if hh_start!=0.0 and hh_end!=0.0:
                hour_from = float(hh_start.hour + 2) + float('0.' + self.converter_cent(float(hh_start.minute)))
                hour_to = float(hh_end.hour + 2) + float('0.' + self.converter_cent(float(hh_end.minute)))

                for c in calendar_attendance:
                    if hour_from >= c.hour_from and hour_to <= c.hour_to:
                        self.working_hour += float(hour_to - hour_from)
                    elif hour_from >= c.hour_from and hour_to >= c.hour_to and hour_from<=c.hour_to:
                        self.working_hour += float(c.hour_to - hour_from)
                    elif hour_from <= c.hour_from and hour_to <= c.hour_to and hour_to >= c.hour_from:
                        self.working_hour += float(hour_to - c.hour_from)
                    elif hour_from <= c.hour_from and hour_to <= c.hour_to and hour_to <= c.hour_from:
                        self.working_hour += 0
                    elif hour_from <= c.hour_from and hour_to >= c.hour_to:
                        self.working_hour += float(c.hour_to - c.hour_from)

            #devo considerare come start l'ora inserita e come end l'ora di fine turno (primo giorno su più gg di permesso)
            if hh_end == 0.0 and hh_start!= 0.0:
                for c in calendar_attendance:
                    if hh_start.hour+2 >= c.hour_from and hh_start.hour+2 <= c.hour_to:
                        hour_from = float(hh_start.hour+2) + float('0.'+self.converter_cent(float(hh_start.minute)))
                        self.working_hour += float(c.hour_to - hour_from)
                    elif hh_start.hour +2 >= c.hour_from and hh_start.hour +2 >= c.hour_to:
                        continue
                    else:
                        self.working_hour += float(c.hour_to) - float(c.hour_from)

            #giorni intermedi, si contano l'orario di inzio e fine turno
            if hh_end == 0.0 and hh_start == 0.0:
                for c in calendar_attendance:
                    self.working_hour += float(c.hour_to - c.hour_from)

            #ultimo giorno : lo start è l'inizio del turno e l'end è la data inserita
            if hh_end!=0.0 and hh_start == 0.0:
                hour_to = float(hh_end.hour + 2) + float('0.' + self.converter_cent(float(hh_end.minute)))
                for c in calendar_attendance:
                    if hour_to <= c.hour_to and hour_to>= c.hour_from:
                        self.working_hour += float(hour_to-c.hour_from)
                    elif hour_to >= c.hour_to and hour_to>= c.hour_from:
                        self.working_hour += float(c.hour_to - c.hour_from)
                    elif hour_to >= c.hour_to and hour_to>= c.hour_from:
                        self.working_hour += 0
Пример #34
0
async def mention_afk(mention):
    """ This function takes care of notifying the people who mention you that you are AFK."""
    global COUNT_MSG
    global USERS
    global ISAFK
    global USER_AFK  # pylint:disable=E0602
    global afk_time  # pylint:disable=E0602
    global afk_start
    global afk_end
    back_alivee = datetime.now()
    afk_end = back_alivee.replace(microsecond=0)
    afk_since = "a while ago"
    if ISAFK and mention.message.mentioned:
        now = datetime.now()
        datime_since_afk = now - afk_time  # pylint:disable=E0602
        time = float(datime_since_afk.seconds)
        days = time // (24 * 3600)
        time = time % (24 * 3600)
        hours = time // 3600
        time %= 3600
        minutes = time // 60
        time %= 60
        seconds = time
        if days == 1:
            afk_since = "Kemarin"
        elif days > 1:
            if days > 6:
                date = now + datetime.timedelta(
                    days=-days, hours=-hours, minutes=-minutes)
                afk_since = date.strftime("%A, %Y %B %m, %H:%I")
            else:
                wday = now + datetime.timedelta(days=-days)
                afk_since = wday.strftime("%A")
        elif hours > 1:
            afk_since = f"`{int(hours)} jam {int(minutes)} menit` yang lalu"
        elif minutes > 0:
            afk_since = f"`{int(minutes)} menit {int(seconds)} detik` yang lalu"
        else:
            afk_since = f"`{int(seconds)} detik` yang lalu"

        is_bot = False
        if (sender := await mention.get_sender()):
            is_bot = sender.bot
            if is_bot: return  # ignore bot

        chat_obj = await mention.client.get_entity(mention.chat_id)
        chat_title = chat_obj.title

        if mention.sender_id not in USERS or chat_title not in USERS:
            if AFKREASON:
                await mention.reply(
                    f"ЁЯПГ Saya telah meninggalkan telegram sejak {afk_since}.\
                        \nReason: `{AFKREASON}`\nтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦДтЦДтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦД  \nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦИтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦИтЦИтЦИтЦИтЦИтЦИтЦАтЦАтЦАтЦИтЦСтЦСтЦСтЦСтЦИтЦИтЦИтЦИтЦИтЦИтЦА  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦАтЦА"
                )
            else:
                await mention.reply(str(choice(AFKSTR)))
            if mention.sender_id is not None:
                USERS.update({mention.sender_id: 1})
            else:
                USERS.update({chat_title: 1})
        else:
            if USERS[mention.sender_id] % randint(2, 4) == 0:
                if AFKREASON:
                    await mention.reply(
                        f"ЁЯПГ Saya masih meninggalkan telegram sejak {afk_since}.\
                            \nReason: `{AFKREASON}`\nтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦДтЦДтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦД  \nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦИтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦИтЦИтЦИтЦИтЦИтЦИтЦАтЦАтЦАтЦИтЦСтЦСтЦСтЦСтЦИтЦИтЦИтЦИтЦИтЦИтЦА  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦАтЦА"
                    )
                else:
                    await mention.reply(str(choice(AFKSTR)))
                if mention.sender_id is not None:
                    USERS[mention.sender_id] += 1
                else:
                    USERS[chat_title] += 1
            COUNT_MSG += 1
Пример #35
0
def before_request():
    flask.session.permanent = True
    app.permanent_session_lifetime = datetime.timedelta(minutes=30)
    flask.session.modified = True
    flask.g.user = flask_login.current_user
Пример #36
0
 def was_commented_recently(self):
     return self.created >= timezone.now() - datetime.timedelta(days=1)
Пример #37
0
 def daterange(start_date, end_date):
     for n in range(int((end_date - start_date).days)):
         yield start_date + datetime.timedelta(n)
Пример #38
0
def coerce_gmaps_time(time):
    lst = time.split()
    if len(lst) == 4:
        return datetime.timedelta(hours=int(lst[0]), minutes=int(lst[2]))
    elif len(lst) == 2:
        return datetime.timedelta(minutes=int(lst[0]))
Пример #39
0
datetime.time(20, 19, 55, 170002)

>>> t2=t1.replace(month=t1.month+1)
>>> t2.date()
datetime.date(2020, 3, 15)

>>> t2=t1.replace(month=t1.month+1,day=t1.day+3)
>>> t2.date()
datetime.date(2020, 3, 18)

# delta
>>> t1=datetime.now()
>>> t2=datetime.now()
>>> t=t2-t1
>>> t
datetime.timedelta(0, 128, 638181)
>>> t.seconds
128
>>> t.days
0
>>> t.microseconds
638181

# timedelta
>>> from datetime import timedelta

>>> a=timedelta(days=7,hours=5)
>>> a
datetime.timedelta(7, 18000)

>>> now=datetime.now()
Пример #40
0
# Set some defaults
calcend = 0
seed_num = 100000
add_nml_line = False

# Read in command line parameters
(opts,args) = getopt.getopt(sys.argv[1:],'i:d:l:e:f2p')
for o,a in opts:
   if o == '-i':
      today = datetime.datetime.today()
      date_start = today.strftime('%Y%m%d') + str(a)
   if o == '-d':
      date_start = a
   if o == '-l':
      calcend = 1
      lengthdt = datetime.timedelta(hours=int(a))
   if o == '-e':
      seed_num = int(a) * 100000
   if o == '-f':
      max_dom = 1
   if o == '-p':
      add_nml_line = True
   if o == '-2':
      # Rewrite this as if domain 2 was domain 1
      max_dom = '1'
      wps_namelist['geogrid']['e_we'] = [wps_namelist['geogrid']['e_we'][1]]
      wps_namelist['geogrid']['e_sn'] = [wps_namelist['geogrid']['e_sn'][1]]
      wrf_namelist['domains']['e_we'] = [wps_namelist['geogrid']['e_we'][1]]
      wrf_namelist['domains']['e_sn'] = [wps_namelist['geogrid']['e_sn'][1]]
     
Пример #41
0
 def was_published_recently(self):
     return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
Пример #42
0
if __name__ == '__main__':

    # ETL part one; Extract

    # Headers which spotify api requires
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {SPOTIFY_TOKEN}'
    }

    # Yesterdays time from seconds to unixms
    today = datetime.datetime.now()
    yesterday = today - datetime.timedelta(
        days=1
    )  # <-- How many days we are going to go back to get played songs
    yesterday_unix_timestamp = int(yesterday.timestamp()) * 1000

    # Getting all songs I have listened in last 24 hours
    r = requests.get(
        'https://api.spotify.com/v1/me/player/recently-played?after{time}'.
        format(time=yesterday_unix_timestamp),
        headers=headers)

    data = r.json()

    # ETL part two; Transform

    song_names = []
    artist_names = []
def read_table(url):

    browser.get(url)

    dataframe = pd.DataFrame()

    filter_date_start = date.today() + datetime.timedelta(days=2)
    print(filter_date_start)

    filter_date_end = date.today() + datetime.timedelta(days=-183)
    print(filter_date_end)

    try:
        WebDriverWait(browser, 5).until(
            EC.element_to_be_clickable((
                By.ID,
                "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl04_dateInput"
            )))
    finally:
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl05_dateInput"
        ).click()
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl04_dateInput"
        ).send_keys(Keys.CONTROL, "a")
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl04_dateInput"
        ).send_keys(Keys.BACKSPACE)
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl04_dateInput"
        ).send_keys(str(filter_date_end))
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl05_dateInput"
        ).click()
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl05_dateInput"
        ).send_keys(Keys.CONTROL, "a")
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl05_dateInput"
        ).send_keys(Keys.BACKSPACE)
        browser.find_element_by_id(
            "ctl00_ContentPlaceHolder1_rfReport_ctl01_ctl08_ctl05_dateInput"
        ).send_keys(str(filter_date_start))
        try:
            browser.find_element_by_id(
                "ctl00_ContentPlaceHolder1_rfReport_ApplyButton").click()
            print("We did not have to wait to click the apply button")
        except:
            WebDriverWait(browser, 5).until(
                EC.element_to_be_clickable(
                    (By.ID, "ctl00_ContentPlaceHolder1_rfReport_ApplyButton"
                     ))).click()
            print("We had to wait to click the apply button.")
        try:
            WebDriverWait(browser, 1).until(
                EC.visibility_of_element_located(
                    (By.ID, 'ctl00_ContentPlaceHolder1_rgReport_ctl00__0')))
        finally:
            print("We have applied the date filter, we are now grabbing data.")

        # Here we define the number of pages we have to scrape through.

        items_and_pages_element = browser.find_element_by_class_name(
            "rgInfoPart").text
        digits_list = []
        pattern = r'[\d]+[.,\d]+|[\d]*[.][\d]+|[\d]+'
        if re.search(pattern, items_and_pages_element) is not None:
            for catch in re.finditer(pattern, items_and_pages_element):
                # print(catch[0])
                digits_list.append(catch[0])
        else:
            print("Something is broken.")

        # print(digits_list)

        items = int(digits_list[0])
        pages = int(digits_list[1])
        print("Number of items: " + str(items))
        print("Number of pages: " + str(pages))

        while int(len(dataframe.index)) < items:
            try:
                WebDriverWait(browser, 5).until(
                    EC.visibility_of_element_located(
                        (By.ID,
                         'ctl00_ContentPlaceHolder1_rgReport_ctl00__0')))
            finally:
                table_list = browser.find_elements_by_class_name('rgClipCells')
                table_we_want = table_list[1].get_attribute('outerHTML')
                dataframe = dataframe.append(pd.read_html(table_we_want),
                                             ignore_index=True)
                print(len(dataframe))
            try:
                WebDriverWait(browser, 5).until(
                    EC.element_to_be_clickable(
                        (By.CLASS_NAME, "rgPageNext"))).click()
            except:
                WebDriverWait(browser, 10).until(
                    EC.element_to_be_clickable(
                        (By.CLASS_NAME, "rgPageNext"))).click()
        else:
            print("We are done scraping on to the next query.")
            print(dataframe)
            print(len(dataframe.index))

    dataframe = dataframe[[
        0, 12, 3, 5, 6, 7, 2, 8, 9, 10, 4, 11, 13, 14, 18, 15, 16, 17
    ]]

    #TODO: Fix this ordering and rearrangin' of stuff

    # dataframe.rename(columns={0:"RatingID",12:"Checkbox3Value",3:"ServiceDate",5:"TestingComplete",6:"DataEntryComplete",7:"Reschedule", 2:"ServiceName",8:"Reinspection",9:"RescheduledDate",10:"Price",4:"Employee",11:"PONumber",13:"EmployeeTime5",14:"EmployeeTime6",18:"LastUpdated",16:"DateEntered",15:"EmployeeTime7",1:"ServiceID",17:"EnteredBy"})

    # ["RatingID","JobNumber","Address","City","State","Zip","Builder","Subdivision","GasUtility","ElectricUtility","Lot","Division","HERSIndex","BldgFile","DateEntered"]

    dataframe[17] = dataframe[17].str[-8:]
    dataframe[4] = pd.to_numeric(dataframe[4],
                                 downcast='integer',
                                 errors='ignore')
    dataframe[18] = pd.to_datetime(dataframe[18], utc=False)

    # dataframe.to_csv("Export_After_Reorganization.csv", encoding="utf-8", index=False)

    dataframe = dataframe.replace({r',': '.'}, regex=True)  # remove all commas
    dataframe = dataframe.replace({r';': '.'}, regex=True)  # remove all commas
    dataframe = dataframe.replace({r'\r': ' '},
                                  regex=True)  # remove all returns
    dataframe = dataframe.replace({r'\n': ' '},
                                  regex=True)  # remove all newlines

    # Remove the previous "DASH_Job_Export_Queue_Reader_Date.csv" file.
    if os.path.exists("DASH_Job_Export_Queue_Reader_Date.csv"):
        os.remove("DASH_Job_Export_Queue_Reader_Date.csv")
    else:
        print("We do not have to remove the file.")

    dataframe.to_csv("DASH_Job_Export_Queue_Reader_Date.csv", index=False)
Пример #44
0
async def afk_on_pm(sender):
    """Function which informs people that you are AFK in PM"""
    global ISAFK
    global USERS
    global COUNT_MSG
    global afk_time
    global afk_start
    global afk_end
    not_afk = datetime.now()
    afk_end = not_afk.replace(microsecond=0)
    afk_str = "a while ago"
    if (sender.is_private and sender.sender_id != 777000
            and not (await sender.get_sender()).bot):
        if PM_AUTO_BAN:
            try:
                from userbot.modules.sql_helper.pm_permit_sql import is_approved

                apprv = is_approved(sender.sender_id)
            except AttributeError:
                apprv = True
        else:
            apprv = True
        if apprv and ISAFK:
            now = datetime.now()
            afk_since = now - afk_time
            day = float(afk_since.seconds) // (24 * 3600)
            time = float(afk_since.seconds) % (24 * 3600)
            hours = time // 3600
            time %= 3600
            minutes = time // 60
            time %= 60
            seconds = time
            if day == 1:
                afk_str = "Yesterday"
            elif day > 1:
                if day > 6:
                    date = now + datetime.timedelta(
                        days=-day, hours=-hours, minutes=-minutes)
                    afk_since = date.strftime("%A, %Y %B %m, %H:%I")
                else:
                    wday = now + datetime.timedelta(days=-day)
                    afk_str = wday.strftime("%A")
            elif hours > 1:
                afk_str = f"`{int(hours)}h{int(minutes)}m` ago"
            elif minutes > 0:
                afk_str = f"`{int(minutes)}m{int(seconds)}s` ago"
            else:
                afk_str = f"`{int(seconds)}s` ago"
            if sender.sender_id not in USERS:
                if AFKREASON:
                    await sender.reply(
                        f"**SABAR LAGI AFK!!.** (Since: {afk_str})"
                        f"\nReason: `{AFKREASON}`.")
                else:
                    await sender.reply(
                        f"**SABAR LAGI AFK!!.** (Since: {afk_str})"
                        "\n**Tunggu ya kawan.**")
                USERS.update({sender.sender_id: 1})
                COUNT_MSG = COUNT_MSG + 1
            elif apprv and sender.sender_id in USERS:
                if USERS[sender.sender_id] % randint(2, 4) == 0:
                    if AFKREASON:
                        await sender.reply(
                            "**SABAR LAGI AFK!!.** (Since: {afk_str})"
                            f"\nReason: `{AFKREASON}`.")
                    else:
                        await sender.reply(
                            "**SABAR LAGI AFK!!.** (Since: {afk_str})"
                            "\n**Tunggu ya kawan.**")
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
                else:
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
Пример #45
0
 def getMetaData(self):
     return db.session.query(User, Logs).\
        filter(Logs.timestamp > datetime.timedelta(days = 1),
              Logs.timestamp <= datetime.datetime.today()).\
              join(Logs).all()
Пример #46
0
async def afk_on_pm(sender):
    """ Function which informs people that you are AFK in PM """
    global ISAFK
    global USERS
    global COUNT_MSG
    global COUNT_MSG
    global USERS
    global ISAFK
    global USER_AFK  # pylint:disable=E0602
    global afk_time  # pylint:disable=E0602
    global afk_start
    global afk_end
    back_alivee = datetime.now()
    afk_end = back_alivee.replace(microsecond=0)
    afk_since = "a while ago"
    if (sender.is_private and sender.sender_id != 777000
            and not (await sender.get_sender()).bot):
        if PM_AUTO_BAN:
            try:
                from userbot.modules.sql_helper.pm_permit_sql import is_approved

                apprv = is_approved(sender.sender_id)
            except AttributeError:
                apprv = True
        else:
            apprv = True
        if apprv and ISAFK:
            now = datetime.now()
            datime_since_afk = now - afk_time  # pylint:disable=E0602
            time = float(datime_since_afk.seconds)
            days = time // (24 * 3600)
            time = time % (24 * 3600)
            hours = time // 3600
            time %= 3600
            minutes = time // 60
            time %= 60
            seconds = time
            if days == 1:
                afk_since = "Kemarin"
            elif days > 1:
                if days > 6:
                    date = now + datetime.timedelta(
                        days=-days, hours=-hours, minutes=-minutes)
                    afk_since = date.strftime("%A, %Y %B %m, %H:%I")
                else:
                    wday = now + datetime.timedelta(days=-days)
                    afk_since = wday.strftime("%A")
            elif hours > 1:
                afk_since = f"`{int(hours)} jam {int(minutes)} menit` yang lalu"
            elif minutes > 0:
                afk_since = f"`{int(minutes)} menit {int(seconds)} detik` yang lalu"
            else:
                afk_since = f"`{int(seconds)} detik` yang lalu"
            if sender.sender_id not in USERS:
                if AFKREASON:
                    await sender.reply(
                        f"ЁЯПГ Saya telah meninggalkan telegram sejak {afk_since}.\
                        \nReason: `{AFKREASON}`\nтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦДтЦДтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦД  \nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦИтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦИтЦИтЦИтЦИтЦИтЦИтЦАтЦАтЦАтЦИтЦСтЦСтЦСтЦСтЦИтЦИтЦИтЦИтЦИтЦИтЦА  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦАтЦА"
                    )
                else:
                    await sender.reply(str(choice(AFKSTR)))
                USERS.update({sender.sender_id: 1})
                COUNT_MSG = COUNT_MSG + 1
            elif apprv and sender.sender_id in USERS:
                if USERS[sender.sender_id] % randint(2, 4) == 0:
                    if AFKREASON:
                        await sender.reply(
                            f"ЁЯПГ Saya masih meninggalkan telegram sejak {afk_since}.\
                            \nReason: `{AFKREASON}`\nтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦДтЦДтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦИтЦД  \nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦУтЦУтЦУтЦУтЦУтЦУтЦИтЦИтЦИтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИ\nтЦИтЦИтЦИтЦИтЦИтЦИтЦАтЦАтЦАтЦИтЦСтЦСтЦСтЦСтЦИтЦИтЦИтЦИтЦИтЦИтЦА  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦИтЦСтЦСтЦИ  \nтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦСтЦАтЦА"
                        )
                    else:
                        await sender.reply(str(choice(AFKSTR)))
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
                else:
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
Пример #47
0
async def mention_afk(mention):
    """ This function takes care of notifying the people who mention you that you are AFK."""
    global COUNT_MSG
    global USERS
    global ISAFK
    global USER_AFK  # pylint:disable=E0602
    global afk_time  # pylint:disable=E0602
    global afk_start
    global afk_end
    back_alivee = datetime.now()
    afk_end = back_alivee.replace(microsecond=0)
    afk_since = "**a while ago**"
    if mention.message.mentioned and not (await mention.get_sender()).bot:
        if ISAFK:
            now = datetime.now()
            datime_since_afk = now - afk_time  # pylint:disable=E0602
            time = float(datime_since_afk.seconds)
            days = time // (24 * 3600)
            time = time % (24 * 3600)
            hours = time // 3600
            time %= 3600
            minutes = time // 60
            time %= 60
            seconds = time
            if days == 1:
                afk_since = "**Yesterday**"
            elif days > 1:
                if days > 6:
                    date = now + \
                        datetime.timedelta(
                            days=-days, hours=-hours, minutes=-minutes)
                    afk_since = date.strftime("%A, %Y %B %m, %H:%I")
                else:
                    wday = now + datetime.timedelta(days=-days)
                    afk_since = wday.strftime('%A')
            elif hours > 1:
                afk_since = f"`{int(hours)}h {int(minutes)}m` ago"
            elif minutes > 0:
                afk_since = f"`{int(minutes)}m {int(seconds)}s` ago"
            else:
                afk_since = f"`{int(seconds)}s` ago"
            if mention.sender_id not in USERS:
                if AFKREASON:
                    await mention.reply(f"**I'm not available right now.** (Since **{afk_since}**).\
                        \nReason: `{AFKREASON}`")
                else:
                    await mention.reply(f"**I'm not available right now.** (Since **{afk_since}**).\
                        \n**Please come back later**")
                USERS.update({mention.sender_id: 1})
                COUNT_MSG = COUNT_MSG + 1
            elif mention.sender_id in USERS:
                if USERS[mention.sender_id] % randint(2, 4) == 0:
                    if AFKREASON:
                        await mention.reply(f"**I'm still not available right now.** (Since **{afk_since}**).\
                            \nReason: `{AFKREASON}`")
                    else:
                        await mention.reply(f"**I'm not available right now.** (Since **{afk_since}**).\
                        \n**Please come back later**")
                    USERS[mention.sender_id] = USERS[mention.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
                else:
                    USERS[mention.sender_id] = USERS[mention.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
Пример #48
0
def to_seconds(duration):
    x = time.strptime(duration, '%H:%M:%S')
    return datetime.timedelta(hours=x.tm_hour,
                              minutes=x.tm_min,
                              seconds=x.tm_sec).total_seconds()
Пример #49
0
# 时间戳转北京时间
time_1 = 1601863976	    # 以2020/10/5 10:12:56为例子
time_tuple_1 = time.localtime(time_1)
bj_time = time.strftime("%Y/%m/%d %H:%M:%S", time_tuple_1)
print("北京时间:", bj_time)


# 北京时间转时间戳
str_time = "2020/10/05 10:12:56"
time_tuple_2 = time.strptime(str_time, "%Y/%m/%d %H:%M:%S")
time_stample = time.mktime(time_tuple_2)
print("时间戳:", int(time_stample))




# 北京时间转时间戳
deal_time = '1968/1/4 21:0:0'   # 待转换的时间
dateTime_p = datetime.strptime(deal_time, '%Y/%m/%d %H:%M:%S')
metTime = dateTime_p - datetime(1970, 1, 1)
date_tample= metTime.days * 24 * 3600 + metTime.seconds - 28800  # 换算成秒数
print("时间戳:",date_tample)


#时间戳转北京时间
import datetime
timestamp = -1893436000     # 待转换的时间戳
print(datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=timestamp+8*3600)) # 北京时间

print(type(datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=timestamp+8*3600)))
Пример #50
0
async def afk_on_pm(sender):
    """ Function which informs people that you are AFK in PM """
    global ISAFK
    global USERS
    global COUNT_MSG
    global COUNT_MSG
    global USERS
    global ISAFK
    global USER_AFK  # pylint:disable=E0602
    global afk_time  # pylint:disable=E0602
    global afk_start
    global afk_end
    back_alivee = datetime.now()
    afk_end = back_alivee.replace(microsecond=0)
    afk_since = "**a while ago**"
    if sender.is_private and sender.sender_id != 777000 and not (
            await sender.get_sender()).bot:
        if PM_AUTO_BAN:
            try:
                from userbot.modules.sql_helper.pm_permit_sql import is_approved
                apprv = is_approved(sender.sender_id)
            except AttributeError:
                apprv = True
        else:
            apprv = True
        if apprv and ISAFK:
            now = datetime.now()
            datime_since_afk = now - afk_time  # pylint:disable=E0602
            time = float(datime_since_afk.seconds)
            days = time // (24 * 3600)
            time = time % (24 * 3600)
            hours = time // 3600
            time %= 3600
            minutes = time // 60
            time %= 60
            seconds = time
            if days == 1:
                afk_since = "**Yesterday**"
            elif days > 1:
                if days > 6:
                    date = now + \
                        datetime.timedelta(
                            days=-days, hours=-hours, minutes=-minutes)
                    afk_since = date.strftime("%A, %Y %B %m, %H:%I")
                else:
                    wday = now + datetime.timedelta(days=-days)
                    afk_since = wday.strftime('%A')
            elif hours > 1:
                afk_since = f"`{int(hours)}h {int(minutes)}m` ago"
            elif minutes > 0:
                afk_since = f"`{int(minutes)}m {int(seconds)}s` ago"
            else:
                afk_since = f"`{int(seconds)}s` ago"
            if sender.sender_id not in USERS:
                if AFKREASON:
                    await sender.reply(f"**I'm not available right now.** (Since **{afk_since}**).\
                        \nReason: `{AFKREASON}`")
                else:
                    await sender.reply(f"**I'm not available right now.** (Since **{afk_since}**).\
                        \n**Please come back later**")
                USERS.update({sender.sender_id: 1})
                COUNT_MSG = COUNT_MSG + 1
            elif apprv and sender.sender_id in USERS:
                if USERS[sender.sender_id] % randint(2, 4) == 0:
                    if AFKREASON:
                        await sender.reply(f"**I'm still not available right now.** (Since **{afk_since}**).\
                            \nReason: `{AFKREASON}`")
                    else:
                        await sender.reply(f"**I'm not available right now.** (Since **{afk_since}**).\
                        \n**Please come back later**")
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
                else:
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
Пример #51
0
from bs4 import BeautifulSoup
from datetime import datetime
import pytz

req = requests.get('https://osam.com/Commentary')
soup = BeautifulSoup(req.content, 'html.parser')
table = soup.select('a[href$=\".pdf\"]')

data = [{"turl": "https://osam.com" + x['href']} for x in table]
scraperwiki.sqlite.save(unique_keys=['turl'], data=data)

import datetime
import math
import re
dta = [
    datetime.datetime.now() + datetime.timedelta(days=x)
    for x in range(-10, 0)
]
dta = [x.weekday() for x in dta if x.weekday() < 5]
dta = [
    "http://www.qhdb.com.cn/Newspaper/PageNavigate.aspx?nid=" +
    str(math.floor(x / 7) * 5 + x % 7 + 2413) for x in dta
]
for dtb in dta[0:1]:
    # print(dtb)
    req = requests.get(dtb)
    soup = BeautifulSoup(req.content, 'html.parser')
    table = soup.select('.p_b_list2')
    # print(table)

    table = soup.find_all("span", class_="float")
Пример #52
0
 def get(self, request, pk, format=None):
     response = []
     response_count = {}
     # page_num = request.data['page']
     filter = request.GET.get('time')
     page_num = pk
     questions = es.search(index=index,
                           body={
                               "size": 10000,
                               "query": {
                                   "match": {
                                       "Category": "TEF"
                                   }
                               }
                           })["hits"]["hits"]
     count = es.search(index=index,
                       body={
                           "size": 10000,
                           "query": {
                               "match": {
                                   "Category": "TEF"
                               }
                           }
                       })["hits"]["total"]
     if filter == '24 Hours':
         time = datetime.datetime.now() - datetime.timedelta(days=1)
         print(time)
         questions = es.search(
             index=index,
             body={
                 "size": 10000,
                 "query": {
                     "bool": {
                         "must": [{
                             "match": {
                                 "Category": "TEF"
                             }
                         }, {
                             "range": {
                                 "Timestamp": {
                                     "gte": time,
                                     "lte": datetime.datetime.now()
                                 }
                             }
                         }]
                     }
                 }
             })["hits"]["hits"]
         count = es.search(index=index,
                           body={
                               "size": 10000,
                               "query": {
                                   "bool": {
                                       "must": [{
                                           "match": {
                                               "Category": "TEF"
                                           }
                                       }, {
                                           "range": {
                                               "Timestamp": {
                                                   "gte":
                                                   time,
                                                   "lte":
                                                   datetime.datetime.now()
                                               }
                                           }
                                       }]
                                   }
                               }
                           })["hits"]["total"]
     elif filter == 'past week':
         time = datetime.datetime.now() - datetime.timedelta(days=7)
         print(time)
         questions = es.search(
             index=index,
             body={
                 "size": 10000,
                 "query": {
                     "bool": {
                         "must": [{
                             "match": {
                                 "Category": "TEF"
                             }
                         }, {
                             "range": {
                                 "Timestamp": {
                                     "gte": time,
                                     "lte": datetime.datetime.now()
                                 }
                             }
                         }]
                     }
                 }
             })["hits"]["hits"]
         count = es.search(index=index,
                           body={
                               "size": 10000,
                               "query": {
                                   "bool": {
                                       "must": [{
                                           "match": {
                                               "Category": "TEF"
                                           }
                                       }, {
                                           "range": {
                                               "Timestamp": {
                                                   "gte":
                                                   time,
                                                   "lte":
                                                   datetime.datetime.now()
                                               }
                                           }
                                       }]
                                   }
                               }
                           })["hits"]["total"]
     elif filter == 'past month':
         time = datetime.datetime.now() - datetime.timedelta(days=30)
         print(time)
         questions = es.search(
             index=index,
             body={
                 "size": 10000,
                 "query": {
                     "bool": {
                         "must": [{
                             "match": {
                                 "Category": "TEF"
                             }
                         }, {
                             "range": {
                                 "Timestamp": {
                                     "gte": time,
                                     "lte": datetime.datetime.now()
                                 }
                             }
                         }]
                     }
                 }
             })["hits"]["hits"]
         count = es.search(index=index,
                           body={
                               "size": 10000,
                               "query": {
                                   "bool": {
                                       "must": [{
                                           "match": {
                                               "Category": "TEF"
                                           }
                                       }, {
                                           "range": {
                                               "Timestamp": {
                                                   "gte":
                                                   time,
                                                   "lte":
                                                   datetime.datetime.now()
                                               }
                                           }
                                       }]
                                   }
                               }
                           })["hits"]["total"]
     if page_num <= (count / 10) + 1:
         paginator = Paginator(questions, 10)
         page = paginator.page(page_num)
         response_count["Count"] = count
         for i in page.object_list:
             data = {}
             data["Question"] = i["_source"]["Question"]
             data["Answers"] = i["_source"]["Answers"]
             data["pkid"] = i["_source"]["pkid"]
             data["AskedBy"] = i["_source"]["Sender"]
             data["Category"] = i["_source"]["Category"]
             response.append(data)
         response_count["Response"] = response
         return Response(response_count)
     return Response([])
Пример #53
0
async def mention_afk(mention):
    """This function takes care of notifying the people who mention you that you are AFK."""
    global COUNT_MSG
    global USERS
    global ISAFK
    global afk_time
    global afk_start
    global afk_end
    not_afk = datetime.now()
    afk_end = not_afk.replace(microsecond=0)
    if ISAFK and mention.message.mentioned:
        now = datetime.now()
        afk_since = now - afk_time
        day = float(afk_since.seconds) // (24 * 3600)
        time = float(afk_since.seconds) % (24 * 3600)
        hours = time // 3600
        time %= 3600
        minutes = time // 60
        time %= 60
        seconds = time
        if day == 1:
            afk_str = "Yesterday"
        elif day > 1:
            if day > 6:
                date = now + datetime.timedelta(
                    days=-day, hours=-hours, minutes=-minutes)
                afk_str = date.strftime("%A, %Y %B %m, %H:%I")
            else:
                wday = now + datetime.timedelta(days=-day)
                afk_str = wday.strftime("%A")
        elif hours > 1:
            afk_str = f"`{int(hours)}h{int(minutes)}m` ago"
        elif minutes > 0:
            afk_str = f"`{int(minutes)}m{int(seconds)}s` ago"
        else:
            afk_str = f"`{int(seconds)}s` ago"

        is_bot = False
        if sender := await mention.get_sender():
            is_bot = sender.bot
            if is_bot:
                return  # ignore bot

        chat_obj = await mention.client.get_entity(mention.chat_id)
        chat_title = chat_obj.title

        if mention.sender_id not in USERS or chat_title not in USERS:
            if AFKREASON:
                await mention.reply(f"**SABAR LAGI AFK!!.** (Since: {afk_str})"
                                    f"\nReason: `{AFKREASON}`.")
            else:
                await mention.reply(f"**SABAR LAGI AFK!!.** (Since: {afk_str})"
                                    "\n**Tunggu ya kawan.**")
            if mention.sender_id is not None:
                USERS.update({mention.sender_id: 1})
            else:
                USERS.update({chat_title: 1})
        else:
            if USERS[mention.sender_id] % randint(2, 4) == 0:
                if AFKREASON:
                    await mention.reply(
                        f"**SABAR LAGI AFK!!.** (Since: {afk_str})"
                        f"\nReason: `{AFKREASON}`.")
                else:
                    await mention.reply(
                        f"**SABAR LAGI AFK!!.** (Since: {afk_str})"
                        "\n**Tunggu ya kawan.**")
            if mention.sender_id is not None:
                USERS[mention.sender_id] += 1
            else:
                USERS[chat_title] += 1
        COUNT_MSG += 1
Пример #54
0
async def mention_afk(mention):
    """ This function takes care of notifying the people who mention you that you are AFK."""
    global COUNT_MSG
    global USERS
    global ISAFK
    global USER_AFK  # pylint:disable=E0602
    global afk_time  # pylint:disable=E0602
    global afk_start
    global afk_end
    user = await bot.get_me()  # pylint:disable=E0602
    back_alivee = datetime.now()
    afk_end = back_alivee.replace(microsecond=0)
    afk_since = "**Terakhir Aktif**"
    if mention.message.mentioned and not (await mention.get_sender()).bot:
        if ISAFK:
            now = datetime.now()
            datime_since_afk = now - afk_time  # pylint:disable=E0602
            time = float(datime_since_afk.seconds)
            days = time // (24 * 3600)
            time = time % (24 * 3600)
            hours = time // 3600
            time %= 3600
            minutes = time // 60
            time %= 60
            seconds = time
            if days == 1:
                afk_since = "**Kemarin**"
            elif days > 1:
                if days > 6:
                    date = now + \
                        datetime.timedelta(
                            days=-days, hours=-hours, minutes=-minutes)
                    afk_since = date.strftime("%A, %Y %B %m, %H:%I")
                else:
                    wday = now + datetime.timedelta(days=-days)
                    afk_since = wday.strftime('%A')
            elif hours > 1:
                afk_since = f"`{int(hours)}h {int(minutes)}m`"
            elif minutes > 0:
                afk_since = f"`{int(minutes)}m {int(seconds)}s`"
            else:
                afk_since = f"`{int(seconds)}s`"
            if mention.sender_id not in USERS:
                if AFKREASON:
                    await mention.reply(f"**┌ ❏ AFK .**\
                        \n**│ ├ USER : {ALIVE_NAME}**\
                        \n**│ ├ SEJAK : {afk_since} Ago.**\
                        \n**└ └ ALASAN:** `{AFKREASON}`")
                else:
                    await mention.reply(str(choice(AFKSTR)))
                USERS.update({mention.sender_id: 1})
                COUNT_MSG = COUNT_MSG + 1
            elif mention.sender_id in USERS:
                if USERS[mention.sender_id] % randint(2, 4) == 0:
                    if AFKREASON:
                        await mention.reply(f"**┌ ❏ AFK .**\
                        \n**│ ├ USER : {ALIVE_NAME}**\
                        \n**│ ├ SEJAK : {afk_since} Ago.**\
                        \n**└ └ ALASAN:** `{AFKREASON}`")
                    else:
                        await mention.reply(str(choice(AFKSTR)))
                    USERS[mention.sender_id] = USERS[mention.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
                else:
                    USERS[mention.sender_id] = USERS[mention.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
Пример #55
0
def point_of_timeout():
    delta = datetime.timedelta(seconds=LOCK_TIMEOUT)
    return datetime.datetime.now() - delta
Пример #56
0
async def afk_on_pm(sender):
    """ Function which informs people that you are AFK in PM """
    global ISAFK
    global USERS
    global COUNT_MSG
    global COUNT_MSG
    global USERS
    global ISAFK
    global USER_AFK  # pylint:disable=E0602
    global afk_time  # pylint:disable=E0602
    global afk_start
    global afk_end
    user = await bot.get_me()  # pylint:disable=E0602
    back_alivee = datetime.now()
    afk_end = back_alivee.replace(microsecond=0)
    afk_since = "**Belum Lama**"
    if sender.is_private and sender.sender_id != 777000 and not (
            await sender.get_sender()).bot:
        if PM_AUTO_BAN:
            try:
                from userbot.modules.sql_helper.pm_permit_sql import is_approved
                apprv = is_approved(sender.sender_id)
            except AttributeError:
                apprv = True
        else:
            apprv = True
        if apprv and ISAFK:
            now = datetime.now()
            datime_since_afk = now - afk_time  # pylint:disable=E0602
            time = float(datime_since_afk.seconds)
            days = time // (24 * 3600)
            time = time % (24 * 3600)
            hours = time // 3600
            time %= 3600
            minutes = time // 60
            time %= 60
            seconds = time
            if days == 1:
                afk_since = "**Kemarin**"
            elif days > 1:
                if days > 6:
                    date = now + \
                        datetime.timedelta(
                            days=-days, hours=-hours, minutes=-minutes)
                    afk_since = date.strftime("%A, %Y %B %m, %H:%I")
                else:
                    wday = now + datetime.timedelta(days=-days)
                    afk_since = wday.strftime('%A')
            elif hours > 1:
                afk_since = f"`{int(hours)}h {int(minutes)}m`"
            elif minutes > 0:
                afk_since = f"`{int(minutes)}m {int(seconds)}s`"
            else:
                afk_since = f"`{int(seconds)}s`"
            if sender.sender_id not in USERS:
                if AFKREASON:
                    await sender.reply(f"**┌ ❏ AFK .**\
                        \n**│ ├ USER : {ALIVE_NAME}**\
                        \n**│ ├ SEJAK : {afk_since} Ago.**\
                        \n**└ └ ALASAN:** `{AFKREASON}`")
                else:
                    await sender.reply(str(choice(AFKSTR)))
                USERS.update({sender.sender_id: 1})
                COUNT_MSG = COUNT_MSG + 1
            elif apprv and sender.sender_id in USERS:
                if USERS[sender.sender_id] % randint(2, 4) == 0:
                    if AFKREASON:
                        await sender.reply(f"**┌ ❏ AFK .**\
                        \n**│ ├ USER : {ALIVE_NAME}**\
                        \n**│ ├ SEJAK : {afk_since} Ago.**\
                        \n**└ └ ALASAN:** `{AFKREASON}`")
                    else:
                        await sender.reply(str(choice(AFKSTR)))
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
                else:
                    USERS[sender.sender_id] = USERS[sender.sender_id] + 1
                    COUNT_MSG = COUNT_MSG + 1
Пример #57
0
def timedelta(process_delay_latency):
    import datetime
    return datetime.timedelta(seconds=(process_delay_latency) / 2)
Пример #58
0
def addOneSecond(actual_time):
    import datetime
    return actual_time + datetime.timedelta(0, 1)
Пример #59
0
    ##MongoDB or DYNAMO DB - json docs

##ORM - object relational mappers



if __name__ == "__main__":
    headers = {
        "Accept" : "application/json",
        "Content-Type" : "application/json",
        "Authorization" : "Bearer {token}".format(token = TOKEN)

    }

    today = datetime.datetime.now()
    yesterday = today - datetime.timedelta(days=1)
    yesterday_unix_timestamp = int(yesterday.timestamp()) * 1000

    r = requests.get("https://api.spotify.com/v1/me/player/recently-played?after={time}".format(time=yesterday_unix_timestamp), headers = headers)

    data = r.json()

    song_titles = []
    artists = []
    played_at_list = [] 
    timestamps = []

    for song in data["items"]:
        song_titles.append(song["track"]["name"])
        artists.append(song["track"]["album"]["artists"][0]["name"])
        played_at_list.append(song["played_at"])
Пример #60
0
    def work(self, file3):
        if True:
            games = []
            print 'Formatting matches into series.'
            matches_py_date = {}
            matches = 0
            '''
			Read test data into simple dictionary for later use.
			'''
            with open(file3, 'rb') as f:
                for row in csv.reader(f, delimiter=';'):
                    if row[0] not in matches_py_date:
                        matches_py_date[row[0]] = []
                    matches_py_date[row[0]].append(row)
            '''
			Read dictionary values key by key sorted by date. Remove item when read.
			'''
            for item in sorted(matches_py_date.keys()):
                region = ''
                while len(matches_py_date[item]) > 0:
                    for match in matches_py_date[item]:
                        if match[1] == '1-0:blue':
                            score = ['1-0']
                            blue_red = ['blue']
                        else:
                            score = ['0-1']
                            blue_red = ['red']
                        date = match[0]
                        home = match[2]
                        away = match[3]
                        BO = match[7]
                        home_team = [match[4]]
                        away_team = [match[5]]
                        league = match[9]
                        blue_side = [match[2]]
                        try:
                            region = match[8].replace(' ', '')
                        except IndexError:
                            print 'ff error'
                        tiedot = [
                            date, BO, '', '', '', home, away, score, home_team,
                            away_team, region, league, blue_side
                        ]
                        break
                    matches_py_date[item].remove(match)
                    deletes = []
                    a = True
                    while a == True:
                        for match2 in matches_py_date[item]:
                            if match2[2].lower() == home.lower(
                            ) and match2[3].lower() == away.lower():
                                home_team.append(match2[4])
                                away_team.append(match2[5])
                                blue_side.append(match[2])
                                if match2[1] == '1-0:blue':
                                    score.append('1-0')
                                    blue_red.append('blue')
                                else:
                                    score.append('0-1')
                                    blue_red.append('red')
                                deletes.append(match2)
                            elif match2[3].lower() == home.lower(
                            ) and match2[2].lower() == away.lower():
                                home_team.append(match2[5])
                                away_team.append(match2[4])
                                blue_side.append(match[3])
                                if match2[1] == '0-1:red':
                                    score.append('1-0')
                                    blue_red.append('red')
                                else:
                                    score.append('0-1')
                                    blue_red.append('blue')
                                deletes.append(match2)
                        a = False
                        if deletes != []:
                            for match in deletes:
                                matches_py_date[item].remove(match)
                        matches += 1

                        if (league == 'NA LCS Summer 2018'):
                            date = date[:4] + '/' + str(int(
                                date[4:6])) + '/' + str(int(date[6:8]))
                            date_1 = datetime.datetime.strptime(
                                date, "%Y/%m/%d")
                            date = date_1 + datetime.timedelta(days=1)
                            date = date.strftime("%Y%m%d")

                        tiedot = [
                            date, BO, '', '', '', home, away, score, home_team,
                            away_team, region, blue_red, league, blue_side
                        ]
                        tiedot = Scraper().Format_match_data(tiedot)
                        games.append([tiedot])
            '''
			Ramove all possible dublicates
			'''
            new_list = []
            for i in games:
                if i not in new_list:
                    new_list.append(i)
            games = new_list

            print '{} series found.'.format(len(games))
            print 'Writing all into single file.'

            with open(file3, 'wb') as f:
                writer = csv.writer(f, delimiter=';')
                for match in games:
                    writer.writerow(match[0])
            f.close()