Subversion Repositories NedoOS

Rev

Rev 1437 | Rev 1459 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  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. unsigned char netbuf[8192];
  11. unsigned char picture[7000];
  12. unsigned char piclist[3072];
  13. unsigned char crlf[2] = {13, 10};
  14. unsigned int bytecount;
  15. unsigned char status, key;
  16. struct sockaddr_in   targetadr;
  17. struct readstructure   readStruct;
  18.  
  19. void putdec(int c)
  20. {
  21.   int div;
  22.   int hassent = 0;
  23.   for (div = 100; div > 0; div /= 10) {
  24.     int disp = c / div;
  25.     c %= div;
  26.     if ((disp != 0) || (hassent) || (div == 1)) {
  27.       hassent = 1;
  28.       putchar('0' + disp);
  29.     }
  30.   }
  31. }
  32.  
  33. void errorPrint(unsigned int error)
  34. {
  35.   switch (error)
  36.   {
  37.     case 2:
  38.       printf ("02 SHUT_RDWR\n\r");
  39.       break;
  40.     case 4:
  41.       printf ("04 ERR_INTR\n\r");
  42.       break;
  43.     case 23:
  44.       printf ("23 ERR_NFILE\n\r");
  45.       break;
  46.     case 35:
  47.       printf ("35 ERR_EAGAIN or ERR_EWOULDBLOCK\n\r");
  48.       break;
  49.     case 37:
  50.       printf ("37 ERR_ALREADY\n\r");
  51.       break;
  52.     case 38:
  53.       printf ("38 ERR_NOTSOCK\n\r");
  54.       break;
  55.     case 40:
  56.       printf ("40 ERR_EMSGSIZE\n\r");
  57.       break;
  58.     case 41:
  59.       printf ("41 ERR_PROTOTYPE\n\r");
  60.       break;
  61.     case 47:
  62.       printf ("47 ERR_AFNOSUPPORT\n\r");
  63.       break;
  64.     case 53:
  65.       printf ("53 ERR_ECONNABORTED\n\r");
  66.       break;
  67.     case 54:
  68.       printf ("54 ERR_CONNRESET\n\r");
  69.       break;
  70.     case 57:
  71.       printf ("57 ERR_NOTCONN\n\r");
  72.       break;
  73.     case 65:
  74.       printf ("65 ERR_HOSTUNREACH\n\r");
  75.       break;
  76.     default:
  77.       printf ("%u UNKNOWN ERROR\n\r", error);
  78.       break;
  79.   }
  80.   YIELD();
  81.   //    do {key = _low_level_get();} while (key == 0);
  82. }
  83.  
  84. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  85. {
  86.   unsigned char socket, retry = 100;
  87.   unsigned int todo;
  88.   todo = OS_NETSOCKET ((family << 8) + protocol);
  89.   if (todo > 32767) {
  90.     printf("OS_NETSOCKET: ");
  91.     errorPrint(todo & 255);
  92.     exit(0);
  93.  
  94.   }
  95.   else {
  96.     socket = ( (todo & 65280) >> 8);
  97.     //printf ("OS_NETSOCKET: Socket #%d created\n\r", socket);
  98.   }
  99.   return socket;
  100. }
  101.  
  102. unsigned char netConnect (unsigned char socket)
  103. {
  104.   unsigned int todo;
  105.  
  106.   targetadr.family  = AF_INET;
  107.   targetadr.porth   = 00;
  108.   targetadr.portl   = 80;
  109.   targetadr.b1      = 217;
  110.   targetadr.b2      = 146;
  111.   targetadr.b3      = 69;
  112.   targetadr.b4      = 13;
  113.  
  114.   todo = OS_NETCONNECT (socket, &targetadr);
  115.   if (todo > 32767)
  116.   {
  117.     printf("OS_NETCONNECT: ");
  118.     errorPrint(todo & 255);
  119.     exit(0);
  120.   } else {
  121.     //printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  122.   }
  123.   return 0;
  124. }  
  125.  
  126.  
  127. unsigned int tcpSend (unsigned char socket, unsigned int messageadr, unsigned int size)
  128. {
  129.   unsigned char retry = 100;
  130.   unsigned int todo;
  131.   readStruct.socket  = socket;
  132.   readStruct.BufAdr  = messageadr;
  133.   readStruct.bufsize = size;
  134.   readStruct.protocol = SOCK_STREAM;
  135.  
  136.  
  137.  
  138. wizwrite:
  139.   todo = OS_WIZNETWRITE (&readStruct);
  140.   if (todo > 32767)
  141.   {
  142.     printf("OS_WIZNETWRITE: ");
  143.     errorPrint(todo & 255);
  144.     if (retry == 0) {
  145.       exit(0);
  146.     }
  147.     retry--;
  148.     goto wizwrite;
  149.   }
  150.   else
  151.   {
  152.     //printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  153.   }
  154.   return todo;
  155. }
  156.  
  157. unsigned int tcpRead (unsigned char socket)
  158. {
  159.   unsigned char retry = 80;
  160.   unsigned int err, todo;
  161.  
  162.   readStruct.socket  = socket;
  163.   readStruct.BufAdr  = (unsigned int)&netbuf;
  164.   readStruct.bufsize = sizeof (netbuf);
  165.   readStruct.protocol = SOCK_STREAM;
  166.  
  167. wizread:
  168.   todo = OS_WIZNETREAD (&readStruct);
  169.   err = todo & 255;
  170.   if (todo > 32767)
  171.   {
  172.         YIELD();
  173.         retry--;
  174.     if (retry == 0)
  175.         {
  176.         if (err == ERR_EAGAIN) {todo = 0; return todo;}
  177.     printf("OS_WIZNETREAD: ");
  178.     errorPrint(err);
  179.         exit(0);
  180.     }
  181.     goto wizread;
  182.   }
  183.   printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  184.  return todo;
  185. }
  186.  
  187. unsigned int cutHeader(unsigned int todo)
  188. {
  189.   unsigned int q, headlng;
  190.   unsigned char *count;
  191.  
  192.   count = strstr (netbuf, "\r\n\r\n");
  193.   if ( count == NULL)
  194.   {
  195.     printf ("header not found\r\n");
  196.   }
  197.   else
  198.   {
  199.     headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
  200.     q = todo - headlng;
  201.     memcpy (&netbuf, count + 4, q);
  202.     //printf ("header removed. %u bytes\r\n", headlng);
  203.   }
  204.   return q;
  205. }
  206.  
  207. unsigned int netShutDown(unsigned char socket)
  208. {
  209.   unsigned int todo;
  210.   todo = OS_NETSHUTDOWN (socket);
  211.   if (todo > 32767) {
  212.     printf("OS_NETSHUTDOWN: ");
  213.     errorPrint(todo & 255);
  214.     return 255;
  215.   }  else {
  216.     //printf ("Socket #%u closed.\n\r", socket);
  217.   }
  218.   return 0;
  219. }
  220.  
  221. void fillPicture(unsigned char socket)
  222. {
  223.   unsigned int todo, w, pPos, q, headskip;
  224.  
  225.           headskip = 0;
  226.   pPos = 0;
  227.    while (1)
  228.    {
  229.         todo =  tcpRead (socket);
  230.         if (todo == 0) {break;}
  231.     q = todo;
  232.     if (headskip == 0)
  233.     {
  234.       headskip = 1;
  235.       q = cutHeader(todo);
  236.     }
  237.     for (w = 0; w < q; w++)
  238.     {
  239.       picture [w + pPos]  = netbuf[w];
  240.     }
  241.     pPos = pPos + q;
  242.   }
  243.     netShutDown(socket);
  244. }
  245.  
  246.  
  247.  
  248. unsigned char getPic(unsigned long fileId)
  249. {
  250.   unsigned int todo;
  251.   unsigned char cmdlist1[] = "GET \/file\/id:";
  252.   unsigned char cmdlist2[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0";
  253.   unsigned char buffer  [] = "0000000000";
  254.   unsigned char socket;
  255.   socket = OpenSock(AF_INET, SOCK_STREAM);
  256.     todo = netConnect (socket);
  257.                 netbuf[0] = '\0';
  258.                 sprintf(buffer, "%lu", fileId);
  259.                 strcat (netbuf, cmdlist1);
  260.                 strcat (netbuf, buffer);
  261.                 strcat (netbuf, cmdlist2);
  262.   todo = tcpSend (socket, (unsigned int)&netbuf, strlen(netbuf));
  263.   fillPicture(socket);
  264.   return 0;
  265. }
  266. unsigned char savePic(unsigned long fileId)
  267. {
  268.   FILE *fp2;
  269.   unsigned char fileName[32];
  270.   unsigned char buffer  [] = "0000000000";
  271.   strcpy(fileName, "zxart_");
  272.   sprintf(buffer, "%lu", fileId);
  273.   strcat (fileName, buffer);
  274.   strcat (fileName, ".scr");
  275.  
  276.  fp2 = OS_CREATEHANDLE(fileName, 0x80);
  277.   if (((int)fp2) & 0xff)
  278.   {
  279.     printf (fileName);
  280.     printf(" creating error\r\n");
  281.     exit(0);
  282.   }
  283.   OS_WRITEHANDLE(picture, fp2, 6912);
  284.   OS_CLOSEHANDLE(fp2);
  285.   return 0;
  286. }
  287.  
  288. const char* parseJson(unsigned char *property)
  289. {
  290.   unsigned char *count, lng, *start, test;
  291.   int q;
  292.   netbuf[0] = '\0';
  293.   lng = strlen(property);
  294.   count = strstr (picture, property);
  295.   if ( count == NULL)
  296.   {
  297.     printf ("not found\r\n");
  298.         exit(0);
  299.     return "*not found*";
  300.   }
  301.   start = count + lng;
  302.   test  = *start;
  303.   q = -1;
  304. while (((unsigned char)test !='\"') && ((unsigned char)test !=',') && ((unsigned char)test !=']'))
  305. // Нужно более универальное  решениен чтоб запятые в текте не мешались
  306.   {
  307.   q++;
  308.   test  = *start;
  309.   start++;
  310.   }
  311. strncat(netbuf, count + lng, q);  
  312. return netbuf;  
  313. }
  314.  
  315. void convert866(void)
  316. {
  317.   unsigned int lng, targetPos, w, q = 0;
  318.   unsigned char buffer[8], one, two;
  319.   unsigned int decVal;
  320.   lng = strlen(netbuf);
  321.   targetPos = lng + 1;
  322.  
  323.  while (q < lng)
  324.   {
  325.         one = netbuf [q];
  326.         two = netbuf [q + 1];
  327.         if ( one == 92 && two == 117)
  328.         {
  329.         q = q + 2;
  330.         for (w = 0; w < 4; w++)  {buffer [w] = netbuf [q + w];}
  331.         q = q + 4;
  332.         buffer[4] = '\0';
  333.         decVal= (unsigned int)strtol(buffer, NULL, 16);
  334.                                                                                                         //printf("hex %s\n\r",buffer);
  335.                                                                                                         //printf("dec %u\n\r",decVal);
  336.        
  337.        
  338.           if (decVal < 1088) {decVal = decVal - 912;}
  339.           if (decVal > 1087) {decVal = decVal - 864;}
  340.           if (decVal == 1025) {decVal = 240;}
  341.           if (decVal == 1105) {decVal = 241;}
  342.           netbuf [targetPos] = decVal;
  343.         }
  344.                 else {netbuf [targetPos] = netbuf [q]; q++;}
  345.         targetPos++;
  346.         }
  347.         netbuf [targetPos] = '\0';
  348.  
  349.   for (w = lng + 1; w < targetPos + 1; w++)
  350.   {
  351.   netbuf [w - lng -1] = netbuf [w];
  352.    }
  353. }      
  354.  
  355.  
  356.  
  357.  
  358.  
  359. unsigned long processJson(unsigned long startPos)
  360. {
  361.   unsigned int todo, pPos, headskip;
  362.   unsigned char cmdlist1[] = "GET /api/export:zxPicture\/filter:zxPicture\/limit:1\/start:";
  363.   unsigned char cmdlist2[] = "\/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0";
  364.   unsigned char buffer  [] = "000000000";
  365.   unsigned char *count, socket;
  366.   const char *titleptr;
  367.   unsigned long idpic, bytecount;
  368.  
  369.  
  370.   socket = OpenSock(AF_INET, SOCK_STREAM);
  371.   netConnect (socket);
  372.    
  373.           netbuf[0] = '\0';
  374.       strcat (netbuf, cmdlist1);
  375.           sprintf(buffer, "%lu", startPos);
  376.           strcat (netbuf, buffer);
  377.           strcat (netbuf, cmdlist2);
  378.              
  379.           todo = tcpSend (socket, (unsigned int)&netbuf, strlen(netbuf));
  380.          
  381.  
  382.          
  383.   headskip = 0;
  384.   pPos = 0;
  385.   fillPicture(socket);
  386.   netShutDown(socket);
  387.   count = strstr (picture, "responseStatus\":\"success");
  388.   if ( count == NULL)
  389.   {
  390.     printf ("BAD JSON no(responseStatus"":""success)\r\n");
  391.  
  392.         exit(0);
  393.   }
  394.  
  395.   count = strstr (picture, "\"id\":");
  396.   if ( count == NULL)
  397.   {
  398.     printf ("BAD JSON: ID not found \r\n");
  399.         exit(0);
  400.   }
  401.  
  402.  
  403.        
  404.   netbuf[0] = '\0';
  405.   strcat (piclist, parseJson("\"id\":"));
  406.   idpic = atol (netbuf);
  407.   strcat (piclist, "\n\r");
  408.  
  409.   parseJson(",\"title\":\"");
  410.   convert866();
  411.  printf("Title: ");
  412.  printf(netbuf);
  413.  printf("\n\r");
  414.  
  415.  strcat (piclist, netbuf) ;
  416.   strcat (piclist, "\n\r");
  417.   strcat (piclist, parseJson ("\"dateCreated\":"));
  418.   strcat (piclist, "\n\r");
  419.   strcat (piclist, parseJson("\"authorIds\":["));
  420.   strcat (piclist, "\n\r");
  421.   strcat (piclist, parseJson("\"rating\":\""));
  422.   strcat (piclist, "\n\r");
  423.   strcat (piclist, parseJson("\"views\":\""));
  424.   strcat (piclist, "\n\r");
  425.   strcat (piclist, parseJson("\"year\":\""));
  426.   strcat (piclist, "\n\r");
  427. //convert866();
  428.  
  429. return idpic;
  430. }
  431.  
  432.  
  433.  
  434.  
  435. C_task main (void)
  436. {
  437.   unsigned char errno;
  438.   unsigned long iddqd, count;
  439.   os_initstdio();
  440.  
  441.  piclist[0] = '\0';
  442.  
  443.  
  444. for (count = 0; count < 20;count++)
  445. {
  446.   iddqd = processJson(count);
  447.  // printf ("\n\rProcess %lu json \n\r", iddqd);
  448.  
  449.  errno = getPic(iddqd);
  450.   printf ("PicDownloaded %lu,  %u\n\r",iddqd, errno);
  451.  savePic(iddqd);
  452. }
  453. }
  454.  
  455.