<commit_before>package com.chrisdmilner.webapp;

import facebook4j.*;
import facebook4j.conf.ConfigurationBuilder;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

// Contains functions related to Facebook data mining.
public class FacebookMiner {

	// FOR TEST USE
	public static void main(String[] args) {
		if (args.length != 2) {
			System.err.println("ERROR incorrect number of arguments. You must give two arguments: a Facebook id and an access token or ''.");
			System.exit(1);
		}

		FactBook fb = mine(args[0], args[1]);
		System.out.println(fb.toString());
	}

	// Extracts the available Facebook data for a given user ID.
	public static FactBook mine(String id, String at) {

        System.out.println("\n - STARTING FACEBOOK MINER - \n");

	    FactBook fs = new FactBook();

        System.out.println("   Connecting to API and retrieving user data");

		FacebookFactory ff = new FacebookFactory(configureF4J(at).build());

		// Get the raw Facebook API user data.
		Facebook fb = ff.getInstance();

		User u = null;
		try {
            fb.extendTokenExpiration();

            Reading reading = new Reading().fields( "about",        "address",          "age_range",
                                                    "birthday",     "cover",            "education",
                                                    "email",        "first_name",       "gender",
                                                    "hometown",     "interested_in",    "last_name",
                                                    "locale",       "location",         "middle_name",
                                                    "name",         "political",        "relationship_status",
                                                    "religion",     "significant_other","sports",
                                                    "website",      "work");

            if (at.equals("")) {
                u = fb.getUser(id, reading);

            } else {
                u = fb.getMe(reading);

                Reading r = new Reading().fields("images");
                ResponseList<Photo> photos = fb.getPhotos(r);
                List<Image> images;
                for (Photo photo : photos) {
                     images = photo.getImages();
                    fs.addFact(new Fact<>("Image URL", images.get(0).getSource().toString(), "Facebook", "Photos"));
                }

                // TODO fb.getVideos (logged in or not)

                // TODO fb.getInterests etc.
                // TODO fb.getFamily ??

            }

		} catch (FacebookException e) {
			System.err.println("   ERROR getting facebook profile");
			e.printStackTrace();
		}

		System.out.println("   Processing the user's profile data");

		if (u.getLanguages() != null) {
			for (int i = 0; i < u.getLanguages().size(); i++) {
				fs.addFact(new Fact<>("Language", u.getLanguages().get(i).getName(), "Facebook", "UserProfile"));
			}
		}

		// Add the available data to the factbook.
		if (u.getName() != null) 		        fs.addFact(new Fact<>("Name", u.getName(), "Facebook", "UserProfile"));
		if (u.getFirstName() != null) 		    fs.addFact(new Fact<>("First Name", u.getFirstName(), "Facebook", "UserProfile"));
		if (u.getMiddleName() != null) 			fs.addFact(new Fact<>("Middle Name", u.getMiddleName(), "Facebook", "UserProfile"));
		if (u.getLastName() != null) 			fs.addFact(new Fact<>("Last Name", u.getLastName(), "Facebook", "UserProfile"));
		if (u.getUsername() != null) 			fs.addFact(new Fact<>("Username", u.getUsername(), "Facebook", "UserProfile"));
		if (u.getPicture() != null && !u.getPicture().isSilhouette())
		                                        fs.addFact(new Fact<>("Image URL", u.getPicture().getURL().toString(), "Facebook", "UserProfile"));
		if (u.getAgeRange() != null) { 			fs.addFact(new Fact<>("Minimum Age", u.getAgeRange().getMin(), "Facebook", "UserProfile"));
												fs.addFact(new Fact<>("Maximum Age", u.getAgeRange().getMax(), "Facebook", "UserProfile")); }
		if (u.getBio() != null) 				fs.addFact(new Fact<>("Description", u.getBio(), "Facebook", "UserProfile"));
		if (u.getBirthday() != null)			fs.addFact(new Fact<>("Birthday", u.getBirthday(), "Facebook", "UserProfile"));
		if (u.getEmail() != null) 				fs.addFact(new Fact<>("Email", u.getEmail(), "Facebook", "UserProfile"));
		if (u.getGender() != null) 				fs.addFact(new Fact<>("Gender", u.getGender(), "Facebook", "UserProfile"));
		if (u.getTimezone() != null) 			fs.addFact(new Fact<>("Time Zone", u.getTimezone(), "Facebook", "UserProfile"));
		if (u.getHometown() != null && u.getHometown().getName()!=null)
		                                        fs.addFact(new Fact<>("Home Town", u.getHometown().getName(), "Facebook", "UserProfile"));
		if (u.getLocation() != null && u.getLocation().getName()!=null)
		                                        fs.addFact(new Fact<>("Location", u.getLocation().getName(), "Facebook", "UserProfile"));
		if (u.getLocale() != null)				fs.addFact(new Fact<>("Locale", u.getLocale(), "Facebook", "UserProfile"));
		if (u.getLink() != null)				fs.addFact(new Fact<>("Linked URL", u.getLink().toString(), "Facebook", "UserProfile"));
		if (u.getRelationshipStatus() != null)	fs.addFact(new Fact<>("Relationship Status", u.getRelationshipStatus(), "Facebook", "UserProfile"));
		if (u.getPolitical() != null)			fs.addFact(new Fact<>("Politics", u.getPolitical(), "Facebook", "UserProfile"));
		if (u.getReligion() != null)			fs.addFact(new Fact<>("Religion", u.getReligion(), "Facebook", "UserProfile"));
		if (u.getWebsite() != null)				fs.addFact(new Fact<>("Linked URL", u.getWebsite().toString(), "Facebook", "UserProfile"));<commit_msg>Improved Facebook Miner Birth Date<commit_after>package com.chrisdmilner.webapp;

import facebook4j.*;
import facebook4j.conf.ConfigurationBuilder;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

// Contains functions related to Facebook data mining.
public class FacebookMiner {

	// FOR TEST USE
	public static void main(String[] args) {
		if (args.length != 2) {
			System.err.println("ERROR incorrect number of arguments. You must give two arguments: a Facebook id and an access token or ''.");
			System.exit(1);
		}

		FactBook fb = mine(args[0], args[1]);
		System.out.println(fb.toString());
	}

	// Extracts the available Facebook data for a given user ID.
	public static FactBook mine(String id, String at) {

        System.out.println("\n - STARTING FACEBOOK MINER - \n");

	    FactBook fs = new FactBook();

        System.out.println("   Connecting to API and retrieving user data");

		FacebookFactory ff = new FacebookFactory(configureF4J(at).build());

		// Get the raw Facebook API user data.
		Facebook fb = ff.getInstance();

		User u = null;
		try {
            fb.extendTokenExpiration();

            Reading reading = new Reading().fields( "about",        "address",          "age_range",
                                                    "birthday",     "cover",            "education",
                                                    "email",        "first_name",       "gender",
                                                    "hometown",     "interested_in",    "last_name",
                                                    "locale",       "location",         "middle_name",
                                                    "name",         "political",        "relationship_status",
                                                    "religion",     "significant_other","sports",
                                                    "website",      "work");

            if (at.equals("")) {
                u = fb.getUser(id, reading);

            } else {
                u = fb.getMe(reading);

                Reading r = new Reading().fields("images");
                ResponseList<Photo> photos = fb.getPhotos(r);
                List<Image> images;
                for (Photo photo : photos) {
                     images = photo.getImages();
                    fs.addFact(new Fact<>("Image URL", images.get(0).getSource().toString(), "Facebook", "Photos"));
                }

                // TODO fb.getVideos (logged in or not)

                // TODO fb.getInterests etc.
                // TODO fb.getFamily ??

            }

		} catch (FacebookException e) {
			System.err.println("   ERROR getting facebook profile");
			e.printStackTrace();
		}

		System.out.println("   Processing the user's profile data");

		if (u.getLanguages() != null) {
			for (int i = 0; i < u.getLanguages().size(); i++) {
				fs.addFact(new Fact<>("Language", u.getLanguages().get(i).getName(), "Facebook", "UserProfile"));
			}
		}

		// Add the available data to the factbook.
		if (u.getName() != null) 		        fs.addFact(new Fact<>("Name", u.getName(), "Facebook", "UserProfile"));
		if (u.getFirstName() != null) 		    fs.addFact(new Fact<>("First Name", u.getFirstName(), "Facebook", "UserProfile"));
		if (u.getMiddleName() != null) 			fs.addFact(new Fact<>("Middle Name", u.getMiddleName(), "Facebook", "UserProfile"));
		if (u.getLastName() != null) 			fs.addFact(new Fact<>("Last Name", u.getLastName(), "Facebook", "UserProfile"));
		if (u.getUsername() != null) 			fs.addFact(new Fact<>("Username", u.getUsername(), "Facebook", "UserProfile"));
		if (u.getPicture() != null && !u.getPicture().isSilhouette())
		                                        fs.addFact(new Fact<>("Image URL", u.getPicture().getURL().toString(), "Facebook", "UserProfile"));
		if (u.getAgeRange() != null) { 			fs.addFact(new Fact<>("Minimum Age", u.getAgeRange().getMin(), "Facebook", "UserProfile"));
												fs.addFact(new Fact<>("Maximum Age", u.getAgeRange().getMax(), "Facebook", "UserProfile")); }
		if (u.getBio() != null) 				fs.addFact(new Fact<>("Description", u.getBio(), "Facebook", "UserProfile"));

		if (u.getBirthday() != null) {
            //fs.addFact(new Fact<>("Birthday", u.getBirthday(), "Facebook", "UserProfile"));
            String bday = u.getBirthday();
            if (bday.length() == 4) // YYYY
                fs.addFact(new Fact<>("Birth Year", bday, "Facebook", "UserProfile"));
            else { // MM/DD
                String[] parts = bday.split("/");
                fs.addFact(new Fact<>("Birth Month", parts[0], "Facebook", "UserProfile"));
                fs.addFact(new Fact<>("Birth Day", parts[1], "Facebook", "UserProfile"));
                if (parts.length > 2)
                    fs.addFact(new Fact<>("Birth Year", parts[2], "Facebook", "UserProfile"));
            }
		}

        if (u.getEmail() != null) 				fs.addFact(new Fact<>("Email", u.getEmail(), "Facebook", "UserProfile"));
		if (u.getGender() != null) 				fs.addFact(new Fact<>("Gender", u.getGender(), "Facebook", "UserProfile"));
		if (u.getTimezone() != null) 			fs.addFact(new Fact<>("Time Zone", u.getTimezone(), "Facebook", "UserProfile"));
		if (u.getHometown() != null && u.getHometown().getName()!=null)
		                                        fs.addFact(new Fact<>("Home Town", u.getHometown().getName(), "Facebook", "UserProfile"));
		if (u.getLocation() != null && u.getLocation().getName()!=null)
		                                        fs.addFact(new Fact<>("Location", u.getLocation().getName(), "Facebook", "UserProfile"));
		if (u.getLocale() != null)				fs.addFact(new Fact<>("Locale", u.getLocale(), "Facebook", "UserProfile"));
		if (u.getLink() != null)				fs.addFact(new Fact<>("Linked URL", u.getLink().toString(), "Facebook", "UserProfile"));
		if (u.getRelationshipStatus() != null)	fs.addFact(new Fact<>("Relationship Status", u.getRelationshipStatus(), "Facebook", "UserProfile"));
		if (u.getPolitical() != null)			fs.addFact(new Fact<>("Politics", u.getPolitical(), "Facebook", "UserProfile"));
		if (u.getReligion() != null)			fs.addFact(new Fact<>("Religion", u.getReligion(), "Facebook", "UserProfile"));
		if (u.getWebsite() != null)				fs.addFact(new Fact<>("Linked URL", u.getWebsite().toString(), "Facebook", "UserProfile"));
{'fraction_non_alphanumeric': 0.17204034391534392, 'fraction_numerical': 0.0019014550264550263, 'mean_word_length': 2.371516164994426, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 103, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2492891', 'n_tokens_mistral': 3880, 'n_tokens_neox': 3386, 'n_words': 826}