Comments (1)
For anyone who's interested, here's the code I ended up using to create a post with an image from the page's metatags using Jsoup and commons-io FileUtils:
import java.io.IOException;
import java.net.URL;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Document;
import com.echobox.api.linkedin.client.VersionedLinkedInClient;
import com.echobox.api.linkedin.client.DefaultVersionedLinkedInClient;
import com.echobox.api.linkedin.version.Version;
import com.echobox.api.linkedin.types.urn.URN;
import com.echobox.api.linkedin.types.urn.URNEntityType;
import com.echobox.api.linkedin.types.posts.Post;
import com.echobox.api.linkedin.types.posts.Distribution;
import com.echobox.api.linkedin.types.images.InitializeUploadRequestBody;
import com.echobox.api.linkedin.types.images.InitializeUpload;
import com.echobox.api.linkedin.connection.versioned.VersionedPostConnection;
import com.echobox.api.linkedin.connection.versioned.VersionedImageConnection;
import com.echobox.api.linkedin.util.PostUtils;
import static com.echobox.api.linkedin.types.images.InitializeUploadRequestBody.*;
// Pattern to detect URLs in strings
private static final String URL_REGEX = "\\(?\\b(https?://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
private static final Pattern urlPattern = Pattern.compile(URL_REGEX);
private static final int READ_TIMEOUT = 10000;
private static final int CONNECT_TIMEOUT = 5000;
private URN organizationURN;
private VersionedPostConnection postConnection;
private VersionedImageConnection imageConnection;
public void init(String organizationId, String accessToken) throws IOException
{
VersionedLinkedInClient client = new DefaultVersionedLinkedInClient(accessToken, Version.DEFAULT_VERSION);
postConnection = new VersionedPostConnection(client);
imageConnection = new VersionedImageConnection(client);
organizationURN = new URN(URNEntityType.ORGANIZATION, organizationId);
}
public String sendPost(String text) throws IOException
{
String link = extractUrl(text);
// Crawl the link's page to get the metatags
URL url = new URL(link);
Document doc = Jsoup.parse(url, READ_TIMEOUT);
List<Element> tags = doc.getElementsByTag("meta");
String title = getMetatag(tags, "og:title");
String description = getMetatag(tags, "og:description");
String image = getMetatag(tags, "og:image");
// Download the image file
String filename = image.substring(image.lastIndexOf("/")+1);
File file = File.createTempFile("image-", null);
FileUtils.copyURLToFile(new URL(image), file, CONNECT_TIMEOUT, READ_TIMEOUT);
// Upload the image file and delete the downloaded file
InitializeUploadRequest request = new InitializeUploadRequest(organizationURN);
URN imageURN = imageConnection.uploadImage(new InitializeUploadRequestBody(request), filename, file);
file.delete();
// Create the post and send it
Distribution distribution = new Distribution(Distribution.FeedDistribution.MAIN_FEED);
Post post = new Post(organizationURN, text, distribution,
Post.LifecycleState.PUBLISHED, Post.Visibility.PUBLIC);
PostUtils.fillArticleContent(post, link, imageURN, title, description);
URN postURN = postConnection.createPost(post);
return postURN.getId();
}
private String getMetatag(List<Element> tags, String name)
{
String ret = null;
for(Element tag : tags)
{
String attr = tag.attr("property");
if(attr != null && attr.equals(name))
{
ret = tag.attr("content");
break;
}
}
return ret;
}
private String extractUrl(String text)
{
Matcher m = urlPattern.matcher(text);
return m.find() ? m.group() : null;
}
from ebx-linkedin-sdk.
Related Issues (20)
- How can we share to linkedin a link with a different Thumbnail image
- Upload mapping error HOT 9
- Access feed with posts for connected user posts HOT 1
- Unable to obtain accessToken via ebx-linkedin-sdk HOT 7
- Unable to Post with a loaded videos= HOT 2
- Problems with share IMAGE HOT 1
- Unable to collect Statistics for UGC post. HOT 7
- Unable to post any of the data type in jsonbody other than string HOT 1
- fix getShares method HOT 1
- Integration with Post API HOT 1
- JSONUtils.getValue() should support booleans HOT 1
- DefaultLinkedInExceptionMapper needs a public constructor HOT 1
- DefaultJsonMapper throws error when converting Post jsonString to an object of Post class. HOT 2
- Unable to use createPost for reshare HOT 1
- Support for fetching video details is missing
- Support for digitalmediaAsset field for comment objects
- Unable to deserialize ReshareContext class HOT 1
- Unable to Deserialize PollOption and PollSettings objects HOT 1
- Duplicate id definition in Post and LinkedInURNIdType HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ebx-linkedin-sdk.