****************************************************************************************** facebot.py: The Python script that reads from the database and checks for new friends and adds them to the user database and relationships. ****************************************************************************************** print """|------------------------------------------| by: Daniel Folkes email: danfolkes@gmail.com Starting... """ import urllib2, time, MySQLdb from urllib2 import Request, urlopen, URLError while 1: db = MySQLdb.connect(host="localhost", user="USERNAME", passwd="PASSWORD",db="facebot") cur = db.cursor() numres = cur.execute("SELECT * FROM feeds ORDER BY ts ASC LIMIT 1") res = cur.fetchone() id = res[0] url = res[1] print "--------------------------------------------------" print url, "Long Wait....." print "--------------------------------------------------" req = Request(url) time.sleep(10) try: response = urlopen(req) except URLError, e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server couldn\'t fulfill the request.' print 'Error code: ', e.code else: # everything is fine line = response.readline() pid = 0 parent = "" while line: uname = "" if line.find("author") != -1: uname = line[line.find("")+8:line.find("")] elif line.find("")!=-1: line = response.readline() if line.find("")!=-1 and line.find("")!=-1: if line.find("Friends' Facebook") != -1: parent = line[line.find("")+7:line.find("Friends' Facebook")-8] curInsPar = db.cursor() sqll = "INSERT INTO u (name) VALUES ('"+parent+"')" pid = 0 try: curInsPar.execute(sqll) pid = db.insert_id() except: pid = 0 curInsPar2 = db.cursor() sqll = "SELECT `uid` FROM `u` WHERE name='"+parent+"'" try: curInsPar2.execute(sqll) or die("nope.") pid = curInsPar2.fetchone()[0] except: pid = 0 time.sleep(2) if uname: cur2 = db.cursor() sql = "INSERT INTO u (name) VALUES ('"+uname+"')" uid = 0 rid = 0 try: cur2.execute(sql) or die("nope.") uid = db.insert_id() except: uid = 0 cur2= db.cursor() sql = "SELECT `uid` FROM `u` WHERE name='"+uname+"'" try: cur2.execute(sql) or die("nope.") uid = cur2.fetchone()[0] except: uid = 0 if uid and pid: sql2 = "INSERT INTO rel (u1, u2) VALUES ("+str(pid)+","+str(uid)+")" sqlCheck = "SELECT rid FROM rel WHERE u1="+str(pid)+" and u2="+str(uid) if uid < pid: sql2 = "INSERT INTO rel (u1, u2) VALUES ("+str(uid)+","+str(pid)+")" sqlCheck = "SELECT rid FROM rel WHERE u1="+str(uid)+" and u2="+str(pid) try: cur2= db.cursor() cur2.execute(sqlCheck) t = cur2.fetchone() except: print "Error: rel failed" if not t: curInsRel= db.cursor() curInsRel.execute(sql2) time.sleep(2) printstrg = "uname: " + str(uname) print "uname : ", uname print "uid : ",uid print "parent : ", parent print "pid : ",pid print "----------" time.sleep(2) line = response.readline() #add date to url try: cur3 = db.cursor() sql = "UPDATE feeds SET type = 1 WHERE fid="+str(id) cur3.execute(sql) cur3 = db.cursor() sql = "UPDATE feeds SET type = 0 WHERE fid="+str(id) cur3.execute(sql) cur3.execute(sql) or die("nope.") except: print "Could not update where fid = "+ str(id) ****************************************************************************************** index.php: The PHP form will add the URL submitted to the database. ****************************************************************************************** <?php if(isset($_POST['url'])) { $url = $_POST['url']; $length = strlen($url); $start10 = substr($url,0,10); $end10 = substr($url,-10); $cfacebook = substr_count(strtolower($url), 'facebook'); $cfeeds = substr_count(strtolower($url), 'feeds'); $cfriendstatus = substr_count(strtolower($url), 'friends_status'); $output = "ww"; $add = true; if ( ($length>150) || ($length < 10)) { $add=false; $output .= " lendgth."; } elseif ($cfacebook != 1) { $add=false; $output .= " cfacedbook ."; } elseif ($cfeeds != 1) { $add=false; $output .= " cfededs ."; } elseif ($cfriendstatus != 1) { $add=false; $output .= " cfrienddstatus ."; } if ($add === true) { include '/var/g2data/configface.phps'; include '/var/g2data/opendb.phps'; mysql_select_db($mysql); $query = "INSERT INTO feeds (furl, type) VALUES ('$url', 0)"; mysql_query($query) or die('URL not inserted. This might be because the URL you added is already in the database, or there is just something wrong with our server. :( Please try again.'); include '/var/g2data/closedb.phps'; } else { echo "<span class='error'>You must enter a valid feed URL.</span>"; } } ?> <html> <head> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1>Facebook Robot</h1> </div> <div id="header"> <a href="index.php">home</a> | <a href="view.php">view</a> </div> <div id="content"> <div id="step1"> <span class="big">1</span> <span class="subtitle"> Go to the Status Page</span> <div class="innerstep">Located <a href="http://www.facebook.com/friends/?status&ref=hp">here</a>. <br /><span class="smalltxt">or http://www.facebook.com/friends/?status&ref=hp</span> <br /> <br /><br /></div> </div> <div id="step2"> <span class="big">2</span><span class="subtitle">Copy the RSS Link</span> <div class="innerstep">Right-click on the [Friends' Status Feed] Button and select "Copy Link Location" <br /> <br /><img src="./images/rightclickStatus.jpg" /> <br /><br /></div> </div> <div id="step3"> <span class="big">3</span><span class="subtitle">Submit your RSS Feed</span> <div class="innerstep"><form method="POST" action=<?php echo $PHP_SELF;?>> Feed URL:<input type="text" name="url" size="50"/> <span style="display:none;"> <input type="text" name="comment" size="5"/> </span> <input type="submit" value="Update Facebot" /> </form> <br /><span class="alert">This URL does release any of your personal information. It will only remember that your you are friends with people. If you would like to look at the information that it takes, go to the View Page.</span> <br /><br /></div> </div> <div id="step4"> <span class="big">4</span> <span class="subtitle"><a href="view.php">View Results</a></span> <div class="innerstep">Your feed will show up in 1-2000 seconds. :)~~<br /><br /><br /><br /><br /></div> </div> </div> </body> </html> <?php /*include '/var/g2data/configface.phps'; include '/var/g2data/opendb.phps'; $query = "SELECT * FROM feeds ORDER BY fid DESC"; $result = mysql_query($query); echo "<ul>"; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $i++; echo "{$row['fid']} | {$row['furl']} | {$row['type']} | {$row['ts']}<br/>"; } include '/var/g2data/closedb.phps';*/ ?> ****************************************************************************************** view.php: The PHP page shows the relationships between all the users submitted using. the system. ****************************************************************************************** <?php include '/var/g2data/configface.phps'; include '/var/g2data/opendb.phps'; ?> <html> <head> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1>Facebook Robot</h1> </div> <div id="header"> <a href="index.php">home</a> | <a href="view.php">view</a> <?php $query = "SELECT COUNT(fid) as cfid FROM feeds"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC); echo "<br/>Checking {$row['cfid']} Feeds. "; $query = "SELECT COUNT(uid) as cuid FROM u"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC); echo "Found {$row['cuid']} People."; ?> </div> <div id="header"> <h1>::RELATIONSHIPS(thus far)::</h1> </div> <div id="content"> </div> <div id="step2" style="display:none;"> <span class="big">2</span><span class="subtitle">Users Added:</span> <div class="innerstep"><br/><?php $query = "SELECT COUNT(uid) as c FROM u"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "Count: {$row['c']}<br/><br/>"; } ?></div> </div> <div id="step3" style="display:none;"> <span class="big">3</span><span class="subtitle">Relationships Added:</span> <div class="innerstep"><br/><br/><br/><br/><br/> </div> <h1>Relationships</h1> </div></div><?php $sqlName = "Garber"; $q1 = "SELECT distinct u1 FROM rel ORDER BY u1 ASC"; $q2 = "SELECT distinct u2 FROM rel ORDER BY u1 DESC"; //$query = "SELECT u1,u2,name FROM rel,u WHERE u1=(select uid from u where name like '%" + $sqlName + "%') or u2=(select uid from u where name like '%" + $sqlName + "%') ORDER BY u1 ASC"; $ar = array(); $r1 = mysql_query($q1); $r2 = mysql_query($q2); while($row = mysql_fetch_array($r1, MYSQL_ASSOC)) { $ar += array($row['u1']=>0); } while($row = mysql_fetch_array($r2, MYSQL_ASSOC)) { $ar += array($row['u2']=>0); } foreach ($ar as $key => $value) { #add values $q3 = "SELECT COUNT(u1) as c FROM rel WHERE u1 = {$key} or u2 = {$key}"; $r3 = mysql_query($q3); $row3 = mysql_fetch_array($r3, MYSQL_ASSOC); $ar[$key] = $row3['c']; } arsort($ar,SORT_NUMERIC); foreach ($ar as $key => $value) { if (($key != null)&&($value>=2)){ echo "<div style='font-size:12px;background-color:black;float:left;margin:1px;'>"; $q3 = "SELECT distinct name FROM u WHERE uid = {$key}"; $r3 = mysql_query($q3); $row = mysql_fetch_array($r3, MYSQL_ASSOC); echo "<div>{$row['name']}<br/># of Friends:{$value}</div>"; echo "<div style='text-align:left;font-size:9px;background-color:#333333;padding-left:15px;width:150px;'>"; $q3 = "SELECT distinct u2 as u22 FROM rel WHERE u1 = {$key}"; $r3 = mysql_query($q3); while($row = mysql_fetch_array($r3, MYSQL_ASSOC)) { $q4 = "SELECT distinct name as name2 FROM u WHERE uid = {$row['u22']}"; $r4 = mysql_query($q4); while($row = mysql_fetch_array($r4, MYSQL_ASSOC)) { echo "{$row['name2']}<br/>"; } } $q3 = "SELECT distinct u1 as u12 FROM rel WHERE u2 = {$key}"; $r3 = mysql_query($q3); while($row = mysql_fetch_array($r3, MYSQL_ASSOC)) { $q4 = "SELECT distinct name as name2 FROM u WHERE uid = {$row['u12']}"; $r4 = mysql_query($q4); while($row = mysql_fetch_array($r4, MYSQL_ASSOC)) { echo "{$row['name2']}<br/>"; } } echo "</div>"; echo "</div>";} } unset($value); ?><br/><br/><br/><br/><br/> </body> </html> <?php include '/var/g2data/closedb.phps'; ?>