Converting date and time from shell script into python - python

I am a beginner to python and I have this date and time code from shell script, and how do I make it into python, also how can I take date from last month till now
ToDay=`date "+%Y%m%d"`
CheckDATE=`date "+%Y-%m-%d" --date '1 day ago'`
NOWDATE=`date "+%Y-%m-%d"`
This is the python code
from datetime import datetime, date, timedelta
progToRun = 'python ' + ScriptDir + '/bin/panafapi.py -K ' + secretkey + ' --samples -j -r "{\\"query\\":{\\"operator\\":\\"all\\",\\"children\\":[{\\"field\\":\\"alias.ip_address\\",\\"operator\\":\\"contains\\",\\"value\\":\\"' + ResultFile + '\\"},{\\"operator\\":\\"any\\",\\"children\\":[{\\"field\\":\\"sample.update_date\\",\\"operator\\":\\"is in the range\\",\\"value\\":[\\"' + CheckDATE + 'T00:00:00\\",\\"' + NOWDATE + 'T23:59:59\\"]},{\\"field\\":\\"sample.create_date\\",\\"operator\\":\\"is in the range\\",\\"value\\":[\\"' + CheckDATE + 'T00:00:00\\",\\"' + NOWDATE + 'T23:59:59\\"]},{\\"operator\\":\\"any\\",\\"children\\":[{\\"field\\":\\"sample.malware\\",\\"operator\\":\\"is\\",\\"value\\":1},{\\"field\\":\\"sample.malware\\",\\"operator\\":\\"is\\",\\"value\\":4}]}]}]},\\"scope\\":\\"global\\",\\"size\\":1,\\"from\\":0,\\"sort\\":{\\"create_date\\":{\\"order\\":\\"desc\\"}}}" > ' + ResultDir + 'srciplist-' + ToDay + '.json'
ToDay = datetime.now().strftime('%Y%m%d')
CheckDATE = datetime.strptime("2017-12-01", "%Y-%m-%d").date()
NOWDATE = datetime.now().date()
Error:
progToRun = 'python ' + ScriptDir + '/bin/panafapi.py -K ' + secretkey + ' --samples -j -r "{\\"query\\":{\\"operator\\":\\"all\\",\\"children\\":[{\\"field\\":\\"alias.ip_address\\",\\"operator\\":\\"contains\\",\\"value\\":\\"' + ResultFile + '\\"},{\\"operator\\":\\"any\\",\\"children\\":[{\\"field\\":\\"sample.update_date\\",\\"operator\\":\\"is in the range\\",\\"value\\":[\\"' + CheckDATE + 'T00:00:00\\",\\"' + NOWDATE + 'T23:59:59\\"]},{\\"field\\":\\"sample.create_date\\",\\"operator\\":\\"is in the range\\",\\"value\\":[\\"' + CheckDATE + 'T00:00:00\\",\\"' + NOWDATE + 'T23:59:59\\"]},{\\"operator\\":\\"any\\",\\"children\\":[{\\"field\\":\\"sample.malware\\",\\"operator\\":\\"is\\",\\"value\\":1},{\\"field\\":\\"sample.malware\\",\\"operator\\":\\"is\\",\\"value\\":4}]}]}]},\\"scope\\":\\"global\\",\\"size\\":1,\\"from\\":0,\\"sort\\":{\\"create_date\\":{\\"order\\":\\"desc\\"}}}" > ' + ResultDir + 'srciplist-' + ToDay + '.json'
TypeError: must be str, not datetime.date

>>> import datetime
>>> today = datetime.date.today()
>>> yesterday = today - datetime.timedelta(days=1)
>>> ToDay = today.strftime("%Y%m%d")
>>> CheckDATE = yesterday.strftime("%Y-%m-%d")
>>> NOWDATE = today.strftime("%Y-%m-%d")
>>> print ToDay
20180126
>>> print CheckDATE
2018-01-25
>>> print NOWDATE
2018-01-26

You can convert date using the following code:
CheckDATE = datetime.datetime.strptime("2017-12-01", "%Y-%m-%d").date()
ToDAY = datetime.datetime.now().strftime("%Y-%m-%d")
CheckDATE = CheckDATE.strftime("%Y-%m-%d")
Adding date at the end only outputs the date part and omits time.
I hope this answers your question.
Edit: Updated based on additional information.

