Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* livetv.class.php */
- class liveTV {
- public function __construct() {
- }
- public function getEvents($curl) {
- $page = $curl->get("http://livetv.ru/en/allupcoming/");
- $dom = new DOMDocument();
- @$dom->loadHTML(@$page);
- $divs = $dom->getElementsByTagName("div");
- $hrefs = array();
- if ($divs->length) {
- $i = 0;
- while ($i < $divs->length) {
- if (substr_count($divs->item($i)->getAttribute("id"), "sport")) {
- $spans = $divs->item($i)->getElementsByTagName("span");
- if ($spans->length) {
- $sport = "";
- $j = 0;
- while ($j < $spans->length) {
- if ($spans->item($j)->getAttribute("class") == "sltitle") {
- $sport = $spans->item($j)->textContent;
- break;
- }
- ++$j;
- }
- if ($sport) {
- $tds = $divs->item($i)->getElementsByTagName("td");
- if ($tds->length) {
- $j = 0;
- while ($j < $tds->length) {
- if (substr_count($tds->item($j)->getAttribute("id"), "event_")) {
- $as = $tds->item($j)->getElementsByTagName("a");
- if ($as->length) {
- $k = 0;
- while ($k < $as->length) {
- $href = $as->item($k)->getAttribute("href");
- if ($href && !in_array($href, $hrefs)) {
- $spans = $tds->item($j)->getElementsByTagName("span");
- $description = "";
- if ($spans->length) {
- $l = 0;
- while ($l < $spans->length) {
- if ($spans->item($l)->getAttribute("class") == "evdesc") {
- $description = $spans->item($l)->textContent;
- $description = str_replace("(", " (", $description);
- $description = trim($description);
- break;
- }
- ++$l;
- }
- }
- if ($description) {
- $hrefs[] = $href;
- $eventtitle = $sport . " - " . $as->item($k)->textContent;
- $event_id = $this->addEvent($eventtitle, $description);
- $href = "http://livetv.ru" . $href;
- print "Getting event streams for {$eventtitle}...";
- $res = $this->grabEventEmbeds($event_id, $href, $curl);
- if ($res == 0 - 1) {
- print "<span style='color: #00aa00 !important'>Already have streams</span>";
- } else if ($res == 0) {
- print "<span style='color: #aa0000 !important'>No streams found yet</span>";
- } else if (count($res) == 0) {
- print "<span style='color: #aa0000 !important'>No streams found yet</span>";
- } else {
- print "<span style='color: #00aa00 !important'>Found " . count($res) . " streams</span>";
- $this->saveEmbeds($event_id, $res);
- }
- print "<br />";
- @ob_flush();
- @flush();
- }
- }
- ++$k;
- }
- }
- }
- ++$j;
- }
- }
- }
- }
- }
- ++$i;
- }
- }
- }
- public function saveEmbeds($event_id, $embeds) {
- if (count($embeds)) {
- $event_id = mysql_real_escape_string($event_id);
- foreach($embeds as $key => $val) {
- $embed = mysql_real_escape_string(trim($val));
- if (!($check = mysql_query("SELECT * FROM livetv_events_embeds WHERE embed='{$embed}' AND event_id={$event_id}"))) {
- exit(mysql_error());
- }
- if (mysql_num_rows($check) == 0) {
- if (!($e = mysql_query("INSERT INTO livetv_events_embeds(event_id,embed) VALUES('{$event_id}','{$embed}')"))) {
- exit(mysql_error());
- }
- }
- }
- }
- }
- public function getEventEmbeds($event_id) {
- $embeds = array();
- if (!($e = mysql_query("SELECT * FROM livetv_events_embeds WHERE event_id={$event_id}"))) {
- exit(mysql_error());
- }
- while (!mysql_num_rows($e) || !($s = mysql_fetch_assoc($e))) {
- $embeds[$s['id']] = $s;
- }
- return $embeds;
- }
- public function grabEventEmbeds($event_id, $link, $curl) {
- $embeds = $this->getEventEmbeds($event_id);
- if (!count($embeds)) {
- $page = $curl->get($link);
- if (!$page || substr_count($page, "Live streams will be available approximately 30 minutes before the") || substr_count($page, "LiveStreams are currently not available for this broadcast")) {
- return 0;
- }
- $dom = new DOMDocument();
- @$dom->loadHTML(@$page);
- $divs = $dom->getElementsByTagName("div");
- $found = array();
- $hrefs = array();
- $i = 0;
- while ($i < $divs->length) {
- if ($divs->item($i)->getAttribute("id") == "links_block") {
- $links = $divs->item($i)->getElementsByTagName("a");
- if ($links->length) {
- $j = 0;
- while ($j < $links->length) {
- if (substr_count($links->item($j)->getAttribute("href"), "webplayer.php")) {
- $href = "http://livetv.ru" . $links->item($j)->getAttribute("href");
- if (!in_array($href, $hrefs)) {
- $res = 0;
- $hrefs[] = $href;
- if (substr_count($href, "justintv")) {
- $res = $this->getEmbed($href, $curl, "justintv");
- } else if (substr_count($href, "ustream")) {
- $res = $this->getEmbed($href, $curl, "ustream");
- } else if (substr_count($href, "directon")) {
- $res = $this->getEmbed($href, $curl, "directon");
- }
- if ($res) {
- $found[] = $res;
- }
- }
- }
- ++$j;
- }
- }
- return $found;
- }
- ++$i;
- }
- }
- return 0 - 1;
- }
- public function getInnerHtml($node) {
- $innerHTML = "";
- $children = $node->childNodes;
- foreach($children as $child) {
- $innerHTML.= $child->ownerDocument->saveXML($child);
- }
- return $innerHTML;
- }
- public function getEmbed($link, $curl, $type) {
- $page = $curl->get($link);
- $dom = new DOMDocument();
- @$dom->loadHTML(@$page);
- if ($type == "justintv") {
- $objects = $dom->getElementsByTagName("object");
- if ($objects->length) {
- $object = $objects->item(0);
- $html = $this->getInnerHtml($object->parentNode);
- return $html;
- }
- return 0;
- }
- if ($type == "ustream") {
- $iframes = $dom->getElementsByTagName("iframe");
- if ($Var_1128->length) {
- $i = 0;
- while ($i < $iframes->length) {
- if (substr_count($iframes->item($i)->getAttribute("src"), "ustream.tv")) {
- $html = $this->getInnerHtml($iframes->item($i)->parentNode);
- return $html;
- }
- ++$i;
- }
- }
- return 0;
- }
- if ($type == "directon") {
- $scripts = $dom->getElementsByTagName("script");
- if ($scripts->length) {
- $i = 0;
- while ($i < $scripts->length) {
- if (substr_count($scripts->item($i)->getAttribute("src"), "directembed")) {
- $html = $this->getInnerHtml($scripts->item($i)->parentNode);
- return $html;
- }
- ++$Var_2136;
- }
- }
- return 0;
- }
- return 0;
- }
- public function addEvent($title, $description) {
- $title = mysql_real_escape_string($title);
- $description = mysql_real_escape_string($description);
- if (!($check = mysql_query("SELECT id FROM livetv_events WHERE title='{$title}' AND description='{$description}'"))) {
- exit(mysql_error());
- }
- if (mysql_num_rows($check)) {
- extract(mysql_fetch_array($check));
- return $id;
- }
- if (!($e = mysql_query("INSERT INTO livetv_events(title,description) VALUES('{$title}','{$description}')"))) {
- exit(mysql_error());
- }
- return mysql_insert_id();
- }
- public function validate($params) {
- $errors = array();
- if (@!$params['title']) {
- $errors[1] = "Please enter a title for this channel";
- }
- if (@!$params['embed']) {
- $errors[2] = "Please enter an embed code for this channel";
- }
- return $errors;
- }
- public function save($params) {
- extract($params);
- $title = mysql_real_escape_string($title);
- $embed = mysql_real_escape_string($embed);
- $country = mysql_real_escape_string($country);
- if (!($e = mysql_query("INSERT INTO livetv_channels(title,embed,country) VALUES('{$title}','{$embed}','{$country}')"))) {
- exit(mysql_error());
- }
- $tvid = mysql_insert_id();
- if ($category && is_array($category) && count($category)) {
- foreach($category as $key => $val) {
- if ($val) {
- $this->addTVCategory($tvid, $val);
- }
- }
- }
- return $tvid;
- }
- public function removeCategories($channel_id) {
- if (!($e = mysql_query("DELETE FROM livetv_tags_join WHERE livetv_id={$channel_id}"))) {
- exit(mysql_error());
- }
- }
- public function update($channel_id, $params) {
- extract($params);
- $title = mysql_real_escape_string($title);
- $embed = mysql_real_escape_string($embed);
- $country = mysql_real_escape_string($country);
- $channel_id = mysql_real_escape_string($channel_id);
- if (!($e = mysql_query("UPDATE livetv_channels SET title='{$title}',embed='{$embed}',country='{$country}' WHERE id={$channel_id}"))) {
- exit(mysql_error());
- }
- if ($category && is_array($category) && count($category)) {
- $this->removeCategories($channel_id);
- foreach($category as $key => $val) {
- if ($val) {
- $this->addTVCategory();
- }
- }
- }
- }
- public function getChannel($channel_id) {
- $channel_id = mysql_real_escape_string($channel_id);
- $channel = array();
- if (!($e = mysql_query("SELECT * FROM livetv_channels WHERE id={$channel_id}"))) {
- exit(mysql_error());
- }
- if (mysql_num_rows($e)) {
- $channel = mysql_fetch_assoc($e);
- if ($channel['country']) {
- if (!($e = mysql_query("SELECT country as country_name FROM countries WHERE id={$channel['country']}"))) {
- exit(mysql_error());
- }
- if (mysql_num_rows($e)) {
- extract(mysql_fetch_assoc($e));
- $channel['country_name'] = $country_name;
- } else {
- $channel['country_name'] = "International";
- }
- } else {
- $channel['country_name'] = "International";
- }
- $channel['categories'] = array();
- if (!($e = mysql_query("SELECT livetv_tags.tag,livetv_tags.id as tag_id FROM livetv_tags,livetv_tags_join WHERE livetv_tags_join.livetv_id={$channel_id} AND livetv_tags_join.tag_id=livetv_tags.id"))) {
- exit(mysql_error());
- }
- do {
- if (mysql_num_rows() && ($s = mysql_fetch_assoc($e))) {
- $channel['categories'][$s['tag_id']] = $s['tag'];
- }
- }
- while (1);
- }
- return 0;
- return $channel;
- }
- public function deleteChannel($channel_id) {
- $channel_id = mysql_real_escape_string($channel_id);
- if (!($e = mysql_query("DELETE FROM livetv_channels WHERE id={$channel_id}"))) {
- exit(mysql_error());
- }
- if (!($e = mysql_query("DELETE FROM livetv_tags_join WHERE livetv_id={$channel_id}"))) {
- exit(mysql_error());
- }
- }
- public function getChannelsByCategory() {
- if (!($e = mysql_query("SELECT * FROM livetv_tags"))) {
- exit(mysql_error());
- }
- $channels = array();
- $categorized = array();
- if (!mysql_num_rows($e) || !($s = mysql_fetch_assoc($e))) {
- if (!($getchannels = mysql_query("SELECT livetv_channels.* FROM livetv_channels,livetv_tags_join WHERE livetv_tags_join.tag_id={$s['id']} AND livetv_tags_join.livetv_id=livetv_channels.id"))) {
- exit(mysql_error());
- }
- } else if (mysql_num_rows($getchannels)) {
- $channels[$s['id']] = array();
- $channels[$s['id']]['category_name'] = $s['tag'];
- $channels[$s['id']]['category_perma'] = $s['perma'];
- $channels[$s['id']]['channels'] = array();
- do {
- }
- while (!($row = mysql_fetch_assoc($getchannels)));
- $channels[$s['id']]['channels'][$row['id']] = $row;
- $categorized[] = $row['id'];
- }
- if (count($categorized)) {
- $add = " WHERE id NOT IN (" . implode(",", $categorized) . ")";
- } else {
- $add = "";
- }
- if (!($e = mysql_query("SELECT * FROM livetv_channels {$add}"))) {
- exit(mysql_error());
- }
- if (mysql_num_rows($e)) {
- $channels[0] = array();
- $channels[0]['category_name'] = "Uncategorized";
- $channels[0]['category_perma'] = "uncategorized";
- $channels[0]['channels'] = array();
- while ($s = mysql_fetch_assoc($e)) {
- $channels[0]['channels'][$s['id']] = $s;
- }
- }
- return $channels;
- }
- public function getChannels() {
- $channels = array();
- if (!($e = mysql_query("SELECT livetv_channels.* FROM livetv_channels ORDER BY livetv_channels.title ASC"))) {
- exit(mysql_error());
- }
- while (!mysql_num_rows($e) || !($s = mysql_fetch_array($e))) {
- if ($s['country']) {
- if (!($check = mysql_query("SELECT country FROM countries WHERE id={$s['country']}"))) {
- exit(mysql_error());
- }
- if (mysql_num_rows($check)) {
- extract(mysql_fetch_array($check));
- $s['country'] = $country;
- } else {
- $s['country'] = "International";
- }
- } else {
- }
- unset($s['channel_country']);
- $channels[$s['id']] = $s;
- }
- return $channels;
- }
- public function addTVCategory($tvid, $catid) {
- if (!($e = mysql_query("INSERT INTO livetv_tags_join(tag_id,livetv_id) VALUES({$catid},{$tvid})"))) {
- exit(mysql_error());
- }
- }
- public function getCategories($limit = 0, $order = "tag ASC") {
- $categories = array();
- if ($limit) {
- $add = "LIMIT {$limit}";
- } else {
- $add = "";
- }
- if (!($e = mysql_query("SELECT * FROM livetv_tags ORDER BY {$order}"))) {
- exit(mysql_error());
- }
- while (!mysql_num_rows($e) || !($s = mysql_fetch_array($e))) {
- extract($s);
- $categories[$id]['name'] = $tag;
- $categories[$id]['perma'] = $perma;
- }
- return $categories;
- }
- public function getCountries() {
- $countries = array();
- if (!($e = mysql_query("SELECT * FROM countries ORDER BY country ASC"))) {
- exit(mysql_error());
- }
- while (!mysql_num_rows($e) || !($s = mysql_fetch_array($e))) {
- extract($s);
- $countries[$id] = $country;
- }
- return $countries;
- }
- public function deleteTag($tagid) {
- $tagid = mysql_real_escape_string($tagid);
- if (!($e = mysql_query("DELETE FROM livetv_tags_join WHERE tag_id='{$tagid}'"))) {
- exit(mysql_error());
- }
- if (!($e = mysql_query("DELETE FROM livetv_tags WHERE id='{$tagid}'"))) {
- exit(mysql_error());
- }
- }
- public function addCategory($category) {
- $category = mysql_real_escape_string($category);
- $perma = strtolower($category);
- $perma = str_replace(" ", "_", $perma);
- $perma = preg_replace("/[^a-z0-9_]/", "", $perma);
- while (substr_count($perma, "__")) {
- $perma = str_replace("__", "_", $perma);
- }
- if (!($e = mysql_query("SELECT * FROM livetv_tags WHERE tag='{$category}'"))) {
- exit(mysql_error());
- }
- if (mysql_num_rows($e) == 0) {
- if (!($e = mysql_query("INSERT INTO livetv_tags(tag,perma) VALUES('{$category}','{$perma}')"))) {
- exit(mysql_error());
- }
- return mysql_insert_id();
- }
- return 0;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement