function get_text($title, $section, $return = False, $return_lines_array = False) { if ($title == "") { wiki_privmsg($return, "wiki: get_text=invalid title"); return False; } $index = -1; $title = str_replace(" ", "_", $title); if ($section != "") { $uri = "/w/api.php?action=parse&format=php&page=" . urlencode($title) . "&prop=sections"; $response = wget(WIKI_HOST, $uri, 80, WIKI_USER_AGENT); $data = unserialize(strip_headers($response)); if (isset($data["parse"]["sections"]) == False) { wiki_privmsg($return, "wiki: get_text=error getting sections for page \"" . $title . "\""); return False; } $sections = $data["parse"]["sections"]; for ($i = 0; $i < count($sections); $i++) { $line = $sections[$i]["line"]; if (strtolower($section) == strtolower($line)) { $index = $sections[$i]["index"]; break; } } } $uri = "/w/api.php?action=parse&format=php&page=" . urlencode($title) . "&prop=text"; if ($index > 0) { $uri = $uri . "§ion={$index}"; } /*$url="http://".WIKI_HOST.$uri; $url=get_redirected_url($url); if (get_host_and_uri($url,&$host,&$uri,&$port)==False) { wiki_privmsg($return,"wiki: get_text=url parse failed"); return False; }*/ $response = wget(WIKI_HOST, $uri, 80, WIKI_USER_AGENT); $data = unserialize(strip_headers($response)); if (isset($data["parse"]["text"]["*"]) == True) { $text = $data["parse"]["text"]["*"]; if ($section != "") { $id = str_replace(" ", "_", $section); $id = str_replace("~", ".7E", $id); $id = str_replace("(", ".28", $id); $id = str_replace(")", ".29", $id); $head = "<span class=\"mw-headline\" id=\"{$id}\">{$section}</span>"; if (strpos($text, $head) === False) { wiki_privmsg($return, "wiki: get_text=section span not found"); return False; } } } else { wiki_privmsg($return, "wiki: get_text=section not found"); return False; } strip_comments($text); strip_all_tag($text, "h2"); strip_all_tag($text, "h3"); $text = strip_tags($text); $text = trim($text, " \t\n\r\v\""); $br = random_string(30); $text = str_replace("\n", $br, $text); $text = replace_ctrl_chars($text, " "); $text = html_decode($text); $text = clean_text($text); $url = "http://wiki.soylentnews.org/wiki/" . urlencode($title); if ($section != "") { $url = $url . "#{$id}"; } if ($return_lines_array == False) { $text = str_replace($br, " ", $text); $text = clean_text($text); if (strlen($text) > 400) { $text = trim(substr($text, 0, 400)) . "..."; } bot_ignore_next(); wiki_privmsg($return, $text); wiki_privmsg($return, $url); $result = $text; } else { $result = explode($br, $text); for ($i = 0; $i < count($result); $i++) { $result[$i] = trim($result[$i]); if (strlen($result[$i]) > 300) { $result[$i] = trim(substr($result[$i], 0, 300)) . "..."; } } delete_empty_elements($result); $result[] = $url; } return $result; }
function parse_xml($html) { $parts = explode("<story", $html); array_shift($parts); $items = array(); for ($i = 0; $i < count($parts); $i++) { $item = array(); $item["type"] = "xml_story"; $item["title"] = extract_raw_tag($parts[$i], "title"); $item["title"] = html_decode($item["title"]); $item["title"] = html_decode($item["title"]); $item["title"] = replace_ctrl_chars($item["title"], " "); $item["title"] = str_replace(" ", " ", $item["title"]); $url = str_replace("&", "&", strip_ctrl_chars(extract_raw_tag($parts[$i], "url"))); term_echo("*** raw story url: " . $url); $item["url"] = get_redirected_url($url); $item["timestamp"] = time(); if ($item["title"] === False or $item["url"] === False) { continue; } $items[] = $item; } return $items; }
$host = "www.just-one-liners.com"; $port = 80; if (mt_rand(0, 4) == 0) { $uri = "/"; } else { $uri = "/category/confucius-say-wordplay"; } $response = wget($host, $uri, $port, $agent); $delim1 = "<h2 class=\"title\" id=\"post-"; $delim2 = "</h2>"; $text = extract_text($response, $delim1, $delim2); if ($text === False) { return; } $i = strpos($text, "<"); if ($i === False) { return; } $text = substr($text, $i); $text = replace_ctrl_chars($text, " "); $text = trim(strip_tags($text)); $text = str_replace(" ", " ", $text); $text = html_decode($text); $text = html_decode($text); $text_len = strlen($text); $max_text_length = 300; if (strlen($text) > $max_text_length) { $text = trim(substr($text, 0, $max_text_length)) . "..."; } privmsg($text); #####################################################################################################
$response = wget("soylentnews.org", "/"); $delim1 = "<!-- begin site_news block -->"; $delim2 = "<!-- end site_news block -->"; $max_len = 300; $text = extract_text($response, $delim1, $delim2); $parts = explode("<hr>", $text); $result = ""; for ($i = 0; $i < count($parts); $i++) { if (strpos(strtolower($parts[$i]), "meeting") !== False) { $result = $parts[$i]; } } if ($result != "") { term_echo($result); $result = strip_tags($result); $result = replace_ctrl_chars($result, " "); $result = str_replace(" ", " ", $result); if (strlen($result) > $max_len) { $result = trim(substr($result, 0, $max_len)) . "..."; } } else { require_once "wiki_lib.php"; $title = "Issues to Be Raised at the Next Board Meeting"; $section = "Next meeting"; $result = get_text($title, $section, True); var_dump($result); if (is_array($result) == True) { $result = trim(implode(" ", $text)); } if ($result == "") { return;
function source_define($host, $term, $params) { global $debug; $sterm = $term; if ($params["space_delim"] != "") { $sterm = str_replace(" ", $params["space_delim"], $sterm); } $uri = str_replace($params["template"], urlencode($sterm), $params["uri"]); term_echo("*** DEFINE: trying {$host}{$uri} on port " . $params["port"]); $response = wget($host, $uri, $params["port"], ICEWEASEL_UA, "", 20); $html = strip_headers($response); $html = replace_ctrl_chars($html, " "); strip_all_tag($html, "head"); strip_all_tag($html, "script"); if ($debug == "ON") { privmsg("debug [{$host}]: uri = \"{$uri}\""); $L = strlen($html); privmsg("debug [{$host}]: html length = \"{$L}\""); unset($L); privmsg("debug [{$host}]: delim_start = \"" . $params["delim_start"] . "\""); privmsg("debug [{$host}]: delim_end = " . $params["delim_end"] . "\""); } $i = strpos($html, $params["delim_start"]); $def = ""; if ($i !== False) { if ($debug == "ON") { privmsg("debug [{$host}]: delim_start pos = \"{$i}\""); } $html = substr($html, $i + strlen($params["delim_start"])); $i = strpos($html, $params["delim_end"]); if ($i !== False) { if ($debug == "ON") { privmsg("debug [{$host}]: delim_end pos = \"{$i}\""); } $def = trim(strip_tags(substr($html, 0, $i))); $def = str_replace(array("\n", "\r"), " ", $def); $def = str_replace(" ", " ", $def); if (strlen($def) > MAX_DEF_LENGTH) { $def = trim(substr($def, 0, MAX_DEF_LENGTH)) . "..."; } } } if ($def == "") { $location = exec_get_header($response, "location"); if ($location == "") { return False; } else { $new_term = extract_text($location, $params["get_param"], "&", True); if ($new_term != $term) { term_echo("redirecting to \"{$location}\""); if ($debug == "ON") { privmsg("debug [{$host}]: redirecting to \"{$location}\""); } return source_define($host, $new_term, $params); } else { return False; } } } else { if ($params["ignore"] != "" and strpos($def, $params["ignore"]) !== False) { return False; } if (strpos($def, "There aren't any definitions") !== False) { return False; } privmsg("[" . $params["name"] . "] " . chr(3) . "03{$term}" . chr(3) . ": " . html_decode($def)); return True; } }
$pid_test = extract_text($pid_html, $pid_delim1, $pid_delim2); $pid = ""; $parent_url = ""; if ($pid_test !== False) { $pid = $pid_test; $parent_url = "http://soylentnews.org/comments.pl?sid={$sid}&cid={$pid}"; } $subject_delim1 = "<h4><a name=\"{$cid}\">"; $subject_delim2 = "</a>"; $subject = extract_text($parts[$j], $subject_delim1, $subject_delim2); $subject = trim(strip_tags($subject)); $subject = str_replace(" ", " ", $subject); $subject = html_decode($subject); $subject = html_decode($subject); $comment_body = extract_text($parts[$j], "<div id=\"comment_body_{$cid}\">", "</div>"); $comment_body = replace_ctrl_chars($comment_body, " "); $comment_body = str_replace("</p>", " ", $comment_body); $comment_body = str_replace("<p>", " ", $comment_body); $comment_body = str_replace("<br>", " ", $comment_body); $comment_body = trim(strip_tags($comment_body)); $comment_body = str_replace(" ", " ", $comment_body); $comment_body = html_decode($comment_body); $comment_body = html_decode($comment_body); $record = array(); $record["user"] = $user; $record["uid"] = $uid; $record["score"] = $score; $record["score_num"] = $score_num; $record["subject"] = $subject; $record["title"] = $title; $record["comment_body"] = $comment_body;
function process_weather(&$location, $nick, $getdata = False) { $loc = get_location($location, $nick); term_echo("*** WEATHER LOCATION LOOKUP: {$loc}"); if ($loc === False) { if ($location == "") { return False; } $loc = $location; } $location = $loc; $loc_query = filter($loc, VALID_UPPERCASE . VALID_LOWERCASE . VALID_NUMERIC . " "); $prefs = get_prefs($nick); $fheit = "1"; $use_unit_pref = False; if (isset($prefs["unit"]) == True and $getdata == False) { if ($prefs["unit"] == "metric") { $use_unit_pref = True; $fheit = "0"; } if ($prefs["unit"] == "imperial") { $use_unit_pref = True; } } # https://www.google.com/search?gbv=1&q=weather+traralgon $url = "http://www.google.com.au/search?gbv=1&fheit={$fheit}&q=weather+" . urlencode($loc_query); term_echo($url); $response = wget("www.google.com.au", "/search?gbv=1&fheit={$fheit}&q=weather+" . urlencode($loc_query), 80, ICEWEASEL_UA, "", 60); $html = strip_headers($response); $delim1 = "<div class=\"e\">"; $delim2 = "</table>"; $html = extract_text($html, $delim1, $delim2); if ($html === False) { return False; } $html = replace_ctrl_chars($html, " "); $html = str_replace(" ", " ", $html); $html = html_decode($html); $html = html_decode($html); $location = trim(strip_tags(extract_raw_tag($html, "h3"))); if (substr($location, 0, 12) == "Weather for ") { $location = substr($location, 12); } $wind = trim(strip_tags(extract_text_nofalse($html, "style=\"white-space:nowrap;padding-right:15px;color:#666\">Wind: ", "</span>"))); $humidity = extract_text($html, "style=\"white-space:nowrap;padding-right:0px;vertical-align:top;color:#666\">Humidity: ", "</td>"); $parts = explode("<td", $html); $temps = array(); $tempsC = array(); $conds = array(); $days = array(); for ($i = 1; $i < count($parts); $i++) { $cond = extract_text($parts[$i], "alt=\"", "\""); $temp = extract_text($parts[$i], "<span class=\"wob_t\" style=\"display:inline\">", "</span>"); $day = extract_text($parts[$i], "colspan=\"2\" style=\"vertical-align:top;text-align:center\">", "</td>"); if ($cond !== False) { $conds[] = strtolower($cond); } if ($temp !== False) { $temps[] = $temp; $tempsC[] = sprintf("%.0f", (substr($temp, 0, strlen($temp) - 2) - 32) * 5 / 9) . "°C"; } if ($day !== False) { $days[] = $day; } } $offset = 0; $wind_caption = ", wind " . $wind; if ($wind == "") { $offset = 1; $wind_caption = ""; } if (count($conds) != 5 or count($temps) != 10 - $offset or count($tempsC) != 10 - $offset or count($days) != 4) { return False; } if ($use_unit_pref == False) { $result = $location . " - currently " . $temps[0] . " / " . $tempsC[0] . ", " . $conds[0] . $wind_caption . ", humidity " . $humidity . " - "; } else { $result = $location . " - currently " . $temps[0] . ", " . $conds[0] . $wind_caption . ", humidity " . $humidity . " - "; } $fulldays = array("Sun." => "Sunday", "Mon." => "Monday", "Tue." => "Tuesday", "Wed." => "Wednesday", "Thu." => "Thursday", "Fri." => "Friday", "Sat." => "Saturday"); for ($i = 1; $i <= 4; $i++) { $day = $days[$i - 1]; $day = $fulldays[$day]; if ($use_unit_pref == False) { $result = $result . $day . " " . $conds[$i] . " (" . $temps[$i * 2 + 1 - $offset] . ":" . $temps[$i * 2 - $offset] . " / " . $tempsC[$i * 2 + 1 - $offset] . ":" . $tempsC[$i * 2 - $offset] . ")"; } else { $result = $result . $day . " " . $conds[$i] . " (" . $temps[$i * 2 + 1 - $offset] . ":" . $temps[$i * 2 - $offset] . ")"; } if ($i < 4) { $result = $result . ", "; } } $color = "10"; if (isset($prefs["color"]) == True) { $color = $prefs["color"]; } $result = chr(3) . $color . $result; if ($getdata != False) { $data = array(); $data["tempF"] = $temps[0]; $data["tempC"] = $tempsC[0]; $data["cond"] = $conds[0]; $data["wind"] = $wind_caption; $data["humidity"] = $humidity; $data["location"] = $location; return $data; } return $result; }