Skip to content

Commit

Permalink
Adds dates to HTTP directories.
Browse files Browse the repository at this point in the history
This commit adds dates to directories and files listed from an HTTP
source and thus enables the user to sort them by date. It has been made
with Apache and Nginx autoindex modules in mind. There's also a small
change that enables XBMC to read the filesize from Nginx and it adds
the static method MonthStringToMonthNum() to CDateTime. CHTTPDirectory
uses this method to add dates to files/directories.
  • Loading branch information
dez-dk committed Mar 17, 2012
1 parent 95e613d commit a2e188c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
12 changes: 12 additions & 0 deletions xbmc/XBDateTime.cpp
Expand Up @@ -1286,3 +1286,15 @@ CStdString CDateTime::GetAsRFC1123DateTime() const
result.Format("%s, %02i %s %04i %02i:%02i:%02i GMT", DAY_NAMES[time.GetDayOfWeek()], time.GetDay(), MONTH_NAMES[time.GetMonth()-1], time.GetYear(), time.GetHour(), time.GetMinute(), time.GetSecond());
return result;
}

int CDateTime::MonthStringToMonthNum(const CStdString& month)
{
const char* months[] = {"january","february","march","april","may","june","july","august","september","october","november","december"};
const char* abr_months[] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};

int i = 0;
for (; i < 12 && month.CompareNoCase(months[i]) != 0 && month.CompareNoCase(abr_months[i]) != 0; i++);
i++;

return i;
}
1 change: 1 addition & 0 deletions xbmc/XBDateTime.h
Expand Up @@ -99,6 +99,7 @@ class CDateTime : public IArchivable

static CDateTime GetCurrentDateTime();
static CDateTime GetUTCDateTime();
static int MonthStringToMonthNum(const CStdString& month);

const CDateTime& operator =(const SYSTEMTIME& right);
const CDateTime& operator =(const FILETIME& right);
Expand Down
55 changes: 47 additions & 8 deletions xbmc/filesystem/HTTPDirectory.cpp
Expand Up @@ -54,6 +54,18 @@ bool CHTTPDirectory::GetDirectory(const CStdString& strPath, CFileItemList &item
CRegExp reItem(true); // HTML is case-insensitive
reItem.RegComp("<a href=\"(.*)\">(.*)</a>");

CRegExp reDateTime(true);
reDateTime.RegComp("<td align=\"right\">([0-9]{2})-([A-Z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}) +</td>");

CRegExp reDateTimeNginx(true);
reDateTimeNginx.RegComp("</a> +([0-9]{2})-([A-Z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}) ");

CRegExp reSize(true);
reSize.RegComp(">*([0-9.]+)(B|K|M|G| )</td>");

CRegExp reSizeNginx;
reSizeNginx.RegComp("([0-9]+)$");

/* read response from server into string buffer */
char buffer[MAX_PATH + 1024];
while(http.ReadString(buffer, sizeof(buffer)-1))
Expand Down Expand Up @@ -98,18 +110,32 @@ bool CHTTPDirectory::GetDirectory(const CStdString& strPath, CFileItemList &item
if(URIUtils::HasSlashAtEnd(pItem->GetPath()))
pItem->m_bIsFolder = true;

if (!pItem->m_bIsFolder && g_advancedSettings.m_bHTTPDirectoryStatFilesize)
CStdString day, month, year, hour, minute;

if (reDateTime.RegFind(strBuffer.c_str()) >= 0)
{
CFileCurl file;
file.Open(url);
pItem->m_dwSize= file.GetLength();
file.Close();
day = reDateTime.GetReplaceString("\\1");
month = reDateTime.GetReplaceString("\\2");
year = reDateTime.GetReplaceString("\\3");
hour = reDateTime.GetReplaceString("\\4");
minute = reDateTime.GetReplaceString("\\5");
}
else if (reDateTimeNginx.RegFind(strBuffer.c_str()) >= 0)
{
day = reDateTimeNginx.GetReplaceString("\\1");
month = reDateTimeNginx.GetReplaceString("\\2");
year = reDateTimeNginx.GetReplaceString("\\3");
hour = reDateTimeNginx.GetReplaceString("\\4");
minute = reDateTimeNginx.GetReplaceString("\\5");
}

if (day.length() > 0 && month.length() > 0 && year.length() > 0)
{
pItem->m_dateTime = CDateTime(atoi(year.c_str()), CDateTime::MonthStringToMonthNum(month), atoi(day.c_str()), atoi(hour.c_str()), atoi(minute.c_str()), 0);
}

if (!pItem->m_bIsFolder && pItem->m_dwSize == 0)
if (!pItem->m_bIsFolder)
{
CRegExp reSize(true);
reSize.RegComp(">*([0-9.]+)(B|K|M|G| )</td>");
if (reSize.RegFind(strBuffer.c_str()) >= 0)
{
double Size = atof(reSize.GetReplaceString("\\1"));
Expand All @@ -124,6 +150,19 @@ bool CHTTPDirectory::GetDirectory(const CStdString& strPath, CFileItemList &item

pItem->m_dwSize = (int64_t)Size;
}
else if (reSizeNginx.RegFind(strBuffer.c_str()) >= 0)
{
double Size = atof(reSizeNginx.GetReplaceString("\\1"));
pItem->m_dwSize = (int64_t)Size;
}
}

if (!pItem->m_bIsFolder && pItem->m_dwSize == 0 && g_advancedSettings.m_bHTTPDirectoryStatFilesize)
{
CFileCurl file;
file.Open(url);
pItem->m_dwSize= file.GetLength();
file.Close();
}

items.Add(pItem);
Expand Down

0 comments on commit a2e188c

Please sign in to comment.