回精華區
作者hialan.bbs@processor.tfcis.org (hialan) 看板: plan
標題yahoo 線上字典
時間動力核心 (2004/02/16 Mon 03:09:02) Updated: 2004/02/16
之前 chwaian 提供了好幾個..

我把原先 dreye.c 改一改讓他支援 Yahoo 的線上字典

參考站:
telnet://at.twbbs.org --> 各種服務 -> 育樂中心 -> 奇摩字典


/*-------------------------------------------------------*/
/* dict.c        (YZU WindTopBBS Ver 3.02 )              */
/*-------------------------------------------------------*/
/* target : Yahoo 線上字典                               */
/* create : 01/07/09                                     */
/* update :   /  /                                       */
/* author : statue.bbs@bbs.yzu.edu.tw                    */
/* change : hialan.bbs@venice.twbbs.org                  */
/*-------------------------------------------------------*/

#if 0

  普通
  http://tw.dictionary.yahoo.com/word/hello

  片語
  http://tw.dictionary.yahoo.com/word/hello?t=i

  同義字/反義字
  http://tw.dictionary.yahoo.com/word/hello?t=s

#endif


#include "bbs.h"

#ifdef HAVE_NETTOOL

#define mouts(y,x,s)        { move(y,x); outs(s); }

#define HTTP_PORT           80
#define SERVER_yahoo        "tw.dictionary.yahoo.com"
#define CGI_yahoo           "/word/"
#define REF                "http://tw.dictionary.yahoo.com/"


static void
url_encode(dst, src)        /* URL encoding */
  uschar *dst;              /* Thor.990331: 要 src 的三倍空間 */
  uschar *src;
{
  for (; *src; src++)
  {
    if (*src == ' ')
      *dst++ = '+';
    else if (is_alnum(*src))
      *dst++ = *src;
    else
    {
      register cc = *src;
      *dst++ = '%';
      *dst++ = radix32[cc >> 4];
      *dst++ = radix32[cc & 0xf];
    }
  }
  *dst = '\0';
}


static void
write_file(sockfd, fp)
  int sockfd;
  FILE *fp;
{
  static char pool[2048];
  int cc, i;
  char *xhead, *xtail;
  int show, start_show;
  int space;                /* 在 html 中,連續的 space 只會算一次 */

  char *start_str[] =
  {
    "<!-- view content_start-->",
    "<!-- end show top-->",
    "<!--end 650 footer-->",
    NULL
  };

  char *stop_str[] =
  {
    "<!-- show top-->",
    "<!--begin 650 footer-->",
    NULL
  };

  char *newline_str[] =         /* 取代換行字元的符號 */
  {
    "<br>",
    "</td>",
    "</li>",
    "<blockquote>",
    NULL
  };

  /* parser return message from web server */
  xhead = pool;
  xtail = pool;
  show = 1;
  start_show=0;
  space = 0;

  for (;;xhead++)
  {
    if (xhead >= xtail)
    {
      xhead = pool;
      cc = read(sockfd, xhead, sizeof(pool));
      if (cc <= 0)
        break;
      xtail = xhead + cc;
    }

    if (!start_show)
    {
      for (i = 0; start_str[i] != NULL; i++)
      {
        if (!str_ncmp(xhead, start_str[i], strlen(start_str[i])))
        {
          start_show = 1;
          xhead += strlen(start_str[i]);
          break;
        }
      }
    }
    else if (start_show)
    {
      for (i = 0; stop_str[i] != NULL; i++)
      {
        if (!str_ncmp(xhead, stop_str[i], strlen(stop_str[i])))
        {
          start_show = 0;
          xhead += strlen(stop_str[i]);
          break;
        }
      }
    }

    if (!start_show)
      continue;

    for (i = 0; newline_str[i] != NULL; i++)
    {
      if (!str_ncmp(xhead, newline_str[i], strlen(newline_str[i])))
      {
        fputc('\n', fp);
        xhead += strlen(newline_str[i]);
        space = 0;
        break;
      }
    }

    if (!str_ncmp(xhead, "&nbsp;", 6))
    {
      fputc(' ', fp);
      xhead += 6;
      space = 0;
    }

    if (!str_ncmp(xhead, "&#169;", 6))
    {
      fputs("(C)", fp);
      xhead += 6;
      space = 0;
    }

    if (!str_ncmp(xhead, "<li>", 4))
    {
      fputs("  ˙", fp);
      xhead += 4;
      space = 0;
    }

    /* 標籤略過 */

    cc = *xhead;
    switch(cc)
    {
    case '<':
      show = 0;
      continue;
    case '>':
      show = 1;
      continue;
    case '\n':
    case '\r':
      continue;
    case ' ':
      if (space)
        continue;
      space = 1;
    }

    if (show)
      fputc(cc, fp);

    if (cc != ' ')
      space = 0;
  }
  fputc('\n', fp);
}


static int
http_conn(server, s)
  char *server;
  char *s;
{
  int sockfd;
  FILE *fp;
  char fname[64], *str;

  if ((sockfd = dns_open(server, HTTP_PORT)) < 0)
  {
    vmsg("無法與伺服器取得連結,查詢失敗");
    return 0;
  }
  else
  {
    mouts(22, 0, "正在連接伺服器,請稍後(按任意鍵離開).............");
    refresh();
  }
  write(sockfd, s, strlen(s));
  shutdown(sockfd, 1);

  sprintf(fname, "tmp/%s.yahoo_dict", cuser.userid);

  fp = fopen(fname, "w");

  str = strchr(s + 4, ' ');

  if (str)
    *str = '\0';
  fprintf(fp, "該頁連結: http://%s%s\n\n", server, s + 4);

  write_file(sockfd, fp);
  fclose(fp);

  close(sockfd);
  more(fname, NULL);
  unlink(fname);
  return 0;
}


static void
yahoo_dict(word, ans)
  char *word;
  char ans;
{
  char atrn[256], sendform[512];
  char ue_word[90];

  url_encode(ue_word, word);

  if (ans == '2' || ans == '3')
    sprintf(atrn, "%s?t=%c", ue_word, ans == '2' ? 's' : 'i');
  else
    sprintf(atrn, "%s", ue_word);

  sprintf(sendform, "GET %s%s HTTP/1.0\n\n", CGI_yahoo, atrn);

  http_conn(SERVER_yahoo, sendform);
}


int
main_dreye()
{
  char ans[5];
  char word[30];

  while (1)
  {
    clear();
    move(0, 23);
    outs("\033[1;37;44m◎ Yahoo! 線上字典 v0.1 ◎\033[m");
    move(3, 0);
    outs("此字典來源為 Yahoo! 線上字典。\n");
    prints("WWW: %s\n", REF);
    outs("author: statue.bbs@bbs.yzu.edu.tw\n");
    if (!vget(8, 0, "查詢字彙:", word, 30, DOECHO))
      break;

    if (vget(9, 0, "1)意義 2)片語 3)同義字/反義字 Q)離開 [1] ", ans, 3, LCECHO) == 'q')
      break;

    yahoo_dict(word, ans[0]);
  }

  return 0;
}
#endif  /* HAVE_NETTOOL */

--
  Origin:  Maple-itoc˙動力核心  processor.tfcis.org 
  Author: hialan 61-229-111-30.HINET-IP.hinet.net 發表
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