回精華區
作者itoc (太陽花兒~), 信區: plan
標題[功能] ESPN 節目表
時間Sun Dec 15 16:56:01 2002 Updated: 2003/05/04
: menu.c 在適當的選單加上

+ "bin/espn.so:main_espn", 0, - M_XMODE,
+ "ESPN Star  ♂ ESPN節目 ♀",

: game/Makefile

SO =    ... espn.so

: game/espn.c 新增這程式

/*-------------------------------------------------------*/
/* espn.c   ( NTHU CS MapleBBS Ver 3.10 )                */
/*-------------------------------------------------------*/
/* target : EPSN 節目表                                  */
/* create : 02/12/15                                     */
/* update :   /  /                                       */
/* author : itoc.bbs@bbs.tnfsh.tn.edu.tw                 */
/*-------------------------------------------------------*/


#if 0

 要使用 espn.c 必須裝有 lynx,注意 lynx 的路徑要和程式吻合。

 資料來源:ESPN

 URL 格式範例:

 http://jws.startv.com/espn/search_act.cfm?Country=21&Channel=ESPN&
 SportsTypes=BK&From=20021201&To=20021231&btnSubmit.x=67&btnSubmit.y=6

 Country=21     => Taiwan
 Channel=ESPN   => EPSN或是STAR
 SportsTypes=BK => BasketBall
 From=20021201  => 2002/12/01
 To=20021231    => 2002/12/31
 btnSubmit.x=67&btnSubmit.y=6 => view

#endif


#include "bbs.h"


#ifdef HAVE_NETTOOL


#define LYNX_PATH       "/usr/local/bin/lynx --source"  /* lynx 的絕對路徑 */


/*-------------------------------------------------------*/
/* 分析 html                                             */
/*-------------------------------------------------------*/


static char *
strstr2(big, little)
  char *big, *little;
{
  char *str;

  /* 若有 strstr(big, little) 把指標放在最後的下一格 */
  if (str = strstr(big, little))
    return str + strlen(little);

  return NULL;
}


static int wlen;    /* 本行有多少字 */


static int
foutc(fp, ch)
  FILE *fp;
  int ch;
{
  if (ch == '<' || ch == '\n' || ch == '\0')
  {
    ch = '\0';
  }
  else
  {
    if (++wlen > 60)    /* 一行只印 60 字 */
    {
      fprintf(fp, "\n          ");
      wlen = 0;
    }
    fprintf(fp, "%c", ch);
  }

  return ch;
}


static void
fouts(fp, str)      /* 印出 str */
  FILE *fp;
  char *str;
{
  int ch;

  wlen = 0;

  while (ch = *str)
  {
    if (!foutc(fp, ch))
      break;
    str++;
  }
}


static void
fprints(fp, str)
  FILE *fp;
  char *str;
{
  static int line = 0;

  line = (line + 1) % 5;

  if (line == 1)    /* Date */
  {
    fprintf(fp, "撥出時間:");
    fouts(fp, str);
  }
  else if (line == 2)   /* Time */
  {
    fprintf(fp, "  ");
    fouts(fp, str);
  }
  else if (line == 3)   /* Sport */
  {
    fprintf(fp, "  ");
    fouts(fp, str);
    fprintf(fp, "\n");
  }
  else if (line == 4)   /* Event Title */
  {
    fprintf(fp, "節目內容:");
    fouts(fp, str);
    fprintf(fp, "\n");
  }
  else          /* Duration */
  {
    fprintf(fp, "節目歷時:");
    fouts(fp, str);
    fprintf(fp, "\n" MSG_SEPERATOR "\n");
  }
}