Related

Exif date taken for multiple photos - Python

My Exif code failed in my project so the photos came out without a "date taken" field. Luckly ive saved them with the date taken as the tittle eg. "-Forward14-07-2021-08-23-25.jpg". I have thousands of photos like this. I can add that individually using this python code.
from datetime import datetime
import piexif
s = "13"
m = "17"
h = "09"
dd = "14"
mm = "07"
yyyy = "2021"
name = "-Forward"
filename = str(name) + str(dd) + "-" + str(mm) + "-" + str(yyyy) + "-" + str(h) + "-" + str(m) + "-" + str(s) + ".jpg"
exif_dict = piexif.load(filename)
new_date = datetime(int(yyyy), int(mm), int(dd), int(h), int(m),
int(s)).strftime("%Y:%m:%d %H:%M:%S")
exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date
exifif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)
As soon as I try it as a loop it does not work.
import PIL.Image
import piexif
from datetime import datetime
from PIL import Image
#import os
#do not change these:
s = "0"
m = "0"
h = "07"
#change these:
dd = "14"
mm = "07"
yyyy = "2021"
Dir = "D:\Python codes\test" #insert file directory here##
#run code twice, once for pavement and once for forward photos
name = "-Forward" #"-Pavement"
#for filename in os.listdir(dir):
for h in range (7,17):
for m in range (1,61):
for s in range (1,61):
if (int(s) < 10):
s = "0" + str(s)
if (int(m) < 10):
m = "0" + str(m)
if (int(h) < 10):
h = "0" + str(h)
else:
s = int(s)
m = int(m)
h = int(h)
try:
filename = str(name) + str(dd) + "-" + str(mm) + "-" + str(yyyy) + "-" + str(h) + "-" + str(m) + "-" + str(s) + ".jpg"
exif_dict = piexif.load(filename)
new_date = datetime(int(yyyy), int(mm), int(dd), int(h), int(m), int(s)).strftime("%Y:%m:%d %H:%M:%S")
exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)
##insert right directory
# im = Image.open(Dir + name + str(dd) + "-" + str(mm) + "-" + str(yyyy) + "-" + str(h) + "-" +str(m) + "-" +str(s))
#im.save(Dir + name + str(dd) + "-" + str(mm) + "-" + str(yyyy) + "-" + str(h) + "-" +str(m) + "-" +str(s), exif=exif_bytes, quality="keep", optimize=True)
s = int(s) + 1
except:
s = int(s) + 1
m = int(m) + 1
h = int(h) + 1
I've done some more research and found out that the try: and except: were the problem. The working code can be seen below for those who are interested. Thanks for the help!
import PIL.Image
from os import path
import piexif
from datetime import datetime
from PIL import Image
import os
# do not change these:
s = 0
M = 0
h = 7
# change these:
dd = "14"
mm = "07"
yyyy = "2021"
# run code twice, once for pavement and once for forward photos
name = "-Forward" # "-Pavement"
# for filename in os.listdir(dir):
for a in range(7, 17):
M = 0
for b in range(1, 61):
s = 0
for c in range(1, 61):
ss = str(s)
MM = str(M)
hh = str(h)
if s < 10:
ss = "0" + str(s)
if M < 10:
MM = "0" + str(M)
if h < 10:
hh = "0" + str(h)
filename = str(name) + str(dd) + "-" + str(mm) + "-" + str(yyyy) + "-" + str(hh) + "-" + str(
MM) + "-" + str(ss) + ".jpg"
#try:
if path.exists(filename):
# filename = str(name) + str(dd) + "-" + str(mm) + "-" + str(yyyy) + "-" + str(h) + "-" + str(m) +
# "-" + str(s) + ".jpg"
exif_dict = piexif.load(filename)
new_date = datetime(int(yyyy), int(mm), int(dd), h, M, s).strftime("%Y:%m:%d %H:%M:%S")
exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)
# #insert right directory im = Image.open(Dir + name + str(dd) + "-" + str(mm) + "-" + str(yyyy) +
# "-" + str(h) + "-" +str(m) + "-" +str(s)) im.save(Dir + name + str(dd) + "-" + str(mm) + "-" + str(
# yyyy) + "-" + str(h) + "-" +str(m) + "-" +str(s), exif=exif_bytes, quality="keep", optimize=True)
s += 1
#except:
s += 1
M += 1
h += 1

