├── .gitignore ├── README.markdown ├── example.config.properties ├── .settings ├── org.maven.ide.eclipse.prefs └── org.eclipse.jdt.core.prefs ├── .project ├── .classpath ├── src └── main │ └── java │ └── com │ └── podio │ └── sample │ └── alert │ ├── Alert.java │ ├── AlertReader.java │ ├── Importer.java │ └── AlertWriter.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | Podio API Example - Google Alerts 2 | --------------------------------- 3 | 4 | This is an example use of the Podio API to import Google Alerts into Podio -------------------------------------------------------------------------------- /example.config.properties: -------------------------------------------------------------------------------- 1 | google.feed=http://www.google.com/alerts/feeds/00898476767323601411/7430777864198637542 2 | podio.app=13977 3 | podio.endpoint=podio.com 4 | podio.client.mail=client@mail.com 5 | podio.client.secret=clientsecret 6 | podio.user.mail=user@mail.com 7 | podio.user.password=usersecret -------------------------------------------------------------------------------- /.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | #Tue Dec 21 13:50:02 CET 2010 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | fullBuildGoals=process-test-resources 5 | resolveWorkspaceProjects=true 6 | resourceFilterGoals=process-resources resources\:testResources 7 | skipCompilerPlugin=true 8 | version=1 9 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Tue Dec 21 13:50:02 CET 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.source=1.5 7 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | alerts 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.maven.ide.eclipse.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.maven.ide.eclipse.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/java/com/podio/sample/alert/Alert.java: -------------------------------------------------------------------------------- 1 | package com.podio.sample.alert; 2 | 3 | public final class Alert { 4 | 5 | private final String id; 6 | 7 | private final String title; 8 | 9 | private final String content; 10 | 11 | private final String link; 12 | 13 | public Alert(String id, String title, String content, String link) { 14 | super(); 15 | this.id = id; 16 | this.title = title; 17 | this.content = content; 18 | this.link = link; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return "Alert [id=" + id + ", title=" + title + ", content=" + content 24 | + ", link=" + link + "]"; 25 | } 26 | 27 | public String getId() { 28 | return id; 29 | } 30 | 31 | public String getTitle() { 32 | return title; 33 | } 34 | 35 | public String getContent() { 36 | return content; 37 | } 38 | 39 | public String getLink() { 40 | return link; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/podio/sample/alert/AlertReader.java: -------------------------------------------------------------------------------- 1 | package com.podio.sample.alert; 2 | 3 | import java.net.URL; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import com.sun.syndication.feed.synd.SyndContent; 8 | import com.sun.syndication.feed.synd.SyndEntry; 9 | import com.sun.syndication.feed.synd.SyndFeed; 10 | import com.sun.syndication.fetcher.FeedFetcher; 11 | import com.sun.syndication.fetcher.impl.FeedFetcherCache; 12 | import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache; 13 | import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher; 14 | 15 | /** 16 | * Reads a Google Alerts feed 17 | */ 18 | public class AlertReader { 19 | 20 | private final String feed; 21 | 22 | /** 23 | * Creates a new reader for the given feed 24 | * 25 | * @param feed 26 | * The feed to read 27 | */ 28 | public AlertReader(String feed) { 29 | super(); 30 | this.feed = feed; 31 | } 32 | 33 | /** 34 | * Retrieves the feed from delicious as an RSS feed 35 | * 36 | * @return The given feed 37 | * @throws Exception 38 | * If any error occurs during communication with google 39 | */ 40 | private SyndFeed getFeed() throws Exception { 41 | FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); 42 | FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache); 43 | return feedFetcher.retrieveFeed(new URL(feed)); 44 | } 45 | 46 | public List read() throws Exception { 47 | SyndFeed syndFeed = getFeed(); 48 | 49 | List alerts = new ArrayList(); 50 | @SuppressWarnings("unchecked") 51 | List entries = syndFeed.getEntries(); 52 | for (SyndEntry entry : entries) { 53 | if (entry.getTitle().equals("Feeds for Google Alerts")) { 54 | continue; 55 | } 56 | 57 | String id = entry.getUri().substring( 58 | entry.getUri().lastIndexOf('/') + 1); 59 | 60 | SyndContent content = (SyndContent) entry.getContents().get(0); 61 | 62 | alerts.add(new Alert(id, entry.getTitle(), content.getValue(), 63 | entry.getLink())); 64 | } 65 | 66 | return alerts; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/podio/sample/alert/Importer.java: -------------------------------------------------------------------------------- 1 | package com.podio.sample.alert; 2 | 3 | import java.io.FileInputStream; 4 | import java.util.List; 5 | import java.util.Properties; 6 | 7 | import com.podio.APIFactory; 8 | import com.podio.ResourceFactory; 9 | import com.podio.oauth.OAuthClientCredentials; 10 | import com.podio.oauth.OAuthUsernameCredentials; 11 | 12 | /** 13 | * Imports alerts from Google Alerts to a Podio app. The feed, app id and 14 | * authentication configuration must be given in a configuration file. 15 | * 16 | * The app in Podio must have 3 fields with the labels Title, Content and Link. 17 | */ 18 | public final class Importer { 19 | 20 | private Importer() { 21 | } 22 | 23 | /** 24 | * Start the importer with the given configuration file. This will read the 25 | * alerts from Google and write them to an app in Podio. 26 | * 27 | * @param args 28 | * The first parameter must be the path to the configuration file 29 | * @throws Exception 30 | * If any error occurs during execution 31 | */ 32 | public static void main(String[] args) throws Exception { 33 | if (args.length != 1) { 34 | throw new IllegalArgumentException( 35 | "Expected exactly one argument which should be the path of the configuration file"); 36 | } 37 | 38 | Properties config = new Properties(); 39 | config.load(new FileInputStream(args[0])); 40 | 41 | String endpoint = config.getProperty("podio.endpoint", "podio.com"); 42 | 43 | ResourceFactory podioAPI = new ResourceFactory("api." + endpoint, 44 | "upload." + endpoint, 443, true, false, 45 | new OAuthClientCredentials(config 46 | .getProperty("podio.client.mail"), config 47 | .getProperty("podio.client.secret")), 48 | new OAuthUsernameCredentials(config 49 | .getProperty("podio.user.mail"), config 50 | .getProperty("podio.user.password"))); 51 | APIFactory apiFactory = new APIFactory(podioAPI); 52 | 53 | String feed = config.getProperty("google.feed"); 54 | int appId = Integer.parseInt(config.getProperty("podio.app")); 55 | 56 | List alerts = new AlertReader(feed).read(); 57 | new AlertWriter(appId, apiFactory).write(alerts); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.podio.sample 5 | alert 6 | 1.0.0 7 | Google Alert 8 | 9 | 10 | 11 | 12 | maven-compiler-plugin 13 | 14 | 1.6 15 | 1.6 16 | 17 | 18 | 19 | org.apache.maven.plugins 20 | maven-shade-plugin 21 | 1.4 22 | 23 | 24 | package 25 | 26 | shade 27 | 28 | 29 | 30 | 32 | 33 | com.podio.sample.alert.Importer 34 | 35 | 36 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | MIT 48 | http://creativecommons.org/licenses/MIT/ 49 | 50 | 51 | 52 | 53 | com.podio 54 | api 55 | 0.4.0 56 | compile 57 | 58 | 59 | net.java.dev.rome 60 | rome 61 | 1.0.0 62 | compile 63 | 64 | 65 | net.java.dev.rome 66 | rome-fetcher 67 | 1.0.0 68 | jar 69 | compile 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/main/java/com/podio/sample/alert/AlertWriter.java: -------------------------------------------------------------------------------- 1 | package com.podio.sample.alert; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | import com.podio.APIFactory; 8 | import com.podio.app.Application; 9 | import com.podio.app.ApplicationField; 10 | import com.podio.item.FieldValuesUpdate; 11 | import com.podio.item.ItemAPI; 12 | import com.podio.item.ItemBadge; 13 | import com.podio.item.ItemCreate; 14 | 15 | /** 16 | * Writes alerts into a Podio app 17 | */ 18 | public class AlertWriter { 19 | 20 | private final int appId; 21 | 22 | private final APIFactory apiFactory; 23 | 24 | /** 25 | * Creates a new writer that will write to the given app using the API 26 | * 27 | * @param appId 28 | * The id of the app 29 | * @param apiFactory 30 | * The API class to use 31 | */ 32 | public AlertWriter(int appId, APIFactory apiFactory) { 33 | super(); 34 | this.appId = appId; 35 | this.apiFactory = apiFactory; 36 | } 37 | 38 | /** 39 | * Creates the app mapping for the app id from the configuration 40 | * 41 | * @return The app mapping created 42 | */ 43 | private AppMapping getAppMapping() { 44 | Application app = apiFactory.getAppAPI().getApp(appId); 45 | 46 | return AppMapping.get(app); 47 | } 48 | 49 | /** 50 | * Saves the alerts as items in Podio 51 | * 52 | * @param alerts 53 | * The alerts to save 54 | */ 55 | public void write(List alerts) { 56 | AppMapping mapping = getAppMapping(); 57 | ItemAPI itemAPI = apiFactory.getItemAPI(); 58 | 59 | for (Alert alert : alerts) { 60 | // Check that the bookmark has not already been added 61 | List items = itemAPI.getItemsByExternalId(appId, 62 | alert.getId()).getItems(); 63 | if (items.size() == 0) { 64 | // No items exists, so add the item 65 | itemAPI.addItem(appId, mapping.map(alert), false); 66 | } 67 | } 68 | } 69 | 70 | /** 71 | * Maintans a mapping for the individual fields in the app 72 | */ 73 | private static final class AppMapping { 74 | 75 | private final int title; 76 | 77 | private final int content; 78 | 79 | private final int link; 80 | 81 | private AppMapping(int title, int content, int link) { 82 | super(); 83 | this.title = title; 84 | this.content = content; 85 | this.link = link; 86 | } 87 | 88 | /** 89 | * Returns the create object to be used when creating the object in 90 | * Podio 91 | * 92 | * @param alert 93 | * The alert to map 94 | * @return The mapped object 95 | */ 96 | public ItemCreate map(Alert alert) { 97 | List fields = new ArrayList(); 98 | fields.add(new FieldValuesUpdate(title, "value", alert.getTitle())); 99 | fields.add(new FieldValuesUpdate(content, "value", alert 100 | .getContent())); 101 | fields.add(new FieldValuesUpdate(link, "value", alert.getLink())); 102 | 103 | return new ItemCreate(alert.getId(), fields, 104 | Collections. emptyList(), 105 | Collections. emptyList()); 106 | } 107 | 108 | /** 109 | * Creates a mapping configuration based on the app 110 | * 111 | * @param app 112 | * The app to create a mapping for 113 | * @return The mapping created 114 | */ 115 | public static AppMapping get(Application app) { 116 | List fields = app.getFields(); 117 | 118 | return new AppMapping(getField(fields, "Title"), getField(fields, 119 | "Content"), getField(fields, "Link")); 120 | } 121 | 122 | /** 123 | * Finds a field in the list of fields with the given name 124 | * 125 | * @param fields 126 | * The fields to search through 127 | * @param label 128 | * The label to search for 129 | * @return The id of the matching field 130 | */ 131 | private static int getField(List fields, String label) { 132 | for (ApplicationField field : fields) { 133 | if (field.getConfiguration().getLabel().equals(label)) { 134 | return field.getId(); 135 | } 136 | } 137 | 138 | throw new IllegalArgumentException("No field found with the label " 139 | + label); 140 | } 141 | } 142 | } 143 | --------------------------------------------------------------------------------