?login_element?

Subversion Repositories NedoOS

Rev

Rev 1831 | Rev 1973 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. #define COMMANDLINE 0x0080
  13. unsigned char ver[] = "1.8";
  14. unsigned char queryType[64];
  15. unsigned char netbuf[1452];
  16. unsigned char dataBuffer[6096];
  17. unsigned char crlf[2] = {13, 10};
  18. unsigned char formats[4][4] = {"pt3", "pt2", "tfc", "ts"};
  19. unsigned long bytecount;
  20. unsigned char status, key, curFormat;
  21. struct sockaddr_in targetadr;
  22. struct readstructure readStruct;
  23. unsigned long contLen;
  24. long count;
  25. unsigned char saveFlag, saveBak, logFlag, rptFlag;
  26. union APP_PAGES main_pg;
  27. union APP_PAGES player_pg;
  28. extern void dns_resolve(void);
  29.  
  30. struct fileStruct
  31. {
  32.   long picId;
  33.   unsigned long fileSize;
  34.   unsigned int picYear;
  35.   unsigned long totalAmount;
  36.   unsigned int curPos;
  37.   unsigned int startBar;
  38.   unsigned int trackInSeconds;
  39.   unsigned char time[16];
  40.   unsigned char picRating[8];
  41.   unsigned char trackName[256];
  42.   unsigned char fileName[256];
  43.   unsigned char authorIds[64];
  44.   unsigned char authorTitle[64];
  45.   unsigned char authorRealName[64];
  46.   unsigned char afn[64];
  47.   unsigned char tfn[64];
  48.   unsigned char fileName2[256];
  49. } curFileStruct;
  50.  
  51. void delay(unsigned long counter)
  52. {
  53.   unsigned long start, finish;
  54.   counter = counter / 20;
  55.   if (counter < 1)
  56.   {
  57.     counter = 1;
  58.   }
  59.   start = time();
  60.   finish = start + counter;
  61.  
  62.   while (start < finish)
  63.   {
  64.     start = time();
  65.     YIELD();
  66.   }
  67. }
  68.  
  69. void clearStatus(void)
  70. {
  71.   AT(1, 25);
  72.   printf("                                                                               \r");
  73. }
  74.  
  75. void printProgress(unsigned char type)
  76. {
  77.   unsigned char bar, minutes, seconds;
  78.   unsigned char *position;
  79.   long barLenght;
  80.   int timer;
  81.   switch (type)
  82.   {
  83.   case 0: // print empty bar
  84.     AT(6, 11);
  85.     ATRIB(93);
  86.     printf("%02u:%02u", 0, 0);
  87.     AT(15, 11);
  88.     ATRIB(97);
  89.     for (bar = 0; bar < 50; bar++)
  90.     {
  91.       putchar(176);
  92.     }
  93.     putchar(' ');
  94.     putchar(' ');
  95.     minutes = atoi(curFileStruct.time);
  96.     position = (strstr(curFileStruct.time, ":")) + 1;
  97.     seconds = atoi(position);
  98.     curFileStruct.trackInSeconds = minutes * 60 + seconds;
  99.     curFileStruct.curPos = 0;
  100.     curFileStruct.startBar = 0;
  101.     break;
  102.   case 1: // print progress bar
  103.  
  104.     AT(6, 11);
  105.     ATRIB(93);
  106.     timer = floor(curFileStruct.curPos / 60);
  107.     printf("%02u:%02u", timer, (curFileStruct.curPos - (timer * 60)));
  108.  
  109.     barLenght = (curFileStruct.curPos * 50 / curFileStruct.trackInSeconds);
  110.     if (barLenght > 49)
  111.     {
  112.       barLenght = 50;
  113.     }
  114.     AT(15 + curFileStruct.startBar, 11);
  115.     ATRIB(97);
  116.     for (bar = 0; bar < barLenght - curFileStruct.startBar; bar++)
  117.     {
  118.       putchar(178);
  119.     }
  120.     AT(1, 1);
  121.     curFileStruct.startBar = bar;
  122.     break;
  123.   case 2: // print full bar
  124.     AT(15, 11);
  125.     ATRIB(97);
  126.     for (bar = 0; bar < 50; bar++)
  127.     {
  128.       putchar(178);
  129.     }
  130.     break;
  131.   }
  132. }
  133. void errorPrint(unsigned int error)
  134. {
  135.   switch (error)
  136.   {
  137.   case 2:
  138.     printf("02 SHUT_RDWR");
  139.     break;
  140.   case 4:
  141.     printf("04 ERR_INTR");
  142.     break;
  143.   case 23:
  144.     printf("23 ERR_NFILE");
  145.     break;
  146.   case 35:
  147.     printf("35 ERR_EAGAIN");
  148.     break;
  149.   case 37:
  150.     printf("37 ERR_ALREADY");
  151.     break;
  152.   case 38:
  153.     printf("38 ERR_NOTSOCK");
  154.     break;
  155.   case 40:
  156.     printf("40 ERR_EMSGSIZE");
  157.     break;
  158.   case 41:
  159.     printf("41 ERR_PROTOTYPE");
  160.     break;
  161.   case 47:
  162.     printf("47 ERR_AFNOSUPPORT");
  163.     break;
  164.   case 53:
  165.     printf("53 ERR_ECONNABORTED");
  166.     break;
  167.   case 54:
  168.     printf("54 ERR_CONNRESET");
  169.     break;
  170.   case 57:
  171.     printf("57 ERR_NOTCONN");
  172.     break;
  173.   case 65:
  174.     printf("65 ERR_HOSTUNREACH");
  175.     break;
  176.   default:
  177.     printf("%u UNKNOWN ERROR", error);
  178.     break;
  179.   }
  180.   YIELD();
  181.   //    do {key = _low_level_get();} while (key == 0);
  182. }
  183.  
  184. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  185. {
  186.   unsigned char socket;
  187.   unsigned int todo;
  188.   todo = OS_NETSOCKET((family << 8) + protocol);
  189.   if (todo > 32767)
  190.   {
  191.     clearStatus();
  192.     printf("OS_NETSOCKET: ");
  193.     errorPrint(todo & 255);
  194.     exit(0);
  195.   }
  196.   else
  197.   {
  198.     socket = ((todo & 65280) >> 8);
  199.     if (logFlag)
  200.     {
  201.       clearStatus();
  202.       printf("OS_NETSOCKET: Socket #%d created.", socket);
  203.     }
  204.   }
  205.   return socket;
  206. }
  207.  
  208. unsigned char netConnect(unsigned char socket)
  209. {
  210.   unsigned int todo;
  211.  
  212.   targetadr.family = AF_INET;
  213.   targetadr.porth = 00;
  214.   targetadr.portl = 80;
  215.   targetadr.b1 = 217;
  216.   targetadr.b2 = 146;
  217.   targetadr.b3 = 69;
  218.   targetadr.b4 = 13;
  219.  
  220.   todo = OS_NETCONNECT(socket, &targetadr);
  221.   if (todo > 32767)
  222.   {
  223.     clearStatus();
  224.     printf("OS_NETCONNECT: ");
  225.     errorPrint(todo & 255);
  226.  
  227.     exit(0);
  228.   }
  229.   else
  230.   {
  231.     if (logFlag)
  232.     {
  233.       clearStatus();
  234.       printf("OS_NETCONNECT: connected , %u", (todo & 255));
  235.     }
  236.   }
  237.   return 0;
  238. }
  239.  
  240. unsigned int tcpRead(unsigned char socket)
  241. {
  242.   unsigned char retry = 20;
  243.   unsigned int err, todo;
  244.   readStruct.socket = socket;
  245.   readStruct.BufAdr = (unsigned int)&netbuf;
  246.   readStruct.bufsize = sizeof(netbuf);
  247.   readStruct.protocol = SOCK_STREAM;
  248.  
  249. wizread:
  250.   todo = OS_WIZNETREAD(&readStruct);
  251.   if (todo > 32767)
  252.   {
  253.     if (retry == 0)
  254.     {
  255.       err = todo & 255;
  256.       clearStatus();
  257.       printf("OS_WIZNETREAD: ");
  258.       errorPrint(err);
  259.       if (err == 35)
  260.       {
  261.         return 0;
  262.       }
  263.       exit(0);
  264.     }
  265.     retry--;
  266.     if (logFlag)
  267.     {
  268.       AT(54, 25);
  269.       printf("OS_WIZNETREAD: retry %u   ", retry);
  270.     }
  271.     delay(200);
  272.     goto wizread;
  273.   }
  274.   if (logFlag)
  275.   {
  276.     clearStatus();
  277.     printf("OS_WIZNETREAD: %u bytes read.", todo);
  278.   }
  279.   return todo;
  280. }
  281.  
  282. unsigned int netShutDown(unsigned char socket, unsigned char type)
  283. {
  284.   unsigned int todo;
  285.   todo = OS_NETSHUTDOWN(socket, type);
  286.   if (todo > 32767)
  287.   {
  288.     clearStatus();
  289.     printf("OS_NETSHUTDOWN: ");
  290.     errorPrint(todo & 255);
  291.     return 255;
  292.   }
  293.   else
  294.   {
  295.     if (logFlag)
  296.     {
  297.       clearStatus();
  298.       printf("OS_NETSHUTDOWN: Socket #%u closed.", socket);
  299.     }
  300.   }
  301.   return 0;
  302. }
  303. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  304. {
  305.   unsigned int i, j;
  306.   unsigned int lenC, lenS;
  307.  
  308.   for (lenC = 0; c[lenC]; lenC++)
  309.     ;
  310.   for (lenS = 0; s[lenS]; lenS++)
  311.     ;
  312.  
  313.   for (i = startPos; i <= lenS - lenC; i++)
  314.   {
  315.     for (j = 0; s[i + j] == c[j]; j++)
  316.       ;
  317.  
  318.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  319.       return i;
  320.     if (j == lenC)
  321.       if (n - 1)
  322.         n--;
  323.       else
  324.         return i;
  325.   }
  326.   return -1;
  327. }
  328.  
  329. char *str_replace(char *dst, int num, const char *str,
  330.                   const char *orig, const char *rep)
  331. {
  332.   const char *ptr;
  333.   size_t len1 = strlen(orig);
  334.   size_t len2 = strlen(rep);
  335.   char *tmp = dst;
  336.  
  337.   num -= 1;
  338.   while ((ptr = strstr(str, orig)) != NULL)
  339.   {
  340.     num -= (ptr - str) + len2;
  341.     if (num < 1)
  342.       break;
  343.  
  344.     strncpy(dst, str, (size_t)(ptr - str));
  345.     dst += ptr - str;
  346.     strncpy(dst, rep, len2);
  347.     dst += len2;
  348.     str = ptr + len1;
  349.   }
  350.  
  351.   for (; (*dst = *str) && (num > 0); --num)
  352.   {
  353.     ++dst;
  354.     ++str;
  355.   }
  356.   return tmp;
  357. }
  358.  
  359. const char *parseJson(unsigned char *property)
  360. {
  361.   unsigned int w, lng, lngp1, findEnd, listPos;
  362.   unsigned char terminator;
  363.   int n;
  364.   n = -1;
  365.   netbuf[0] = '\0';
  366.   n = pos(dataBuffer, property, 1, 0);
  367.   if (n == -1)
  368.   {
  369.     strcpy(netbuf, "0\0");
  370.     return netbuf;
  371.   }
  372.   lng = n - 1 + strlen(property);
  373.   if (dataBuffer[lng] == ':')
  374.   {
  375.     terminator = '\0';
  376.   }
  377.   if (dataBuffer[lng] == '\"')
  378.   {
  379.     terminator = '\"';
  380.   }
  381.   if (dataBuffer[lng] == '[')
  382.   {
  383.     terminator = ']';
  384.   }
  385.  
  386.   findEnd = 1;
  387.   lngp1 = lng + 1;
  388.  
  389.   while (42)
  390.   {
  391.  
  392.     if ((dataBuffer[lngp1 + findEnd] == ','))
  393.     {
  394.       if (terminator == '\0')
  395.       {
  396.         break;
  397.       }
  398.       if ((dataBuffer[lng + findEnd] == terminator))
  399.       {
  400.         findEnd--;
  401.         break;
  402.       }
  403.     }
  404.     findEnd++;
  405.   }
  406.   listPos = 0;
  407.   for (w = lngp1; w < findEnd + lngp1; w++)
  408.   {
  409.     netbuf[listPos] = dataBuffer[w];
  410.     listPos++;
  411.   }
  412.   netbuf[listPos] = '\0';
  413.   return netbuf;
  414. }
  415. void convert866(void)
  416. {
  417.   unsigned int lng, targetPos, w, q = 0;
  418.   unsigned char buffer[8], one, two;
  419.   unsigned int decVal;
  420.   lng = strlen(netbuf);
  421.   targetPos = lng + 1;
  422.  
  423.   while (q < lng)
  424.   {
  425.     one = netbuf[q];
  426.     two = netbuf[q + 1];
  427.     if (one == 92 && two == 117)
  428.     {
  429.       q = q + 2;
  430.       for (w = 0; w < 4; w++)
  431.       {
  432.         buffer[w] = netbuf[q + w];
  433.       }
  434.       q = q + 4;
  435.       buffer[4] = '\0';
  436.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  437.  
  438.       if (decVal < 1088)
  439.       {
  440.         decVal = decVal - 912;
  441.       }
  442.       if (decVal > 1087)
  443.       {
  444.         decVal = decVal - 864;
  445.       }
  446.       if (decVal == 1025)
  447.       {
  448.         decVal = 240;
  449.       }
  450.       if (decVal == 1105)
  451.       {
  452.         decVal = 241;
  453.       }
  454.       netbuf[targetPos] = decVal;
  455.     }
  456.     else
  457.     {
  458.       netbuf[targetPos] = netbuf[q];
  459.       q++;
  460.     }
  461.     targetPos++;
  462.   }
  463.   netbuf[targetPos] = '\0';
  464.  
  465.   for (w = lng + 1; w < targetPos + 1; w++)
  466.   {
  467.     netbuf[w - lng - 1] = netbuf[w];
  468.   }
  469. }
  470.  
  471. unsigned int cutHeader(unsigned int todo)
  472. {
  473.   unsigned int q, headlng;
  474.   unsigned char *count;
  475.   count = strstr(netbuf, "Content-Length:");
  476.   if (count == NULL)
  477.   {
  478.     clearStatus();
  479.     printf("Content-Length:  not found.");
  480.     contLen = 0;
  481.   }
  482.   else
  483.   {
  484.     contLen = atol(count + 15);
  485.     bytecount = contLen;
  486.     curFileStruct.fileSize = bytecount;
  487.     //  printf("=> Dlinna  soderzhimogo = %lu \n\r", curFileStruct.fileSize);
  488.   }
  489.  
  490.   count = strstr(netbuf, "\r\n\r\n");
  491.   headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
  492.   q = todo - headlng;
  493.   memcpy(&netbuf, count + 4, q);
  494.   // printf ("header removed. %u bytes\r\n", headlng);
  495.   return q;
  496. }
  497.  
  498. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  499. {
  500.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  501.   str_replace(pfn, tfnSize, pfn, "/", "_");
  502.   str_replace(pfn, tfnSize, pfn, ":", "_");
  503.   str_replace(pfn, tfnSize, pfn, "*", "_");
  504.   str_replace(pfn, tfnSize, pfn, "?", "_");
  505.   str_replace(pfn, tfnSize, pfn, "<", "_");
  506.   str_replace(pfn, tfnSize, pfn, ">", "_");
  507.   str_replace(pfn, tfnSize, pfn, "|", "_");
  508.   str_replace(pfn, tfnSize, pfn, " ", "_");
  509.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  510.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  511.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  512.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  513.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  514.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  515. }
  516.  
  517. void stringRepair(unsigned char *pfn, unsigned int tSize)
  518. {
  519.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  520.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  521.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  522.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  523.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  524.   str_replace(pfn, tSize, pfn, "\\/", "/");
  525. }
  526.  
  527. unsigned char saveBuf(unsigned long fileId, unsigned char operation, unsigned int sizeOfBuf)
  528. {
  529.   FILE *fp2;
  530.   unsigned long fileSize;
  531.   unsigned char afnSize, tfnSize;
  532.   unsigned char fileIdChar[10];
  533.  
  534.   if (operation == 00)
  535.   {
  536.  
  537.     if (saveFlag == 0)
  538.     {
  539.       sprintf(curFileStruct.fileName, "temp.%s", formats[curFormat]);
  540.     }
  541.     else
  542.     {
  543.       afnSize = sizeof(curFileStruct.afn) - 1;
  544.       tfnSize = sizeof(curFileStruct.tfn) - 1;
  545.  
  546.       strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  547.       nameRepair(curFileStruct.afn, afnSize);
  548.       strcpy(curFileStruct.tfn, curFileStruct.trackName);
  549.       nameRepair(curFileStruct.tfn, tfnSize);
  550.       sprintf(curFileStruct.fileName, "%s-%s.%s", curFileStruct.afn, curFileStruct.tfn, formats[curFormat]);
  551.  
  552.       if (strlen(curFileStruct.fileName) > 63)
  553.       {
  554.         sprintf(fileIdChar, "-%ld", fileId);
  555.         str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  556.         curFileStruct.fileName[50] = '\0';
  557.         strcat(curFileStruct.fileName, fileIdChar);
  558.         strcat(curFileStruct.fileName, formats[curFormat]);
  559.         // printf("filename = [%s]",curFileStruct.fileName);
  560.       }
  561.     }
  562.     OS_SETSYSDRV();
  563.     OS_MKDIR("../downloads/radio"); // Create if not exist
  564.     OS_CHDIR("../downloads/radio");
  565.     fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  566.     if (((int)fp2) & 0xff)
  567.     {
  568.       clearStatus();
  569.       printf(curFileStruct.fileName);
  570.       printf(" creating error. Check for  downloads\\radio folder.");
  571.       getchar();
  572.       exit(0);
  573.     }
  574.     OS_CLOSEHANDLE(fp2);
  575.     return 0;
  576.   }
  577.  
  578.   if (operation == 01)
  579.   {
  580.     fp2 = OS_OPENHANDLE(curFileStruct.fileName, 0x80);
  581.     if (((int)fp2) & 0xff)
  582.     {
  583.  
  584.       clearStatus();
  585.       printf(curFileStruct.fileName);
  586.       printf(" opening error.");
  587.       exit(0);
  588.     }
  589.     fileSize = OS_GETFILESIZE(fp2);
  590.     OS_SEEKHANDLE(fp2, fileSize);
  591.     OS_WRITEHANDLE(netbuf, fp2, sizeOfBuf);
  592.     OS_CLOSEHANDLE(fp2);
  593.     return 0;
  594.   }
  595.  
  596.   if (operation == 02)
  597.   {
  598.     OS_CLOSEHANDLE(fp2);
  599.     return 0;
  600.   }
  601.  
  602.   return 0;
  603. }
  604.  
  605. void getData(unsigned char socket)
  606. {
  607.   unsigned int todo, w, bPos, bytes2read, headskip;
  608.  
  609.   headskip = 0;
  610.   bPos = 0;
  611.   bytecount = 255;
  612.   while (1)
  613.   {
  614.     todo = tcpRead(socket);
  615.     if (todo == 0)
  616.     {
  617.       break;
  618.     }
  619.     bytes2read = todo;
  620.     if (headskip == 0)
  621.     {
  622.       headskip = 1;
  623.       bytes2read = cutHeader(todo);
  624.     }
  625.  
  626.     if (bPos + bytes2read > sizeof(dataBuffer))
  627.     {
  628.       clearStatus();
  629.       printf("dataBuffer overrun...");
  630.       break;
  631.     }
  632.  
  633.     for (w = 0; w < bytes2read; w++)
  634.     {
  635.       dataBuffer[w + bPos] = netbuf[w];
  636.     }
  637.     bytecount = bytecount - bytes2read;
  638.     bPos = bPos + bytes2read;
  639.     if (bytecount == 0)
  640.     {
  641.       dataBuffer[bytes2read + bPos + 1] = '\0';
  642.       break;
  643.     }
  644.   }
  645.   netShutDown(socket, 1);
  646. }
  647.  
  648. unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
  649. {
  650.   unsigned char retry = 20;
  651.   unsigned int todo;
  652.   readStruct.socket = socket;
  653.   readStruct.BufAdr = messageadr;
  654.   readStruct.bufsize = size;
  655.   readStruct.protocol = SOCK_STREAM;
  656.  
  657. wizwrite:
  658.   todo = OS_WIZNETWRITE(&readStruct);
  659.   if (todo > 32767)
  660.   {
  661.     clearStatus();
  662.     printf("OS_WIZNETWRITE: ");
  663.     errorPrint(todo & 255);
  664.     if (retry == 0)
  665.     {
  666.       exit(0);
  667.     }
  668.     retry--;
  669.     delay(150);
  670.     goto wizwrite;
  671.   }
  672.   else
  673.   {
  674.     if (logFlag)
  675.     {
  676.       clearStatus();
  677.       printf("OS_WIZNETWRITE: %u bytes written.", todo);
  678.     }
  679.   }
  680.   return todo;
  681. }
  682.  
  683. unsigned long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  684. {
  685.   FILE *fp3;
  686.   unsigned int retry, tSize;
  687.   unsigned int todo;
  688.   unsigned char buffer[] = "000000000";
  689.   unsigned char *count, socket;
  690.   unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; Radio)\r\n\r\n\0";
  691.   unsigned char userQuery[256] = "/api/export:zxMusic/limit:10/filter:zxMusicId=44816";
  692.   clearStatus();
  693.   printf("Getting data(%u)...", queryNum);
  694.  
  695.   switch (queryNum)
  696.   {
  697.   case 0: // GET /api/export:zxMusic/limit:1/start:1/filter:zxMusicFormat=pt3/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  698.     strcpy(netbuf, "GET /api/export:zxMusic/limit:");
  699.     sprintf(buffer, "%u", limit);
  700.     strcat(netbuf, buffer);
  701.     strcat(netbuf, "/start:");
  702.     sprintf(buffer, "%lu", startPos);
  703.     strcat(netbuf, buffer);
  704.     strcat(netbuf, "/filter:zxMusicFormat=");
  705.     strcat(netbuf, formats[curFormat]);
  706.     strcat(netbuf, "/order:date,desc");
  707.     strcat(netbuf, userAgent);
  708.     break;
  709.   case 1: // GET /api/types:zxMusic/export:zxMusic/language:eng/limit:1/start:0/order:votes,rand/filter:zxMusicMinRating=4;
  710.     startPos = 0;
  711.     strcpy(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:");
  712.     sprintf(buffer, "%u", limit);
  713.     strcat(netbuf, buffer);
  714.     strcat(netbuf, "/start:");
  715.     sprintf(buffer, "%lu", startPos);
  716.     strcat(netbuf, buffer);
  717.     strcat(netbuf, "/order:votes,rand/filter:zxMusicMinRating=4;zxMusicFormat=");
  718.     strcat(netbuf, formats[curFormat]);
  719.     strcat(netbuf, userAgent);
  720.     break;
  721.   case 2: // GET /api/types:zxMusic/export:zxMusic/language:eng/limit:1/start:0/order:rand/filter:zxMusicFormat=PT3 HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  722.     startPos = 0;
  723.     strcpy(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:");
  724.     sprintf(buffer, "%u", limit);
  725.     strcat(netbuf, buffer);
  726.     strcat(netbuf, "/start:");
  727.     sprintf(buffer, "%lu", startPos);
  728.     strcat(netbuf, buffer);
  729.     strcat(netbuf, "/order:rand/filter:zxMusicFormat=");
  730.     strcat(netbuf, formats[curFormat]);
  731.     strcat(netbuf, userAgent);
  732.     break;
  733.  
  734.   case 3: // GET /api/export:zxMusic/limit:1/start:1/filter:zxMusicFormat=pt3;authorId=7744/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  735.  
  736.     fp3 = OS_OPENHANDLE("radio/user.que", 0x80);
  737.     if (((int)fp3) & 0xff)
  738.     {
  739.       fp3 = OS_CREATEHANDLE("radio/user.que", 0x80);
  740.       OS_WRITEHANDLE(userQuery, fp3, sizeof(userQuery));
  741.       OS_CLOSEHANDLE(fp3);
  742.       fp3 = OS_OPENHANDLE("radio/user.que", 0x80);
  743.     }
  744.     OS_READHANDLE(userQuery, fp3, sizeof(userQuery));
  745.     OS_CLOSEHANDLE(fp3);
  746.  
  747.     strcpy(netbuf, "GET /api/limit:");
  748.     sprintf(buffer, "%u", limit);
  749.     strcat(netbuf, buffer);
  750.     strcat(netbuf, "/start:");
  751.     sprintf(buffer, "%lu", startPos);
  752.     strcat(netbuf, buffer);
  753.     strcat(netbuf, userQuery);
  754.     strcat(netbuf, userAgent);
  755.     break;
  756.  
  757.   case 99: // GET /jsonElementData/elementId:182798
  758.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  759.     sprintf(buffer, "%lu", startPos);
  760.     strcat(netbuf, buffer);
  761.     strcat(netbuf, userAgent);
  762.     break;
  763.   }
  764.  
  765.   retry = 10;
  766. rejson:
  767.   socket = OpenSock(AF_INET, SOCK_STREAM);
  768.   netConnect(socket);
  769.  
  770.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  771.  
  772.   getData(socket);
  773.  
  774.   clearStatus();
  775.   printf("Processing data (%u)...", queryNum);
  776.  
  777.   count = strstr(dataBuffer, "responseStatus\":\"success");
  778.   if (count == NULL)
  779.   {
  780.     clearStatus();
  781.     printf("BAD JSON, NO responseStatus: success. (%u)", retry);
  782.     retry--;
  783.     YIELD();
  784.     if (retry > 0)
  785.     {
  786.       netShutDown(socket, 1);
  787.       goto rejson;
  788.     }
  789.     // AT(1, 1);
  790.     // printf("BAD JSON, NO responseStatus: success. JSON: %s \r\n", dataBuffer);
  791.     // getchar();
  792.     return -1;
  793.   }
  794.  
  795.   count = strstr(dataBuffer, "\"id\":");
  796.   if (count == NULL)
  797.   {
  798.     clearStatus();
  799.     printf("BAD JSON: not ID query = %u startPos = %lu", queryNum, startPos);
  800.     // AT(1, 10);
  801.     // printf("BAD JSON: ID not found JSON:\r\n %s \r\n", dataBuffer);
  802.     return -2;
  803.   }
  804.   if (queryNum < 4)
  805.   {
  806.     netbuf[0] = '\0';
  807.     parseJson("\"id\":");
  808.     curFileStruct.picId = atol(netbuf);
  809.     parseJson(",\"title\":\"");
  810.     convert866();
  811.     strcpy(curFileStruct.trackName, netbuf);
  812.  
  813.     tSize = sizeof(curFileStruct.trackName);
  814.     stringRepair(curFileStruct.trackName, tSize);
  815.  
  816.     /*
  817.         str_replace(curFileStruct.trackName, tnSize, curFileStruct.trackName, "&#039;", "'");
  818.         str_replace(curFileStruct.trackName, tnSize, curFileStruct.trackName, "&amp;", "&");
  819.         str_replace(curFileStruct.trackName, tnSize, curFileStruct.trackName, "&gt;", ">");
  820.         str_replace(curFileStruct.trackName, tnSize, curFileStruct.trackName, "&lt;", "<");
  821.         str_replace(curFileStruct.trackName, tnSize, curFileStruct.trackName, "&quot;", "\"");
  822.         str_replace(curFileStruct.trackName, tnSize, curFileStruct.trackName, "\\/", "/");
  823.     */
  824.     parseJson("\"rating\":\"");
  825.     strcpy(curFileStruct.picRating, netbuf);
  826.     parseJson("\"year\":\"");
  827.     curFileStruct.picYear = atoi(netbuf);
  828.     parseJson("\"totalAmount\":");
  829.     curFileStruct.totalAmount = atol(netbuf);
  830.     parseJson("\"time\":\"");
  831.     strcpy(curFileStruct.time, netbuf);
  832.     parseJson("\"authorIds\":[");
  833.     strcpy(curFileStruct.authorIds, netbuf);
  834.     parseJson("\"authorIds\":[");
  835.     strcpy(curFileStruct.fileName2, netbuf);
  836.   }
  837.   if (queryNum == 99)
  838.   {
  839.     parseJson(",\"title\":\"");
  840.     convert866();
  841.     strcpy(curFileStruct.authorTitle, netbuf);
  842.     parseJson(",\"realName\":\"");
  843.     convert866();
  844.     strcpy(curFileStruct.authorRealName, netbuf);
  845.   }
  846.   return curFileStruct.picId;
  847. }
  848.  
  849. unsigned char getTrack(unsigned long fileId)
  850. {
  851.   unsigned int todo;
  852.   unsigned char cmdlist1[] = "GET /file/id:";
  853.   unsigned char cmdlist2[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; Radio)\r\n\r\n\0";
  854.   unsigned char buffer[] = "0000000000";
  855.   unsigned char socket;
  856.   unsigned int bytes2read, headskip;
  857.   clearStatus();
  858.   printf("Getting track...");
  859.  
  860.   socket = OpenSock(AF_INET, SOCK_STREAM);
  861.   todo = netConnect(socket);
  862.   netbuf[0] = '\0';
  863.   sprintf(buffer, "%lu", fileId);
  864.   strcat(netbuf, cmdlist1);
  865.   strcat(netbuf, buffer);
  866.   strcat(netbuf, cmdlist2);
  867.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  868.  
  869.   headskip = 0;
  870.   bytecount = 255;
  871.   saveBuf(curFileStruct.picId, 00, 0);
  872.   while (bytecount != 0)
  873.   {
  874.     todo = tcpRead(socket);
  875.     if (todo == 0)
  876.     {
  877.       break;
  878.     }
  879.     bytes2read = todo;
  880.     if (headskip == 0)
  881.     {
  882.       headskip = 1;
  883.       bytes2read = cutHeader(todo);
  884.     }
  885.     saveBuf(curFileStruct.picId, 01, bytes2read);
  886.     bytecount = bytecount - bytes2read;
  887.   }
  888.   netShutDown(socket ,0);
  889.   return 0;
  890. }
  891.  
  892. unsigned char runPlayer(void)
  893. {
  894.   FILE *fp2;
  895.   unsigned char fileName[] = "radio/player.ovl";
  896.   unsigned char appCmd[128] = "player.com ";
  897.   unsigned char curPath[128];
  898.   unsigned long playerSize, loaded, loop;
  899.   unsigned char pgbak;
  900.  
  901.   clearStatus();
  902.   printf("Running player...");
  903.  
  904.   strcat(appCmd, curFileStruct.fileName);
  905.   player_pg.l = OS_GETMAINPAGES();
  906.   pgbak = main_pg.pgs.window_3;
  907.   loaded = 0;
  908.   OS_GETPATH((unsigned int)&curPath);
  909.   OS_SETSYSDRV();
  910.   fp2 = OS_OPENHANDLE(fileName, 0x80);
  911.   if (((int)fp2) & 0xff)
  912.   {
  913.     clearStatus();
  914.     printf(fileName);
  915.     printf(" not found.");
  916.     exit(0);
  917.   }
  918.   playerSize = OS_GETFILESIZE(fp2);
  919.   OS_CHDIR(curPath);
  920.   OS_NEWAPP((unsigned int)&player_pg);
  921.   SETPG32KHIGH(player_pg.pgs.window_3);
  922.   memcpy((char *)(0xC080), &appCmd, sizeof(appCmd));
  923.   for (loop = 0; loop < playerSize; loop = loop + loaded)
  924.   {
  925.     loaded = OS_READHANDLE(dataBuffer, fp2, sizeof(dataBuffer));
  926.     memcpy((char *)(0xC100 + loop), &dataBuffer, loaded);
  927.   }
  928.   OS_CLOSEHANDLE(fp2);
  929.   SETPG32KHIGH(pgbak);
  930.   OS_RUNAPP(player_pg.pgs.pId);
  931.   return player_pg.pgs.pId;
  932. }
  933.  
  934. long trackSelector(unsigned char mode)
  935. {
  936.   switch (mode)
  937.   {
  938.   case 0: // Next track
  939.     count++;
  940.     if (count > curFileStruct.totalAmount - 1)
  941.     {
  942.       count = 0;
  943.     }
  944.     break;
  945.   case 1: // Prev. track
  946.     count--;
  947.     if (count < 0)
  948.     {
  949.       count = curFileStruct.totalAmount - 1;
  950.     }
  951.     break;
  952.   }
  953.   return count;
  954. }
  955.  
  956. void printStatus(void)
  957. {
  958.   AT(1, 9);
  959.   ATRIB(93);
  960.   printf(" [Q]Query : ");
  961.   ATRIB(97);
  962.   printf("%s", queryType);
  963.   printf("  ");
  964.   AT(1, 24);
  965.   ATRIB(45);
  966.   printf("                                                                                ");
  967.   AT(2, 24);
  968.   ATRIB(93);
  969.   printf(" [F]Format: ");
  970.   ATRIB(97);
  971.   printf("%s", formats[curFormat]);
  972.   ATRIB(93);
  973.   printf(" [K]Keep files: ");
  974.   ATRIB(97);
  975.   printf("%u", saveFlag);
  976.   ATRIB(93);
  977.   printf(" [R]Repeat: ");
  978.   ATRIB(97);
  979.   printf("%u", rptFlag);
  980.   ATRIB(93);
  981.   printf(" [J]Jump to ");
  982.   printf(" [E]Exit        [%s]\r\n", ver);
  983.  
  984.   ATRIB(97);
  985.   ATRIB(40);
  986. }
  987.  
  988. void printInfo(void)
  989. {
  990.   BOX(30, 2, 50, 6, 40);
  991.   AT(1, 2);
  992.   ATRIB(97);
  993.   ATRIB(93);
  994.   printf(" #: ");
  995.   ATRIB(97);
  996.   printf("%lu", count);
  997.   ATRIB(93);
  998.   printf(" ID: ");
  999.   ATRIB(97);
  1000.   printf("%lu", curFileStruct.picId);
  1001.   ATRIB(93);
  1002.   printf(" Total Tracks: ");
  1003.   ATRIB(97);
  1004.   printf("%lu", curFileStruct.totalAmount);
  1005.   printf(" \r\n");
  1006.   ATRIB(93);
  1007.   printf(" RATING: ");
  1008.   ATRIB(97);
  1009.   printf("%s", curFileStruct.picRating);
  1010.   ATRIB(93);
  1011.   printf(" YEAR: ");
  1012.   ATRIB(97);
  1013.   printf("%u", curFileStruct.picYear);
  1014.   ATRIB(93);
  1015.   printf(" DURATION: ");
  1016.   ATRIB(97);
  1017.   printf("%s", curFileStruct.time);
  1018.   printf(" \r\n\r\n");
  1019.   ATRIB(93);
  1020.   printf(" AuthorsIDs ");
  1021.   ATRIB(97);
  1022.   printf("%s", curFileStruct.authorIds);
  1023.   ATRIB(93);
  1024.   printf(" Author: ");
  1025.   ATRIB(97);
  1026.   printf("%s", curFileStruct.authorTitle);
  1027.   ATRIB(93);
  1028.   printf(" Real name: ");
  1029.   ATRIB(97);
  1030.   printf("%s", curFileStruct.authorRealName);
  1031.   printf(" \r\n\r\n");
  1032.   ATRIB(96);
  1033.   printf("                                                                           \r");
  1034.   printf("   TITLE: %s\r\n", curFileStruct.trackName);
  1035. }
  1036.  
  1037. void printHelp(void)
  1038. {
  1039.   AT(1, 13);
  1040.   ATRIB(97);
  1041.   printf(" [E] or [ESC] Exit to OS           \r\n");
  1042.   printf(" [B] or [<--] Previous track       \r\n");
  1043.   printf(" [ ] or [-->] Next track    \r\n");
  1044.   printf(" [S]          Stop player          \r\n");
  1045.   printf(" [K]          Toggle saving tracks \r\n");
  1046.   printf(" [Q]          Select Query type    \r\n");
  1047.   printf(" [D]          Download track       \r\n");
  1048.   printf(" [R]          Repeat track mode    \r\n");
  1049.   printf(" [J]          Jump to NNNN file from newest  \r\n");
  1050.   printf(" [F]          Change tracks format to play   \r\n");
  1051.   printf(" [L]          Toggle operation logging       \r\n");
  1052. }
  1053.  
  1054. unsigned char testPlayer(void)
  1055. {
  1056.   union APP_PAGES player2_pg;
  1057.   player2_pg.l = OS_GETAPPMAINPAGES(player_pg.pgs.pId);
  1058.  
  1059.   if (errno == 0)
  1060.   {
  1061.     return 1;
  1062.   }
  1063.   else
  1064.   {
  1065.     return 0;
  1066.   }
  1067. }
  1068.  
  1069. C_task main(void)
  1070. {
  1071.   unsigned char errn, keypress, queryNum, pId, alive, changedFormat;
  1072.   long iddqd, idkfa, ipadress;
  1073.   unsigned long curTimer, startTimer, oldTimer;
  1074.   os_initstdio();
  1075.   srand(time());
  1076.   count = 0;
  1077.   saveFlag = 0;
  1078.   logFlag = 0;
  1079.   queryNum = 0;
  1080.   curFormat = 0;
  1081.   changedFormat = 0;
  1082.   rptFlag = 0;
  1083.   strcpy(queryType, "from newest to oldest");
  1084.  
  1085.   BOX(1, 1, 80, 25, 40);
  1086.   AT(1, 1);
  1087.   ATRIB(97);
  1088.   ATRIB(45);
  1089.   printf("                           ZXART.EE radio for nedoNET                           \n\r");
  1090.   AT(1, 24);
  1091.   printf("  [L]Enable logging(press on startup)                                          ");
  1092.  
  1093.   ATRIB(33);
  1094.   ATRIB(40);
  1095.  
  1096.   /*
  1097.     ipadress = OS_DNSRESOLVE("zxart.ee");
  1098.     printf("\n\r  OS_DNSRESOLVE =  %lu \n\r", ipadress);
  1099.     printf("------------------------\n\r");
  1100.     printf("OS_GETPATH = %s\r\n", curPath);
  1101.   */
  1102.  
  1103.   keypress = _low_level_get();
  1104.   if (keypress == 'l' || keypress == 'L')
  1105.   {
  1106.     logFlag = 1;
  1107.     clearStatus();
  1108.     printf("Logging enabled");
  1109.     getchar();
  1110.   }
  1111.  
  1112. start:
  1113.   OS_SETSYSDRV();
  1114.   printHelp();
  1115.   curFileStruct.fileSize = 0;
  1116.  
  1117.   iddqd = processJson(count, 1, queryNum); // Query for track info
  1118.   if (iddqd < 0)
  1119.   {
  1120.     {
  1121.       clearStatus();
  1122.       printf("Error getting track info, next please(%ld)...", iddqd);
  1123.       count = trackSelector(0);
  1124.       goto start;
  1125.     }
  1126.   }
  1127.  
  1128.   idkfa = processJson(atol(curFileStruct.authorIds), 0, 99); // Query for AuthorID
  1129.  
  1130.   if (idkfa < 0)
  1131.   {
  1132.     clearStatus();
  1133.     printf("Error getting author %lu", atol(curFileStruct.authorIds));
  1134.     strcpy(curFileStruct.authorTitle, " Error getting Tittle ");
  1135.     // strcpy(curFileStruct.authorRealName, " \0");
  1136.   }
  1137. replay:
  1138.   errn = getTrack(iddqd); // Downloading the track
  1139. resume:
  1140.   startTimer = time();
  1141.   printProgress(0);
  1142.   pId = runPlayer(); // Start thr Player!
  1143.   printStatus();
  1144.   printInfo();
  1145. rekey:
  1146.   YIELD();
  1147.   keypress = _low_level_get();
  1148.   if (keypress != 0)
  1149.   {
  1150.     if (keypress == 27 || keypress == 'e' || keypress == 'E')
  1151.     {
  1152.       OS_DROPAPP(pId);
  1153.       printf("Good bye... %u \r\n", pId);
  1154.       ATRIB(37);
  1155.       ATRIB(40);
  1156.       exit(0);
  1157.     }
  1158.     if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1159.     {
  1160.       changedFormat = 0;
  1161.       OS_DROPAPP(pId);
  1162.       clearStatus();
  1163.       printf("Player stopped...");
  1164.       count = trackSelector(1);
  1165.       goto start;
  1166.     }
  1167.  
  1168.     if (keypress == 251 || keypress == 32 || keypress == 'n' || keypress == 'N')
  1169.     {
  1170.       changedFormat = 0;
  1171.       OS_DROPAPP(pId);
  1172.       clearStatus();
  1173.       printf("Player stopped...");
  1174.       count = trackSelector(0);
  1175.       goto start;
  1176.     }
  1177.  
  1178.     if (keypress == 'k' || keypress == 'K')
  1179.     {
  1180.       OS_DROPAPP(pId);
  1181.       clearStatus();
  1182.       printf("Player stopped...");
  1183.       saveFlag = !saveFlag;
  1184.       printStatus();
  1185.       goto replay;
  1186.     }
  1187.  
  1188.     if (keypress == 'q' || keypress == 'Q')
  1189.     {
  1190.       OS_DROPAPP(pId);
  1191.       clearStatus();
  1192.       printf("Player stopped...");
  1193.       queryNum++;
  1194.       if (queryNum > 3)
  1195.       {
  1196.         queryNum = 0;
  1197.       }
  1198.       switch (queryNum)
  1199.       {
  1200.       case 0:
  1201.         strcpy(queryType, "from newest to oldest                   ");
  1202.         break;
  1203.       case 1:
  1204.         strcpy(queryType, "Random best and most voted tracks       ");
  1205.         break;
  1206.       case 2:
  1207.         strcpy(queryType, "Random play                             ");
  1208.         break;
  1209.       case 3:
  1210.         strcpy(queryType, "User defined query from \"user.que\"     ");
  1211.         break;
  1212.       }
  1213.       count = 0;
  1214.       printStatus();
  1215.       goto start;
  1216.     }
  1217.  
  1218.     if (keypress == 'j' || keypress == 'J')
  1219.     {
  1220.       AT(1, 7);
  1221.       printf("                                                                      \r");
  1222.       printf("Jump to track:");
  1223.       scanf("%lu", &count);
  1224.       OS_DROPAPP(pId);
  1225.       if (count > curFileStruct.totalAmount - 1)
  1226.       {
  1227.         count = curFileStruct.totalAmount - 1;
  1228.       }
  1229.       goto start;
  1230.     }
  1231.  
  1232.     if (keypress == 'f' || keypress == 'F')
  1233.     {
  1234.       OS_DROPAPP(pId);
  1235.       clearStatus();
  1236.       printf("Player stopped...");
  1237.       curFormat++;
  1238.       count = -1;
  1239.       if (curFormat > 3)
  1240.       {
  1241.         curFormat = 0;
  1242.       }
  1243.       changedFormat = 1;
  1244.       curFileStruct.totalAmount = 1;
  1245.       printStatus();
  1246.       printProgress(0);
  1247.       BOX(1, 2, 80, 6, 40);
  1248.       goto rekey;
  1249.     }
  1250.  
  1251.     if (keypress == 'l' || keypress == 'L')
  1252.     {
  1253.       logFlag = !logFlag;
  1254.       clearStatus();
  1255.       printf("Logging: %u", logFlag);
  1256.     }
  1257.  
  1258.     if (keypress == 's' || keypress == 'S')
  1259.     {
  1260.       OS_DROPAPP(pId);
  1261.       clearStatus();
  1262.       printf("Player stopped...");
  1263.       printProgress(0);
  1264.       getchar();
  1265.       goto resume;
  1266.     }
  1267.     if (keypress == 'r' || keypress == 'R')
  1268.     {
  1269.       rptFlag = !rptFlag;
  1270.       clearStatus();
  1271.       printStatus();
  1272.       goto rekey;
  1273.     }
  1274.     if (keypress == 'd' || keypress == 'D')
  1275.     {
  1276.       saveBak = saveFlag;
  1277.       saveFlag = 1;
  1278.       errn = getTrack(iddqd); // Downloading the track
  1279.       saveFlag = saveBak;
  1280.       clearStatus();
  1281.       printf("File saved: [%s]...", curFileStruct.fileName);
  1282.       goto rekey;
  1283.     }
  1284.   }
  1285.  
  1286.   curTimer = time();
  1287.   curFileStruct.curPos = (curTimer - startTimer) / 50;
  1288.   alive = testPlayer();
  1289.  
  1290.   if (alive == 0 && !changedFormat)
  1291.   {
  1292.     if (rptFlag == 1)
  1293.     {
  1294.       goto resume;
  1295.     }
  1296.     printProgress(2);
  1297.     count = trackSelector(0);
  1298.     goto start;
  1299.   }
  1300.  
  1301.   if (alive == 1 && ((curTimer - oldTimer) > 49))
  1302.   {
  1303.     printProgress(1);
  1304.     oldTimer = curTimer;
  1305.   }
  1306.  
  1307.   YIELD();
  1308.   YIELD();
  1309.   goto rekey;
  1310. }
  1311.