Question about dateutil.relativedelta - Why the ouput is always zero?

Why the output of this relativedelta attribute is also zero?
The data file contains two date time strings, the purpose is to get the time difference between the two.
# python3.6 time_diff.py
0
0
0
0
# cat data
06/21/2019 21:45:24 06/21/2020 21:45:26
06/21/2019 22:42:25 06/22/2020 01:28:41
06/21/2019 22:41:32 06/21/2020 22:42:32
06/20/2019 23:42:25 06/22/2020 02:42:29
# cat time_diff.py
import dateutil.relativedelta, datetime
f = open("data", "r")
for line in f:
t1 = datetime.datetime.strptime(line.split()[0] + " " + line.split()[1], "%m/%d/%Y %H:%M:%S")
t2 = datetime.datetime.strptime(line.split()[0] + " " + line.split()[1], "%m/%d/%Y %H:%M:%S")
rd = dateutil.relativedelta.relativedelta(t1, t2)
print(rd.seconds)
instead of
t1 = datetime.datetime.strptime(line.split()[0] + " " + line.split()[1], "%m/%d/%Y %H:%M:%S")
t2 = datetime.datetime.strptime(line.split()[0] + " " + line.split()[1], "%m/%d/%Y %H:%M:%S")
go with
t1 = datetime.datetime.strptime(line.split()[0] + " " + line.split()[1], "%m/%d/%Y %H:%M:%S")
t2 = datetime.datetime.strptime(line.split()[2] + " " + line.split()[3], "%m/%d/%Y %H:%M:%S")
you are providing wrong input to t2. After splitting the input from the file becomes this ['06/21/2019', '21:45:24', '06/21/2020', '21:45:26'].
So t1 should get input 0 and 1 and t2 should get input 2 and 3.
Below is the updated code:
import dateutil.relativedelta, datetime
f = open("data", "r")
for line in f:
t1 = datetime.datetime.strptime(line.split()[0] + " " + line.split()[1], "%m/%d/%Y %H:%M:%S")
t2 = datetime.datetime.strptime(line.split()[2] + " " + line.split()[3], "%m/%d/%Y %H:%M:%S")
rd = dateutil.relativedelta.relativedelta(t1, t2)
print(t1, t2, rd.seconds)

I have a Pandas code error, but the solution is uncleared

