Debugmode
Debugmode

Reputation: 93

how to extract data from url like facebook in asp.net mvc3?

Is there any way to extract data from url like facebook does?

I refered many links but this is using php code. This- http://www.99points.info/2010/07/facebook-like-extracting-url-data-with-jquery-ajax-php/

http://www.9lessons.info/2010/06/facebook-like-extracting-url-data-with.html

www.webinfopedia.com/extract-meta-data-from-url-using-php.html

I want to implement like this. how can i implement this in my asp.net mvc3 application?

I just refered this -www.achari.in/facebook-like-url-data-extract-using-jquery in this i dont understand what they have done in php code, can anyone explain this code, what they have done in this?

In this, index.php file having

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>achari.in - Demo - Facebook Like URL data Extract Using jQuery PHP and Ajax</title>

<link rel="stylesheet" href="css/style.css" type="text/css">
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="js/jquery.livequery.js"></script>
<script type="text/javascript" src="js/jquery.watermarkinput.js"></script>
</head>
<script type="text/javascript">
    $(document).ready(function(){   
        // delete event
        $('#attach').livequery("click", function(){

            if(!isValidURL($('#url').val()))
            {
                alert('Please enter a valid url.');
                return false;
            }
            else
            {
                $('#load').show();
                $.post("includes/fetch.php?url="+$('#url').val(), {
                }, function(response){
                    $('#loader').html($(response).fadeIn('slow'));
                    $('.images img').hide();
                    $('#load').hide();
                    $('img#1').fadeIn();
                    $('#cur_image').val(1);
                });
            }
        }); 
        // next image
        $('#next').livequery("click", function(){

            var firstimage = $('#cur_image').val();
            $('#cur_image').val(1);
            $('img#'+firstimage).hide();
            if(firstimage <= $('#total_images').val())
            {
                firstimage = parseInt(firstimage)+parseInt(1);
                $('#cur_image').val(firstimage);
                $('img#'+firstimage).show();
            }
        }); 
        // prev image
        $('#prev').livequery("click", function(){

            var firstimage = $('#cur_image').val();

            $('img#'+firstimage).hide();
            if(firstimage>0)
            {
                firstimage = parseInt(firstimage)-parseInt(1);
                $('#cur_image').val(firstimage);
                $('img#'+firstimage).show();
            }

        }); 
        // watermark input fields
        jQuery(function($){

           $("#url").Watermark("http://");
        });
        jQuery(function($){

            $("#url").Watermark("watermark","#369");

        }); 
        function UseData(){
           $.Watermark.HideAll();
           $.Watermark.ShowAll();
        }

    }); 

    function isValidURL(url){
        var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

        if(RegExp.test(url)){
            return true;
        }else{
            return false;
        }
    }

    function confirmQuit()
    {
        var response = confirm('do you wish to close window !');        
        if(response === true)
          {
             return true;
          }
        else {
             return false;
        }
    }
</script>
<body>
<div align="center">
<br clear="all" /><br clear="all" />
<div style="font-size:30px;">Facebook Like URL data Extract Using jQuery PHP and Ajax</div>
<br clear="all" />
<a style="color:#000000; font-size:14px" href="#">Back To Tutorial</a>
<br clear="all" />

<input type="hidden" name="cur_image" id="cur_image" />
<div class="wrap" align="center">

    <div class="box" align="left">

        <div class="head">Link</div>
        <div class="close" align="right">
            <div class="closes" onclick="if(confirmQuit() == true) { window.close(); }"></div>
        </div>

        <br clear="all" /><br clear="all" />

        <input type="text" name="url" size="64" id="url" />
        <input type="button" name="attach" value="Attach" id="attach" />
        <br clear="all" />

        <div id="loader">

            <div align="center" id="load" style="display:none"><img src="images/load.gif" /></div>

        </div>
        <br clear="all" />
    </div>

</div>
</div>
  <br clear="all" />
</body>
</html>

above code is ui part and

    fetch.php
    <?php

   $url = $_REQUEST['url'];
$url = checkValues($url);

function checkValues($value)
{
    $value = trim($value);
    if (get_magic_quotes_gpc()) 
    {
        $value = stripslashes($value);
    }
    $value = strtr($value, array_flip(get_html_translation_table(HTML_ENTITIES)));
    $value = strip_tags($value);
    $value = htmlspecialchars($value);
    return $value;
}   

function fetch_record($path)
{
    $file = fopen($path, "r"); 
    if (!$file)
    {
        exit("Problem occured");
    } 
    $data = '';
    while (!feof($file))
    {
        $data .= fgets($file, 1024);
    }
    return $data;
}

$string = fetch_record($url);


/// fecth title
$title_regex = "/<title>(.+)<\/title>/i";
preg_match_all($title_regex, $string, $title, PREG_PATTERN_ORDER);
$url_title = $title[1];

/// fecth decription
$tags = get_meta_tags($url);

// fetch images
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $string, $img, PREG_PATTERN_ORDER);
$images_array = $img[1];

?>

    <div class="images">
    <?php
    $k=1;
    for ($i=0;$i<=sizeof($images_array);$i++)
    {
        if(@$images_array[$i])
        {
            if(@getimagesize(@$images_array[$i]))
            {
                list($width, $height, $type, $attr) = getimagesize(@$images_array[$i]);
                if($width >= 50 && $height >= 50 ){

                echo "<img src='".@$images_array[$i]."' width='100' id='".$k."' >";

                $k++;

                }
            }
        }
    }
    ?>
    <!--<img src="ajax.jpg"  alt="" />-->
    <input type="hidden" name="total_images" id="total_images" value="<?php echo --$k?>" />
    </div>
    <div class="info">

        <label class="title">
            <?php  echo @$url_title[0]; ?>
        </label>
        <br clear="all" />
        <label class="url">
            <?php  echo substr($url ,0,35); ?>
        </label>
        <br clear="all" /><br clear="all" />
        <label class="desc">
            <?php  echo @$tags['description']; ?>
        </label>
        <br clear="all" /><br clear="all" />

        <label style="float:left"><img src="images/prev.png" id="prev" alt="" /><img src="images/next.png" id="next" alt="" /></label>

        <label class="totalimg">
            Total <?php echo $k?> images
        </label>
        <br clear="all" />

    </div>

I dont know php so can anybody help me to find this solution?

In above code they use this function

   function fetch_record($path)
{
$file = fopen($path, "r"); 
if (!$file)
{
    exit("Problem occured");
} 
$data = '';
while (!feof($file))
{
    $data .= fgets($file, 1024);
}
return $data;
 }

can anybody tell me what exactly in this code what is use of file open in this, is this for reading url index file??

Upvotes: 0

Views: 2662

Answers (3)

Yuksel Daskin
Yuksel Daskin

Reputation: 11

The link on the page I posted before was unfortunately dead but it is fixed now.

http://nalgorithm.wordpress.com/2011/12/05/embedded-url-representation/

As I stated there, I am working on the service version of this tool so that other webapps can make use of it just by (jquery) and a service call.

Upvotes: 1

Yuksel Daskin
Yuksel Daskin

Reputation: 11

Take a look at this open-source project:

http://nalgorithm.wordpress.com/2011/12/05/embedded-url-representation/

It is actually a class library, which is called by an MVC application client by using JQuery and its AJAX features.

It uses AgilityPack beneath but there are so many other issues you have to handle, like encoding.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

For parsing HTML I would recommend you taking a look at HTML Agility Pack.

Upvotes: 0

Related Questions