Visa ett inlägg
Gammal 2012-02-27, 17:55   #16
KP
Sharpest
 
KPs avatar
 
Reg.datum: dec 2009
Ort: Kebnekaise
Inlägg: 5 830
Sharp$: 45582
Standard

Citat:
Ursprungligen postat av Persa Visa inlägg
Har fixat ett enkelt php-skript som hämtar ut datan från sharps sökresultat och skapar ett rss-flöde. Problemet är som jag skrev tidigare att man måste vara inloggad för att få det att fungera. Detta betyder att det i dagsläget inte fungerar, och att jag har kopierat innehållet från sökresultatet och sparat allt i en statiskt fil. Det är alltså bara "Proof of concept" men koden ska fungera om sökningen öppnas upp för icke-medlemmar.

Här finns testsidan:
http://utilities.persa.se/soulman/

Såhär fungerar skriptet:
Spoiler:

Skriptet hämtar sidan cached.htm (detta skall bytas ut mot https://www.sharps.se/forums/search.p...induser&u=5236) om sökningen öppnas upp. Jag är fullt medveten om att koden kan skrivas bättre men i dagsläget finns inga planer på att förfina den.
Kod:
<?php

include("simple_html_dom.php");

// Get data
$items = extractData("cached.htm");

// Write data
echo writeDocument($items);

//----------------------------------------------------------
// Function - write xml body
//----------------------------------------------------------
function writeDocument($items)
{
	$output =
"<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
    <channel>
        <title>Sharps.se - Soulman posts</title>
        <link>https://www.sharps.se</link>
        <description>Sharps.se RSS feed</description>
        <language>en-us</language>
        <copyright>Copyright (C) 2012 persa</copyright>
$items</channel>
<rss>";
	return $output;
}

//----------------------------------------------------------
// Function - extract data from url
//----------------------------------------------------------
function extractData($url)
{
	// Get current timestamp
	$time_unix = time();
	$day = 86400;

	// Set the output string
	$items = "";

	// Create DOM from URL or file
	$html = file_get_html($url);

	// Parse the file and find all <table ... id="post???">
	foreach($html->find("table[id^=post]") as $element)
	{
		// Get <td>-element
		$td = $element->find('td', 0);

		// Filter out garbage
		$td->find('span', 0)->outertext = "";
		$td->find('img', 0)->outertext = "";

		// Get date and time
		@list($date, $time) = explode(",", $td->innertext);
		$date = trim($date);
		$time = trim($time);

		// Set correct date for today and yesterday
		if ($date == "idag")		$date = date("Y-m-d", $time_unix);
		if ($date == "ig&aring;r")	$date = date("Y-m-d", $time_unix - $day);
		$date = date("D, d M Y H:i:s O", strtotime("$date $time"));

		// Get the correct div with the containing description
		$div = $element->find('div[class=alt2]', 0)->find('em', 0);

		// Remove images
		$div->find('img', 0)->outertext = "";

		// Get the link and title
		$link = "https://www.sharps.se/forums/" . $div->find('a', 0)->href;
		$title = str_replace("\n", " ", $div->find('a', 0)->innertext);

		// Filter out garbage
		$div->find('a', 0)->outertext = "";
		$div->find('br', 0)->outertext = "";

		// Get the description
		$description = trim(str_replace("<br />", " ", $div->innertext));
		$description = str_replace("\n", " ", $description);

		// Write RSS item
		$items .= 
"        <item>
            <title>$title</title>
            <link>$link</link>
            <description>$description</description>
            <pubDate>$date</pubDate>
        </item>\n";
	}

	return $items;
}
?>
Om du söker efter "vbulletin login script" så hittar du ett och annat att använda med
KP är inte uppkopplad   Ge poäng Svara med citat