Archive
subtract a day from a python date
Problem
When working with dates, I prefer the order yyyy-mm-dd (e.g. 2015-07-10). Its main advantage is that if you use it as a prefix and sort your entries, you get them in chronological order. (Another advantage is that in my home country we use this order, so this is much closer to my thinking).
So, I faced the following problem: having a date as a string (“2015-07-10”), calculate the date one day before and produce a string again (“2015-07-09”).
Solution
>>> from datetime import datetime, timedelta
>>> date = "2015-07-10"
>>> today = datetime.strptime(date, '%Y-%m-%d')
>>> today
datetime.datetime(2015, 7, 10, 0, 0)
>>> yesterday = today - timedelta(days=1)
>>> yesterday
datetime.datetime(2015, 7, 9, 0, 0)
>>> yesterday.strftime('%Y-%m-%d')
'2015-07-09'
Links
- http://stackoverflow.com/questions/466345/converting-string-into-datetime
- http://stackoverflow.com/questions/441147/how-can-i-subtract-a-day-from-a-python-date
Remark
Back to dates: if I need to write a date in English, I write it like this: “May 12, 1984”. Avoid “05-12-1984”, because what is it? May 12? Or December 5? God knows only.
The News Television Project (HírTV)
In this post I describe how to watch news on a Hungarian site. Although the video that we want to play is in Hungarian, you might get some ideas that you can use in a different project.
Project description
Currently I live abroad and sometimes I want to watch news in my mother tongue. So, the Hungarian News Television (HírTV) collects its news programs at http://www.hirtv.hu/view/videoview/hirado . Here, a video has the following URL: http://www.hirtv.net/filmek/hirado21/hiradoYYYYMMDD.wmv , where YYYYMMDD is the date (for instance http://www.hirtv.net/filmek/hirado21/hirado20101018.wmv). Instead of starting a web browser, visiting this page and clicking on a link, I want to launch the news video with a Python script.
Difficulty
When the script is executed, it may be possible that the news of the current day is not yet uploaded. So we need to verify if the URL exists. However, if we want to get a WMV file that doesn’t exist, the web server of HirTv will return an HTML page instead of indicating that the given URL is missing. So we will have to verify the Content-Type of the URLs. If it’s text/html => error, if it’s video/x-ms-wmv => OK.
Solution
#!/usr/bin/env python
import datetime
import urllib
import os
WMV = 'video/x-ms-wmv'
base = 'http://www.hirtv.net/filmek/hirado21/hirado'
ext = '.wmv'
def get_content_type(url):
d = urllib.urlopen(url)
return d.info()['Content-Type']
def date_to_str(d):
return "%d%02d%02d" % d
def prettify(d):
return "%d-%02d-%02d" % d
def play_video(video_url):
print "> " + video_url
command = 'mplayer %s 1>/dev/null 2>&1' % video_url
#command = 'vlc %s 1>/dev/null 2>&1' % video_url # if you prefer VLC
os.system(command)
today = datetime.date.today().timetuple()[:3]
video_today = base + date_to_str(today) + ext
if get_content_type(video_today) == WMV:
play_video(video_today)
else:
yesterday = (datetime.date.today() - datetime.timedelta(days = 1)).timetuple()[:3]
video_yesterday = base + date_to_str(yesterday) + ext
print "The video for today (%s) is not available." % prettify(today)
val = raw_input( "Do you want to watch the video of yesterday (%s) [y/n]? " % prettify(yesterday) )
if val == "y":
if get_content_type(video_yesterday) == WMV:
play_video(video_yesterday)
else:
print "Sorry. The video of yesterday (%s) is not available either." % prettify(yesterday)
First we determine the today’s date and using this information we create a URL for the video file. If it really exists (i.e. the Content-Type is correct), then we play it calling mplayer. If the Content-Type is incorrect, then the video of today was not yet uploaded. In this case we offer the user to play the video of yesterday.
Update (20101107): A bug in date_to_str() and prettify() was corrected. Months and days must be padded with 0s, i.e. 6 must become 06 for instance. VLC support is also added, it’s put in comment.
