<commit_before>package eu.socialsensor.framework.abstractions.socialmedia.facebook;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.restfb.types.Comment;
import com.restfb.types.NamedFacebookType;
import com.restfb.types.Place;
import com.restfb.types.Post;
import com.restfb.types.Post.Likes;
import com.restfb.types.User;

import eu.socialsensor.framework.common.domain.Item;
import eu.socialsensor.framework.common.domain.Location;
import eu.socialsensor.framework.common.domain.MediaItem;
import eu.socialsensor.framework.common.domain.SocialNetworkSource;
import eu.socialsensor.framework.common.domain.WebPage;

/**
 * Class that holds the information regarding the facebook post
 * @author ailiakop
 * @author <EMAIL>
 *
 */
public class FacebookItem extends Item {
	
	public FacebookItem(String id, Operation operation) {
		super(SocialNetworkSource.Facebook.toString(), operation);
		setId(SocialNetworkSource.Facebook+"#"+id);
	}
	
	public FacebookItem(Post post) {
		
		super(SocialNetworkSource.Facebook.toString(), Operation.NEW);
		
		if (post == null || post.getId() == null) return;
		
		//Id
		id = SocialNetworkSource.Facebook+"#"+post.getId();
		//SocialNetwork Name
		streamId = SocialNetworkSource.Facebook.toString();
		//Timestamp of the creation of the post
		publicationTime = post.getCreatedTime().getTime();
		//Message that post contains
		String msg = post.getMessage();
		if(msg != null) {
			if(msg.length()>100) {
				title = msg.subSequence(0, 100)+"...";
			}
			else {
				title = msg;
			}
		}
		//All the text inside the post
		text = msg; 
		description = post.getDescription();
		
		//Location 
		Place place = post.getPlace();
		if(place != null) {
			String placeName = place.getName();
			com.restfb.types.Location loc = place.getLocation();
			if(loc != null) {
				Double latitude = loc.getLatitude();
				Double longitude = loc.getLongitude();
		
				location = new Location(latitude, longitude, placeName);
			}
		}
		//Popularity of the post
		if(post.getLikesCount() != null)
			likes = post.getLikesCount();
		else {
			Likes likesPosts = post.getLikes();
			if(likesPosts!=null) {
				if(likesPosts.getCount()==null) {
					List<NamedFacebookType> likeData = likesPosts.getData();
					if(likeData != null) {
						likes = (long) likeData.size();
					}
				}
				else {
					likes = likesPosts.getCount();
				}
			}
		}
		
		if(post.getSharesCount() != null)
			shares = post.getSharesCount();
		
		//Media Items - WebPages in a post
		String type = post.getType();
		
		if(type.equals("photo")) {
			
			String picture = post.getPicture();
			
			try {
				if (picture != null) { 
					URL p_url = null;
					StringBuilder b = new StringBuilder(picture);
					int index = picture.lastIndexOf("_s.");
					if(index>0) {
						b.replace(index, index+3, "_n." );
						picture = picture.replaceAll("_s.", "_n.");
						p_url = new URL(picture);
					
						if(p_url != null){
							
							String mediaId = SocialNetworkSource.Facebook+"#"+post.getId();
							//url
							MediaItem mediaItem = new MediaItem(p_url);
							
							//id
							mediaItem.setId(mediaId);
							//SocialNetwork Name
							mediaItem.setStreamId(streamId);
							//Reference
							mediaItem.setRef(id);
							//Type 
							mediaItem.setType("image");
							//Time of publication
							mediaItem.setPublicationTime(publicationTime);
							//Author
							mediaItem.setUser(streamUser);
							//PageUrl
							String pageUrl = post.getLink();
							mediaItem.setPageUrl(pageUrl);
							//Thumbnail
							String thumbnail = post.getPicture();
							mediaItem.setThumbnail(thumbnail);
							//Title
							mediaItem.setTitle(title);
							//Description
							mediaItem.setDescription(description);
							//Tags
							mediaItem.setTags(tags);
							//Popularity
							mediaItem.setLikes(likes);
							mediaItem.setShares(shares);
					
							//Store mediaItems and their ids 
							mediaItems.add(mediaItem);
							
							mediaIds.add(mediaId);
						}
					}
				}
			} catch (MalformedURLException e) { 
				e.printStackTrace();
			}
		}
		else if(type.equals("link")) {
			
			webPages = new ArrayList<WebPage>();
			String picture = post.getPicture(); ///!!!!
			if (picture != null) { <commit_msg>added comments <commit_after>package eu.socialsensor.framework.abstractions.socialmedia.facebook;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.restfb.types.Comment;
import com.restfb.types.NamedFacebookType;
import com.restfb.types.Place;
import com.restfb.types.Post;
import com.restfb.types.Post.Likes;
import com.restfb.types.User;

import eu.socialsensor.framework.common.domain.Item;
import eu.socialsensor.framework.common.domain.Location;
import eu.socialsensor.framework.common.domain.MediaItem;
import eu.socialsensor.framework.common.domain.SocialNetworkSource;
import eu.socialsensor.framework.common.domain.WebPage;

/**
 * Class that holds the information regarding the facebook post
 * @author ailiakop
 * @author <EMAIL>
 *
 */
public class FacebookItem extends Item {
	
	public FacebookItem(String id, Operation operation) {
		super(SocialNetworkSource.Facebook.toString(), operation);
		setId(SocialNetworkSource.Facebook+"#"+id);
	}
	
	public FacebookItem(Post post) {
		
		super(SocialNetworkSource.Facebook.toString(), Operation.NEW);
		
		if (post == null || post.getId() == null) return;
		
		//Id
		id = SocialNetworkSource.Facebook+"#"+post.getId();
		//SocialNetwork Name
		streamId = SocialNetworkSource.Facebook.toString();
		//Timestamp of the creation of the post
		publicationTime = post.getCreatedTime().getTime();
		//Message that post contains
		String msg = post.getMessage();
		if(msg != null) {
			if(msg.length()>100) {
				title = msg.subSequence(0, 100)+"...";
			}
			else {
				title = msg;
			}
		}
		//All the text inside the post
		text = msg; 
		description = post.getDescription();
		
		//Location 
		Place place = post.getPlace();
		if(place != null) {
			String placeName = place.getName();
			com.restfb.types.Location loc = place.getLocation();
			if(loc != null) {
				Double latitude = loc.getLatitude();
				Double longitude = loc.getLongitude();
		
				location = new Location(latitude, longitude, placeName);
			}
		}
		//Popularity of the post
		if(post.getLikesCount() != null)
			likes = post.getLikesCount();
		else {
			Likes likesPosts = post.getLikes();
			if(likesPosts!=null) {
				if(likesPosts.getCount()==null) {
					List<NamedFacebookType> likeData = likesPosts.getData();
					if(likeData != null) {
						likes = (long) likeData.size();
					}
				}
				else {
					likes = likesPosts.getCount();
				}
			}
		}
		
		if(post.getSharesCount() != null)
			shares = post.getSharesCount();
		
		//Media Items - WebPages in a post
		String type = post.getType();
		
		if(type.equals("photo")) {
			
			String picture = post.getPicture();
			
			try {
				if (picture != null) { 
					URL p_url = null;
					StringBuilder b = new StringBuilder(picture);
					int index = picture.lastIndexOf("_s.");
					if(index>0) {
						b.replace(index, index+3, "_n." );
						picture = picture.replaceAll("_s.", "_n.");
						p_url = new URL(picture);
					
						if(p_url != null){
							
							String mediaId = SocialNetworkSource.Facebook+"#"+post.getId();
							//url
							MediaItem mediaItem = new MediaItem(p_url);
							
							//id
							mediaItem.setId(mediaId);
							//SocialNetwork Name
							mediaItem.setStreamId(streamId);
							//Reference
							mediaItem.setRef(id);
							//Type 
							mediaItem.setType("image");
							//Time of publication
							mediaItem.setPublicationTime(publicationTime);
							//Author
							mediaItem.setUser(streamUser);
							//PageUrl
							String pageUrl = post.getLink();
							mediaItem.setPageUrl(pageUrl);
							//Thumbnail
							String thumbnail = post.getPicture();
							mediaItem.setThumbnail(thumbnail);
							//Title
							mediaItem.setTitle(title);
							//Description
							mediaItem.setDescription(description);
							//Tags
							mediaItem.setTags(tags);
							//Popularity
							mediaItem.setLikes(likes);
							mediaItem.setShares(shares);
							Integer commentsCount = post.getComments().getData().size();
							mediaItem.setComments(commentsCount.longValue());
							
							//Store mediaItems and their ids 
							mediaItems.add(mediaItem);
							
							mediaIds.add(mediaId);
						}
					}
				}
			} catch (MalformedURLException e) { 
				e.printStackTrace();
			}
		}
		else if(type.equals("link")) {
			
			webPages = new ArrayList<WebPage>();
			String picture = post.getPicture(); ///!!!!
			if (picture != null) { 
{'fraction_non_alphanumeric': 0.13198316076914324, 'fraction_numerical': 0.0020480145636591193, 'mean_word_length': 2.9049311417147936, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2897861', 'n_tokens_mistral': 3622, 'n_tokens_neox': 2662, 'n_words': 542}