//+------------------------------------------------------------------+
//|                                      MyFxBookCommunityScript.mq4 |
//|                                         Copyright © 2014, Ice FX |
//|                                              http://www.icefx.eu |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, Ice FX"
#property link      "http://www.icefx.eu"
#property strict

string url = "http://www.myfxbook.com/community/outlook";
string targetFile = "outlook_data.html";

int start()
{
   Comment("Downloading data from myfxbook...");

   string response = "";
   if (HttpGET(url, targetFile))
   {
      ProcessResponse(targetFile);
   }
   
   return(0);
}

void ProcessResponse(string fileName)
{
   //Print("FileName: ", fileName);
   
   // Open request file
   int f = FileOpen(fileName, FILE_CSV|FILE_READ, '?');
   if (f == 0) {
      // File not found
      Print("Response file not found! Path: ", fileName);
      Comment("Error! Please check the log");
      return;
   }
   
   string msg = "MyFxBook Outlook:\n\n";
   
   while(!FileIsEnding(f)) 
   {
      // Read one line...
      string line = FileReadString(f);
      
      // if we found our table
      if (StringFind(line, "<td id=\"symbolNameCell") >= 0)
      {
         // Cut symbol name
         CutAt(line, ">");
         CutAt(line, ">");
         string symbol = StringTrimLeft(StringTrimRight(CutAt(line, "<")));

         // Jump to short cell
         for (int i = 0; i < 12; i++)
            FileReadString(f);

         line = FileReadString(f);
         CutAt(line, ">");
         string sShort = CutAt(line, "<");

         // Jump to long cell
         for (int i = 0; i < 5; i++)
            FileReadString(f);

         line = FileReadString(f);
         CutAt(line, ">");
         string sLong = CutAt(line, "<");
         
         //Print("Symbol: ", symbol, " Short: ", sShort, ", Long: ", sLong);
         msg = StringConcatenate(msg, "Symbol: ", symbol, " Short: ", sShort, ", Long: ", sLong, "\n");
      }
   
   }   
   
   Comment(msg);
   
   FileClose(f);   
   FileDelete(fileName);
}



string CutAt(string& str, string sep)
{
   string res = "";
   
   int index = StringFind(str, sep, 0);
   if (index != -1)
   {
      if (index > 0) res = StringSubstr(str, 0, index);
      str = StringSubstr(str, index + StringLen(sep));    
   } else {
      res = str;
      str = "";
   }
   return(res);
}

//////// HTTP GET code ////////

#import "kernel32.dll"
   int  CreateFileW(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
   int  WriteFile(int hFile, string lpBuffer, int nNumberOfBytesToWrite, int &lpNumberOfBytesWritten[], int lpOverlapped);
   int  CloseHandle(int hObject);
#import


#import "wininet.dll"
   int InternetOpenW(string sAgent, int lAccessType, string sProxyName="", string sProxyBypass="", int lFlags=0);
   int InternetOpenUrlW(int hInternetSession, string sUrl, string sHeaders="", int lHeadersLength=0, int lFlags=0,
      int lContext=0);
   int InternetReadFile(int hFile, uchar &arr[], int lNumBytesToRead, int& lNumberOfBytesRead[]);
   int InternetCloseHandle(int hInet);
#import


#define INTERNET_OPEN_TYPE_DIRECT       0
#define INTERNET_OPEN_TYPE_PRECONFIG    1
#define INTERNET_OPEN_TYPE_PROXY        3

// Had to cut the following two defines because of silly MQL4 identifier limits

#define _IGNORE_REDIRECT_TO_HTTP        0x00008000
#define _IGNORE_REDIRECT_TO_HTTPS       0x00004000

#define INTERNET_FLAG_KEEP_CONNECTION   0x00400000
#define INTERNET_FLAG_NO_AUTO_REDIRECT  0x00200000
#define INTERNET_FLAG_NO_COOKIES        0x00080000
#define INTERNET_FLAG_RELOAD            0x80000000
#define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000
#define INTERNET_FLAG_DONT_CACHE        0x04000000
#define INTERNET_FLAG_PRAGMA_NOCACHE    0x00000100
#define INTERNET_FLAG_NO_UI             0x00000200

#define HTTP_ADDREQ_FLAG_ADD            0x20000000
#define HTTP_ADDREQ_FLAG_REPLACE        0x80000000

#define INTERNET_SERVICE_HTTP           3
#define INTERNET_DEFAULT_HTTP_PORT      80

#define ICU_ESCAPE                      0x80000000
#define ICU_USERNAME                    0x40000000
#define ICU_NO_ENCODE                   0x20000000
#define ICU_DECODE                      0x10000000
#define ICU_NO_META                     0x08000000
#define ICU_ENCODE_PERCENT              0x00001000
#define ICU_ENCODE_SPACES_ONLY          0x04000000
#define ICU_BROWSER_MODE                0x02000000

#define AGENT                           "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36"
#define BUFSIZ                          1000

bool HttpGET(string strUrl, string fileName)
{
  int hSession = InternetOpenW(AGENT, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);

  if (hSession == 0) {
    Print("hSession error");
    return(false);
  }

  int hReq = InternetOpenUrlW(hSession, strUrl, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);

  if (hReq == 0) {
    Print("hReq error: ",  GetLastError());
    return(false);
  }

  int     lReturn[]  = {1};
  uchar   buffer[BUFSIZ];
  ZeroMemory(buffer);

  int f = FileOpen(fileName, FILE_WRITE|FILE_BIN);
  FileSeek(f, 0, SEEK_SET);

  while (TRUE) {
    if (InternetReadFile(hReq, buffer, BUFSIZ, lReturn) <= 0 || lReturn[0] == 0) break;
    
    //string s = CharArrayToString(buffer, 0, lReturn[0], CP_UTF8);
    //FileWriteString(f, s, StringBufferLen(s));
    FileWriteArray(f, buffer, 0, lReturn[0]);
  }
  
  FileClose(f);

  InternetCloseHandle(hReq);
  InternetCloseHandle(hSession);

  return (true);
}