I am a newbie to Python, and I have been dooing this Pandas code for reading malware IP addresses, this is the code and below is the error.
import os
from datetime import datetime, date, timedelta
import subprocess
import pyjq
import pandas as pd
# Initializes the variables for the directories
HomeDir = "Users/kiya/Downloads/"
ScriptDir = HomeDir + "pan-python-0.12.0 2"
ResultDir = HomeDir + "techscroll/"
# Create the dates
ToDay = datetime.now().strftime('%Y%m%d')
# checkDATE = (date.today() - timedelta(1)).strfttime('%Y%m%d')
ResultFile = "Test"
CheckDATE = "Test"
NOWDATE = "TEST"
# Run the panafpi
subprocess.check_output('python ' + ScriptDir + '/bin/panafapi.py -K secretkey --samples -j -r "{\"query\":{\"operator\":\"all\",\"children\":[{\"field\":\"alias.ip_address\",\"operator\":\"contains\",\"value\":\"' + ResultFile + '\"},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.update_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"$' + NOWDATE + 'T23:59:59\"]},{\"field\":\"sample.create_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"' + NOWDATE + 'T23:59:59\"]},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":1},{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":4}]}]}]},\"scope\":\"global\",\"size\":1,\"from\":0,\"sort\":{\"create_date\":{\"order\":\"desc\"}}}" > ' + ResultDir + 'srciplist-' + ToDay + '.json', shell=True)
# Using pyjq to filter
filteredResultData = pyjq.all('.hits[]._source | .create_date + "," + .sha256')
# Save the JSON file to comma-separated file
pd.to_csv(ResultDir + "/srciplist-" + ToDay + ".csv", sep=",")
error:
/bin/sh: Users/kiya/Downloads/techscroll/srciplist-20180125.json: No such file or directory
Traceback (most recent call last):
File "/Users/mani/Downloads/tester (1).py", line 22, in <module>
subprocess.check_output('python ' + ScriptDir + '/bin/panafapi.py -K secretkey --samples -j -r "{\"query\":{\"operator\":\"all\",\"children\":[{\"field\":\"alias.ip_address\",\"operator\":\"contains\",\"value\":\"' + ResultFile + '\"},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.update_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"$' + NOWDATE + 'T23:59:59\"]},{\"field\":\"sample.create_date\",\"operator\":\"is in the range\",\"value\":[\"' + CheckDATE + 'T00:00:00\",\"' + NOWDATE + 'T23:59:59\"]},{\"operator\":\"any\",\"children\":[{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":1},{\"field\":\"sample.malware\",\"operator\":\"is\",\"value\":4}]}]}]},\"scope\":\"global\",\"size\":1,\"from\":0,\"sort\":{\"create_date\":{\"order\":\"desc\"}}}" > ' + ResultDir + 'srciplist-' + ToDay + '.json', shell=True)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'python Users/kiya/Downloads/pan-python-0.12.0 2/bin/panafapi.py -K secret key --samples -j -r "{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"Test"},{"operator":"any","children":[{"field":"sample.update_date","operator":"is in the range","value":["TestT00:00:00","$TESTT23:59:59"]},{"field":"sample.create_date","operator":"is in the range","value":["TestT00:00:00","TESTT23:59:59"]},{"operator":"any","children":[{"field":"sample.malware","operator":"is","value":1},{"field":"sample.malware","operator":"is","value":4}]}]}]},"scope":"global","size":1,"from":0,"sort":{"create_date":{"order":"desc"}}}" > Users/kiya/Downloads/techscroll/srciplist-20180125.json' returned non-zero exit status 1.

Getting easygui to self update info

Trying to get a self updating speedometer and clock working for my truck using gps. So far I have been able to get the read out that I want using easygui and msgbox but it is not self updating which will not help much on either application. Below is the code. Any help would be much appreciated, I know this is pretty ugly and probably not correct but I am new to python.
import gps
from easygui import *
import sys
# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
while True:
try:
report = session.next()
if report['class'] == 'TPV':
if hasattr(report, 'time'):
hour = int(report.time[11:13])
hourfix = hour - 7
if hourfix < 12:
time = 'Current Time Is: ' + report.time[5:7] + '/' + report.time[8:10] + '/' + report.time[0:4] + ' ' + str(hourfix) + report.time[13:19] + ' am'
else:
hourfix = hourfix - 12
time = 'Current Time Is: ' + report.time[5:7] + '/' + report.time[8:10] + '/' + report.time[0:4] + ' ' + str(hourfix) + report.time[13:19] + ' pm'
if report['class'] == 'TPV':
if hasattr(report, 'speed'):
speed = int(report.speed * gps.MPS_TO_MPH)
strspeed = str(speed)
currentspeed = 'Current Speed Is: ' + strspeed + ' MPH'
msgbox(time + "\n" + currentspeed, "SPEEDO by Jono")
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None
print "GPSD has terminated"

invalid syntax: None, line 4, pos 72

I can't make a space between year and hour, and I am not allowed to use ",". I have use concatenation.
from datetime import datetime
now = datetime.now()
print str(now.month) + "/" + str(now.day) + "/" + str(now.year), "+ "str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
Try avoiding such long statements and try format them in a readable way. You have wrongly put the + operator within "" and I believe the , should be printed as a separator between month/day/year and hour:minute:second
print str(now.month) + "/" +
str(now.day) + "/" +
str(now.year) + " " +
str(now.hour) + ":" +
str(now.minute) + ":" +
str(now.second)
+ str(now.year), "+ "str(now.hour) + ":"
Your + is in quotes and you have a comma after str(now.year)
now.strftime('%m/%d/%Y + %H:%M:%S')
type this command after 'now = datetime.now()' and you will get the output you want.

Categories

Resources