Commit aedab946dc5d6038c92357933af5263d5789026e

Added Weather

The bot will now search Google for the weather
src/w.mod
(57 / 1)
  
1616IMPORT ProgExec;
1717IMPORT StringParse, StreamFile, Strings;
1818IMPORT Timers, TextIO;
19IMPORT util;
19IMPORT util, p6Fifo;
2020
2121PROCEDURE wakeupIB(parse : Message.Parse);
2222(* have the bot wake ib up *)
4343 Message.PrintLine(parse.chan, "Sorry, I can't log in to ib right now.");
4444 END;
4545END wakeupIB;
46
47PROCEDURE Weather(parse: Message.Parse);
48(* uses Google to get the weather in some location *)
49CONST
50 call = "lynx -dump -width=200 -nolist -accept_all_cookies 'http://www.google.com/search?q=weather+";
51 (* Look for Humidity, grab it and the previous 4 lines, remove newlines,
52 remove 'Add to iGoogle', convert multiple spaces to a single space. *)
53 filter = "| grep -B5 Humidity | tr -d '\n' |sed 's/Add\ to\ iGoogle//' |sed 's/^ *//;s/ *$//;s/ \{1,\}/ /g' ";
54
55VAR
56 command, null, target, result, str: Message.strType;
57 res, pos : CARDINAL;
58 done, found: BOOLEAN;
59
60BEGIN
61 util.CmdUsed(parse);
62 command :=""; null := ""; target := ""; result := ""; str :="";
63 (* Extract the target - at the moment, it will only accept the command in lowercase *)
64 StringParse.Split("her ", parse.cmd, null, target);
65 StringParse.Clip(target);
66 StringParse.Strip(target);
67
68 (* Is there actually a target? *)
69 IF (LENGTH(target) < 2) THEN
70 Message.PrintLine(parse.chan, "For what location?");
71 RETURN
72 END;
73
74 (* save target, format changes follow *)
75 str := target;
76 REPEAT
77 StringParse.Replace(target, " ", "+");
78 UNTIL (NOT StringParse.InString(" ", target));
79
80 FormStr.print(command, "%s%s'%s", call, target, filter);
81 (* make the call *)
82 p6Fifo.writeFIFO(command);
83 p6Fifo.readFIFO(result);
84
85 target := str; (* get rid of the the formatting changes *)
86 (* Check string for Humidity, or Current, if it's there, then the return is valid *)
87 IF StringParse.InString("Humidity", result) THEN
88 (* Error in my code somewhere isn't clearing the previous line, so I'm
89 manually trimming everything after the %. Must be in the FIFO, but I can't see where. *)
90 Strings.FindNext("%", result, 0, found, pos);
91 IF found THEN Strings.Delete(result, pos+1, LENGTH(result)) END;
92 str := result;
93 ELSE
94 FormStr.print(str, "I'm sorry, I couldn't find any weather for %s", target);
95 END;
96
97 Message.PrintLine(parse.chan, str);
98EXCEPT
99 Message.PrintLine(parse.chan, "For what location?");
100 RETURN;
101END Weather;
46102
47103(* whatis (man -f) *)
48104PROCEDURE whatis(parse : Message.Parse);