{--- 此行必须存在 --------------------------------------------------------------------------
WeatherNetName=天气在线
WeatherMeaning=预报7天天气信息，支持国内绝大部分的城市
CityFile=t7online.txt
----------------------------------------------------------------------------
 Unit Name: t7online.wea
 Author:    8-jie software(仅支持DeskWidget V1.2.8及以上版本)
 Version:   1.1.0.3
 History:
  2009-02-25: * 解决了脚本会出错的问题，并使用阻塞式下载函数以提高脚本效率，降低占用
              * 解决了晚上刷新天气时，会把明天的天气当作今天的显示，导致所有天气都错一天的问题
  2009-02-22: * 修改了读取失败后的仍显示刷新天气成功的问题
  2008-12-24: * 解决了分析天气描述信息有时会出错的问题
  2008-11-24: + create this file
----------------------------------------------------------------------------
-----------------------------------------------------------------------------}

const
  DEF_NULLVALUE=-1010;
  
var
  s,WeatherFileName:string;
  aurl:string;
  lst:TStrings;
  //此天气服务器支持的最大天数
  MaxDays:integer;
  CityCode:string;

function StrToWeather(aValue:string):string;
var
  sMajor,sMinor:string;
  wType:string;
begin
  wType:='NA';
  result:='NA';
  //parse value
  if trim(aValue)='' then exit;
  sMajor:=copy(aValue,1,2);
  sMinor:=copy(aValue,3,length(aValue)-2);
  //Major
  if sMajor='so' then wType:='Sunny'
  else if sMajor='ms' then wType:='CloudyLightPartly'
  else if sMajor='wb' then wType:='CloudyPartly'
  else if sMajor='bw' then wType:='CloudyMostly'
  else if sMajor='bd' then wType:='Cloudy'
  else if sMajor='mo' then wType:='Night_Clear'
  else if sMajor='mm' then wType:='Night_CloudyLightPartly'
  else if sMajor='mb' then wType:='Night_CloudyPartly'
  else if sMajor='mw' then wType:='Night_CloudyMostly';
  //Minor
  if (sMinor='r1') or (sMinor='r2') or (sMinor='r3') then
  begin
    if sMinor='r1' then
      wType:='RainLight'
    else if sMinor='r3' then
      wType:='RainHeavy'
    else
      wType:='Rain';
  end
  else if (sMinor='s1') or (sMinor='s2') or (sMinor='s3') then
  begin
    if sMinor='s1' then
      wType:='ShowerLight'
    else if sMinor='s3' then
      wType:='ShowerHeavy'
    else
      wType:='Shower';
  end
  else if (sMinor='sn1') or (sMinor='sn2') or (sMinor='sn3') then
  begin
    if sMinor='sn1' then
      wType:='SnowLight'
    else if sMinor='sn3' then
      wType:='SnowHeavy'
    else
      wType:='Snow';
  end
  else if (sMinor='g1') or (sMinor='g2') or (sMinor='g3') then
  begin
    if sMinor='g1' then
      wType:='LighteningLight'
    else if sMinor='g3' then
      wType:='Thunderstorms'
    else
      wType:='Lightening';
  end;
  result:=wType;
end;

//delete string trail char:'_'
function DelStr_(aStr:string):string;
var
  j:integer;
begin
  result:='';
  for j:=length(aStr)-1 downto 0 do
    if aStr[j]<>'_' then
    begin
      result:=copy(aStr,1,j);
      break;
    end;
end;

function ParseString(value: string):boolean;
var
  lst:TStrings;
  iBase,
  i,ipos:integer;
  s,s1,sSec:string;
  inifn:TIniFile;
  isNightInfo:boolean; //此值为真时，现在读取的其实是明天的天气信息
  y,m,d:word;
