Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def format_date(userdate):
date = dateparser.parse(userdate)
try:
return datetime.datetime.strftime(date, "%Y-%m-%d")
except TypeError:
return None
g.raw_poly_mask = parr
g.poly_mask = Polygon(parr)
if g.args['find']:
g.template = cv2.imread(g.args['find'])
if g.args['resize']:
rh, rw, rl = g.template.shape
g.template = cv2.resize(g.template, (int(rw*resize), int(rh*resize)))
g.template = cv2.cvtColor(g.template, cv2.COLOR_BGR2GRAY)
# if either from or to is specified, populate both ranges
# if not, leave it as None
if g.args['to'] or g.args['from']:
if g.args['to']:
to_time = dateparser.parse(g.args['to'])
else:
to_time= datetime.now()
if g.args['from']:
from_time = dateparser.parse(g.args['from'])
print (from_time)
else:
from_time = to_time - timedelta(hours = 1)
g.args['from']= from_time.strftime('%Y-%m-%d %H:%M:%S')
g.args['to'] = to_time.strftime('%Y-%m-%d %H:%M:%S')
if (to_time <= from_time):
fail_print ('ERROR: Time range from:{} to:{} is invalid '.format(from_time, to_time))
exit(1)
if g.args['monitors']:
category = doc.find('.//div[@class="details"]//a')
if category is None:
return
if category.get('href').split('/')[-1] not in CATEGORIES:
return
meta = self.metadata()
meta.languages = ['de']
meta.add_country('li')
meta.extension = 'pdf'
meta.mime_type = 'application/pdf'
meta.foreign_id = notice_id
meta.title = doc.findtext('.//div[@class="body"]//h3')
date = doc.findtext('.//div[@class="details"]/span[@class="datum"]')
meta.add_date(dateparser.parse(date))
fh, file_path = mkstemp(suffix='.pdf')
try:
res = sess.get(PRINT_URL, stream=True)
fh = os.fdopen(fh, 'w')
for chunk in res.iter_content(chunk_size=1024):
if chunk:
fh.write(chunk)
fh.close()
log.info("Importing %r to %r", meta.title, source)
self.emit_file(source, meta, file_path, move=True)
except Exception as ex:
log.exception(ex)
finally:
if os.path.isfile(file_path):
os.unlink(file_path)
def fixup_dates(title, datetime):
result = {'datetime': datetime, 'title': title}
date_re = "\d{2,4}\W\d{1,2}(\W\d{1,2})?"
date_matcher = re.compile(date_re)
date_matcher_post_space = re.compile(date_re + "\s*")
m = re.match(date_matcher, title)
if m is not None:
d = m.group(0)
dparse = parse(d, settings={'PREFER_DAY_OF_MONTH': 'first'})
if dparse is not None:
dfixed = datetime.combine(dparse, datetime.min.time())
result['datetime'] = dfixed
m = re.sub(date_matcher_post_space, '', title)
if m is not None:
result['title']=m
return result
def date_to_milliseconds(date_str):
"""Convert UTC date to milliseconds
If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"
See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/
:param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
:type date_str: str
"""
# get epoch value in UTC
epoch = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
# parse our date string
d = dateparser.parse(date_str)
# if the date is not timezone aware apply UTC timezone
if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
d = d.replace(tzinfo=pytz.utc)
# return the difference in time
return int((d - epoch).total_seconds() * 1000.0)
def get_weekday(dateVal):
# Converts to format 2004-01-05
dt = dp.parse(dateVal)
return dt.weekday()
def convert_date(dateVal):
# Converts to format 2004-01-05
dt = dp.parse(dateVal)
return dt.date()
t_until = request.GET.get('until')
hostname = request.GET.get('hostname')
application = request.GET.get('application')
date_settings = {'TIMEZONE': settings.TIME_ZONE}
try:
object_id = request.GET.get('object_id') if not hostname else Server.objects.get(hostname=hostname).server_id
except ObjectDoesNotExist:
messages.error(request, 'Server does not exist')
return TemplateResponse(request, 'serverdb/changes.html', {})
if t_from:
column_filter['change_on__gt'] = dateparser.parse(t_from, settings=date_settings)
context['from_understood'] = column_filter['change_on__gt']
if t_until:
column_filter['change_on__lt'] = dateparser.parse(t_until, settings=date_settings)
context['until_understood'] = column_filter['change_on__lt']
if object_id:
q_filter.append((
Q(changeupdate__server_id=object_id) |
Q(changedelete__server_id=object_id) |
Q(changeadd__server_id=object_id)
))
if application:
q_filter.append((
Q(app__name=application) | Q(user__username=application)
))
commits = ChangeCommit.objects.filter(*q_filter, **column_filter).order_by('-change_on')
paginator = Paginator(commits, 20)
try:
or a relative expression i.e. `3m` for three minutes, `5d` for 5 days.
Even `3 minutes` or `5 days` will work.
See https://dateparser.readthedocs.io/en/latest/#features for examples.
"""
import dateparser
ctx.auto_output("table")
settings = {
'DATE_ORDER': 'DMY',
'TIMEZONE': 'UTC',
'RETURN_AS_TIMEZONE_AWARE': True,
}
start_time = dateparser.parse(since, settings=settings)
end_time = dateparser.parse(end, settings=settings)
delta = end_time - start_time
if ctx.verbose:
click.echo(
'Querying from {}:{} to {}:{} a span of {}'.format(
since, start_time.isoformat(), end, end_time.isoformat(), delta
)
)
data = api.get_history(ctx, list(entities), start_time, end_time)
result = [] # type: List[Dict[str, Any]]
entitycount = 0
for item in data:
result.extend(item) # type: ignore
# Get category
vid_category = video_card.find(class_="card-category")
category = ""
if vid_category:
category = vid_category.get_text()
# Get time since video added
vid_time_added = video_card.find(class_="card-time")
if vid_time_added is None:
continue
time_since_added = str(vid_time_added.get_text())
time_since_added_date = dateparser.parse(time_since_added)
# If error occur while parsing date, skip
# TODO: handle case where date malformed (special string field)
if not time_since_added_date:
continue
if not _is_recent(time_since_added_date, max_days_ago):
continue
highlights.append(FootyroomVideoHighlight(video_link, match_name, img_link, view_count, category, time_since_added))
return highlights