Explore Twitter’s Social Network

Twitter’s social network is a directed graph structure, where each user follows some other users. Here we consider two tasks:

  • Random walk: Starting from one user, find a random friend, and continue …
  • Common friends: Find common friends of two users

First we design two basic classes:

  • TwitterUser: A data-type class representing the information of a Twitter user (id, name, friends)
  • TwitterTool: A code-library class with methods on retrieving information from Twitter’s service
public class TwitterUser
{
	long	id;
	String	screen_name;
	String	name;
	long	friends_count;
	long[]	friends_ids;
	public TwitterUser(long _id, String _screen_name, String _name, long _friends_count)
	{
		id = _id; screen_name = _screen_name; name = _name;
		friends_count = _friends_count;
	}
	public long getId()
	{
		return id;
	}
	public String getName()
	{
		return name;
	}
	public void setFriendsIds(long[] fids)
	{
		friends_ids = fids;
	}
	public long[] getFriendsIds()
	{
		return friends_ids;
	}
	public String toString()
	{
		return id + ", " + name + ", " + screen_name;
	}
}
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

public class TwitterTool
{
	// Read from URL and return the content in a String
	public static String readURLContent(String urlString) throws IOException
	{
		URL url = new URL(urlString);
		Scanner scan = new Scanner(url.openStream());
		String content = new String();
		while (scan.hasNext())
			content += scan.nextLine();
		scan.close();
		return content;
	}

	// Extract the middle part of a string from "open" to "close"
	public static String extractMiddle(String str, String open, String close)
	{
		int	begin = str.indexOf(open) + open.length();
		int end = str.indexOf(close, begin);
		return str.substring(begin, end);
	}

	public static TwitterUser constructTwitterUserByName(String screen_name) throws IOException
	{
		String para = "screen_name=" + screen_name;
		return constructTwitterUser(para);
	}
	public static TwitterUser constructTwitterUserById(long id) throws IOException
	{
		String para = "user_id=" + id;
		return constructTwitterUser(para);
	}
	public static TwitterUser constructTwitterUser(String para) throws IOException
	{
		String url = "http://api.twitter.com/1/users/show.json?" + para;
		String content = readURLContent(url);
		if (content == null)
			return null;

		long id = Integer.parseInt(extractMiddle(content, "\"id\":", ","));
		String screen_name = extractMiddle(content, "\"screen_name\":", ",");
		String name = extractMiddle(content, "\"screen_name\":", ",");
		long friends_count = Integer.parseInt(extractMiddle(content, "\"friends_count\":", ","));
		// Construct a TwitterUser object
		TwitterUser u =  new TwitterUser(id, screen_name, name, friends_count);
		return u;
	}

	public static long[] retrieveFriendsIds(long id) throws IOException
	{
		// access Twitter to get friends
		String url = "http://api.twitter.com/1/friends/ids.json?user_id=" + id;
		String content = readURLContent(url);
		if (content == null)
			return null;

		// extract ids
		String str_ids = extractMiddle(content, "\"ids\":[", "]");
		String[] str_ids_arr = str_ids.split(",");
		long[] ids = new long[str_ids_arr.length];
		for (int i = 0; i < ids.length; i++)
			ids[i] = Long.parseLong(str_ids_arr[i]);
		return ids;
	}

}

Random Walk

We next define TwitterRandomWalk class. The input is a twitter user name.

  • Find the user information
  • Find friends of this user (a list of ids)
  • Pick up a random friend id
  • Find the user information, and friends
  • ……
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class TwitterRandomWalk
{
	public static void main(String[] args) throws IOException
	{
		Scanner scan = new Scanner(System.in);
		System.out.println("Please type in a twitter name:");
		String screen_name = scan.nextLine();
		TwitterUser u = TwitterTool.constructTwitterUserByName(screen_name);
		System.out.println(u);

		System.out.println("---------------------------------");
		for (int i = 0; i < 20; i++)
		{
			// Get friends list
			long[] friends = TwitterTool.retrieveFriendsIds(u.getId());
			int r = (int) (friends.length * Math.random());
			TwitterUser f = TwitterTool.constructTwitterUserById(friends[r]);
			System.out.println("-->");
			System.out.println(f);
		}
	}
}

Here we define the TwitterCommonFriend class. The inputs are two twitter usernames.

  • Find friends of both users
  • Check if one is a friend of the other
  • Find common friends of the two users (the intersection of two sets of user-ids)
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class TwitterCommonFriend
{
	// find the intersection of two sorted arrays
	public static long[] intersect(long[] a, long[] b)
	{
		long[] res = new long[a.length];
		int i = 0, j = 0, k = 0;
		while (i < a.length && j < b.length)
		{
			if(a[i] < b[j])
				i++;
			else if (a[i] > b[j])
				j++;
			else
			{
				res[k++] = a[i];
				i++; j++;
			}   
		}
		return Arrays.copyOf(res, k);
	}

	public static void main(String[] args) throws IOException
	{
		Scanner scan = new Scanner(System.in);		
		System.out.println("Please type in two twitter names:");
		String name1 = scan.nextLine();
		String name2 = scan.nextLine();
		TwitterUser user1 = TwitterTool.constructTwitterUserByName(name1);
		TwitterUser user2 = TwitterTool.constructTwitterUserByName(name2);
		
		System.out.println("---------------------------------");
		System.out.println("The two users are:");
		System.out.println(user1);	
		System.out.println(user2);	
		System.out.println("---------------------------------");
		
		// Get friends list
		long[] friends1 = TwitterTool.retrieveFriendsIds(user1.getId());
		long[] friends2 = TwitterTool.retrieveFriendsIds(user2.getId());
		Arrays.sort(friends1);
		Arrays.sort(friends2);
		
		if (Arrays.binarySearch(friends1, user2.getId()) >= 0)
			System.out.println(user2.getName() + " is a friend of " + user1.getName());		
		if (Arrays.binarySearch(friends2, user1.getId()) >= 0)
			System.out.println(user1.getName() + " is a friend of " + user2.getName());
		
		System.out.println("---------------------------------");
		long common_fids[] = intersect(friends1, friends2);
		for (int i = 0; i < common_fids.length; i++)
		{
			TwitterUser u = TwitterTool.constructTwitterUserById(common_fids[i]);
			System.out.println(u);	
		}				
	}
}

Comments

comments

Tagged with: , , , ,
Posted in Java