begin
  result:=false;
  if trim(value)='' then exit;
  iBase:=0;
  lst:=TStringList.Create;
  inifn:=TIniFile.Create(WeatherFileName);
  try
    lst.Text:=value; //UTF8Decode(value);
    //showmessage('begin parse'+inttostr(lst.count));
    lst.SaveToFile('weather.htm');
    if lst.Count<=0 then exit;
    
    inifn.WriteFloat('system','LastUpdate',now);
    inifn.WriteInteger('system','DayNums',MaxDays);
    inifn.WriteInteger('Today','Tmp',DEF_NULLVALUE);
    inifn.WriteInteger('Today','Flik',DEF_NULLVALUE);
    inifn.WriteInteger('Today','hmid',DEF_NULLVALUE);

    for i:=0 to lst.Count-1 do
    begin
      if pos('最高温度',lst.strings[i])>0 then
      begin
        s:=StrBetween(lst.strings[i-19],'dt">','</td>');
        DecodeDate(now,y,m,d);
        isNightInfo:=s<>format('%d月%d日',[m,d]);
        for j := 0 to 6 do
        begin
          if isNightInfo then sSec:='Day'+inttostr(j+1)
            else sSec:='Day'+inttostr(j);
          inifn.WriteString(sSec,'hi',StrBetween(lst.Strings[i+1+j],'">','&deg'));
        end
      end
      else if pos('最低温度',lst.strings[i])>0 then
      begin
        for j := 0 to 6 do
        begin
          if isNightInfo then sSec:='Day'+inttostr(j+1)
            else sSec:='Day'+inttostr(j);
          inifn.WriteString(sSec,'low',StrBetween(lst.Strings[i+1+j],'">','&deg'));
        end
      end
      else if Pos('上午',lst.strings[i])>0 then
      begin
        for j := 0 to 6 do
        begin
          if isNightInfo then sSec:='Day'+inttostr(j+1)
            else sSec:='Day'+inttostr(j);
          s:=DelStr_(StrBetween(lst.Strings[i+1+j],'standard/','.gif'));
          if s<>'' then
            inifn.WriteString(sSec,'am_wType',StrToWeather(s));
          s:=StrBetween(lst.Strings[i+1+j],'title="','">');
          if s<>'' then
            inifn.WriteString(sSec,'am_desc',trim(s));
        end;
      end
      else if Pos('下午',lst.strings[i])>0 then
      begin
        for j := 0 to 6 do
        begin
          if isNightInfo then sSec:='Day'+inttostr(j+1)
            else sSec:='Day'+inttostr(j);
          s:=DelStr_(StrBetween(lst.Strings[i+1+j],'standard/','.gif'));
          if s<>'' then
            inifn.WriteString(sSec,'pm_wType',StrToWeather(s));
          s:=StrBetween(lst.Strings[i+1+j],'title="','">');
          if s<>'' then
            inifn.WriteString(sSec,'pm_desc',trim(s));
        end;
      end
      else if Pos('晚上',lst.strings[i])>0 then
      begin
        for j := 0 to 6 do
        begin
          if isNightInfo then sSec:='Day'+inttostr(j+1)
            else sSec:='Day'+inttostr(j);
          s:=DelStr_(StrBetween(lst.Strings[i+1+j],'standard/','.gif'));
          if s<>'' then
            inifn.WriteString(sSec,'night_wType',StrToWeather(s));
          s:=StrBetween(lst.Strings[i+1+j],'title="','">');
          if s<>'' then
            inifn.WriteString(sSec,'night_desc',trim(s));
        end;
      end
      else if Pos('蒲福级',lst.strings[i])>0 then
      begin
        for j := 0 to 6 do
        begin
          if isNightInfo then sSec:='Day'+inttostr(j+1)
            else sSec:='Day'+inttostr(j);
          s:=StrBetween(lst.Strings[i+1+j],'">','</td>');
          sDirection:=StrBefore(' ',s);
          sLevel:=StrAfter(' ',s);
          if s<>'' then
          begin
            inifn.WriteString(sSec,'am_Wind_Direction',sDirection);
            inifn.WriteString(sSec,'pm_Wind_Direction',sDirection);
            inifn.WriteString(sSec,'night_Wind_Direction',sDirection);
            inifn.WriteString(sSec,'am_Wind_Level',sLevel);
            inifn.WriteString(sSec,'pm_Wind_Level',sLevel);
            inifn.WriteString(sSec,'night_Wind_Level',sLevel);
          end;
        end;
      end
    end;
    result:=true;
  finally
    lst.Free;
    inifn.free;
  end;
end;

procedure RefreshWeatherInfo(aParams:string);
var
  lst:TStrings;
  City,s,sid:string;
begin
  MaxDays:=7;
  lst:=TStringList.create;
  try
    //分析参数
    lst.text:=StrToStrings(aParams,';');
    WeatherFileName:=DataPath+'WeatherInfo.ini';
    City:=lst.Values['City'];
    CityCode:=lst.Values['CityCode'];
  finally
    lst.free;
  end;
  //            http://www.t7online.com//cgi-bin/region?PRG=citybild&CONT=cncn&CREG=hkch&LAND=CI&LANG=cn&WMO=54511
  aurl:=format('http://www.t7online.com//cgi-bin/region?PRG=citybild&CONT=cncn&CREG=hkch&LAND=CI&LANG=cn&WMO=%s',[CityCode]);
  s:='';
  if DownLoadFile(aurl,s,true) then
  begin
    sid:=StrBetween(s,'SID=','" ');
  	if (sid<>'') then 
    begin
      //            http://www.t7online.com/cgi-bin/www/cn/city7daycn?LANG=cn&WMO=54511&PROVIDER=anwendung&SID=5451128e93f068bcc735d1b6aea6c17debd97b
      aurl:=format('http://www.t7online.com/cgi-bin/www/cn/city7daycn?LANG=cn&WMO=%s&PROVIDER=anwendung&SID=%s',[CityCode,sid]);
      if DownLoadFile(aurl,s,true) and ParseString(s) then
        RefreshWidgetWeatherInfo
      else
        RefreshWidgetWeatherInfo(false);
    end
    else
      RefreshWidgetWeatherInfo(false);
  end
  else
    RefreshWidgetWeatherInfo(false);
  ScriptFinished:=true;
end;