static void
html_fetch(fpath, fpw, now, year, month)
  char *fpath;
  FILE *fpw;
  time_t now;
  int year, month;
{
  FILE *fpr;
  char buf[256], *str;

  if (!(fpr = fopen(fpath, "r")))
    return;

  fprintf(fpw, "作者: %s (%s) 站內: ESPN 節目\n", cuser.userid, cuser.username);
  fprintf(fpw, "標題: ESPN 節目表 (%d/%02d)\n", year, month);
  fprintf(fpw, "時間: %s\n\n", Btime(&now));

  while (fgets(buf, 256, fpr))
  {
    if (str = strstr2(buf, "<td nowrap class=\"TDBODY"))
    {
      str += 3;
      fprints(fpw, str);
    }
    else if (str = strstr2(buf, "<td class=\"TDBODY"))
    {
      str += 3;
      if (!str_ncmp(str, "<b>", 3)) /* 跳過 </b> */
        str += 3;
      fprints(fpw, str);
    }
  }

  fclose(fpr);

  fprintf(fpw, "\n--\n資料來源:ESPN\n");
}


/*-------------------------------------------------------*/
/* 主程式                                                */
/*-------------------------------------------------------*/


int
main_espn()
{
  char fpath[64], cmd[256], *fname, *channel;
  time_t now;
  struct tm *ptime;
  int year, month, kind;
  FILE *fp;

  int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                                        /* 1~12 月各有幾天 */

  char sports[9][3] =
  {
    "BK", "BB", "SO",       /* Basketball Baseball Soccer */
    "BA", "TN", "TB",       /* Badmiton   Tennnis  Table_Tennis */
    "VB", "BI", "GO",       /* Volleyball Billiard Golf */
  };

  time (&now);
  ptime = localtime(&now);

  year = ptime->tm_year + 1900;
  month = ptime->tm_mon + 1;

  switch (vans("查詢 1)本月 2)下月 的節目表?[Q] "))
  {
  case '2':
    if (month == 12)
    {
      year++;
      month = 1;
    }
    else
    {
      month++;
    }
    break;
  case '1':
    break;
  default:
    return XEASY;
  }

  switch (vans("查詢 1)ESPN 2)STAR 的節目表?[Q] "))
  {
  case '1':
    channel = "ESPN";
    break;
  case '2':
    channel = "STAR";
    break;
  default:
    return XEASY;
  }

  kind = vans("球類 1)籃 2)棒 3)足 4)羽 5)網 6)桌 7)排 8)撞 9)高 [Q] ") - '1';
  if (kind < 0 || kind > 9)
    return XEASY;

  if (month == 2)
  {
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)/* 二月是閏月 */
      day[1] = 29;
  }

  outz("網頁連結中,請稍候\033[5m...\033[m (連去美國比較久)");
  refresh();

  /* 暫存檔:tmp/ispn.userid 是原來的 html 檔
             tmp/espn.userid 是轉過的文字檔 */
  sprintf(fpath, "tmp/espn.%s", cuser.userid);
  if (!(fp = fopen(fpath, "w")))
    return XEASY;
  fname = fpath + 4;
  *fname = 'i';

  sprintf(cmd, LYNX_PATH " \"http://jws.startv.com/espn/search_act.cfm?Country=21&Channel=%s&SportsTypes=%s&From=%d%02d01&To=%d%02d%d&btnSubmit.x=67&btnSubmit.y=6\" > %s",
    channel, sports[kind], year, month, year, month, day[month - 1], fpath);
  system(cmd);

  html_fetch(fpath, fp, now, year, month);
  fclose(fp);

  *fname = 'e';
  if (more(fpath, (char *) -1) >= 0)
  {
    if (vans("您要將查詢結果寄回信箱嗎?[N] ") == 'y')
      mail_self(fpath, cuser.userid, "[備 忘 錄] ESPN 節目表查詢", 0);
  }

  /* 清除暫存檔 */
  unlink(fpath);
  fpath[4] = 'i';
  unlink(fpath);

  return 0;
}
#endif  /* HAVE_NETTOOL */

--
□ 本文章由 itocitoc.Dorm-GD2.NCTU.edu.tw 發表
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