use of twitter4j.Twitter in project camel by apache.
the class SearchProducer method process.
@Override
public void process(Exchange exchange) throws Exception {
long myLastId = lastId;
// KEYWORDS
// keywords from header take precedence
String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class);
if (keywords == null) {
keywords = endpoint.getProperties().getKeywords();
}
if (keywords == null) {
throw new CamelExchangeException("No keywords to use for query", exchange);
}
Query query = new Query(keywords);
// filter of older tweets
if (endpoint.getProperties().isFilterOld() && myLastId != 0) {
query.setSinceId(myLastId);
}
// since id
Long sinceId = exchange.getIn().getHeader(TwitterConstants.TWITTER_SINCEID, Long.class);
if (sinceId == null) {
sinceId = endpoint.getProperties().getSinceId();
}
if (ObjectHelper.isNotEmpty(sinceId)) {
query.setSinceId(sinceId);
}
// max id
Long maxId = exchange.getIn().getHeader(TwitterConstants.TWITTER_MAXID, Long.class);
if (ObjectHelper.isNotEmpty(maxId)) {
query.setMaxId(maxId);
}
// language
String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class);
if (lang == null) {
lang = endpoint.getProperties().getLang();
}
if (ObjectHelper.isNotEmpty(lang)) {
query.setLang(lang);
}
// number of elements per page
Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class);
if (count == null) {
count = endpoint.getProperties().getCount();
}
if (ObjectHelper.isNotEmpty(count)) {
query.setCount(count);
}
// number of pages
Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class);
if (numberOfPages == null) {
numberOfPages = endpoint.getProperties().getNumberOfPages();
}
Twitter twitter = endpoint.getProperties().getTwitter();
log.debug("Searching twitter with keywords: {}", keywords);
QueryResult results = twitter.search(query);
List<Status> list = results.getTweets();
for (int i = 1; i < numberOfPages; i++) {
if (!results.hasNext()) {
break;
}
log.debug("Fetching page");
results = twitter.search(results.nextQuery());
list.addAll(results.getTweets());
}
if (endpoint.getProperties().isFilterOld()) {
for (Status t : list) {
long newId = t.getId();
if (newId > myLastId) {
myLastId = newId;
}
}
}
exchange.getIn().setBody(list);
// update the lastId after finished the processing
if (myLastId > lastId) {
lastId = myLastId;
}
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class TimeLineCursorAdapter method getCounts.
public void getCounts(final ViewHolder holder, final long tweetId) {
Thread getCount = new Thread(new Runnable() {
@Override
public void run() {
try {
Twitter twitter = getTwitter();
final Status status;
if (holder.retweeter.getVisibility() != View.GONE) {
status = twitter.showStatus(holder.tweetId).getRetweetedStatus();
} else {
status = twitter.showStatus(tweetId);
}
if (status != null && holder.tweetId == tweetId) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
holder.favCount.setText(" " + status.getFavoriteCount());
holder.retweetCount.setText(" " + status.getRetweetCount());
if (status.isFavorited()) {
TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.favoritedButton });
int resource = a.getResourceId(0, 0);
a.recycle();
if (!settings.addonTheme) {
holder.favorite.setColorFilter(context.getResources().getColor(R.color.app_color));
} else {
holder.favorite.setColorFilter(settings.accentInt);
}
holder.favorite.setImageDrawable(context.getResources().getDrawable(resource));
holder.isFavorited = true;
} else {
TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.notFavoritedButton });
int resource = a.getResourceId(0, 0);
a.recycle();
holder.favorite.setImageDrawable(context.getResources().getDrawable(resource));
holder.isFavorited = false;
holder.favorite.clearColorFilter();
}
if (status.isRetweetedByMe()) {
if (!settings.addonTheme) {
holder.retweet.setColorFilter(context.getResources().getColor(R.color.app_color));
} else {
holder.retweet.setColorFilter(settings.accentInt);
}
} else {
holder.retweet.clearColorFilter();
}
}
});
}
} catch (Exception e) {
}
}
});
getCount.setPriority(7);
getCount.start();
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class SavedSearchArrayAdapter method bindView.
@Override
public void bindView(final View view, Context mContext, final String trend) {
final ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(trend);
holder.text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent search = new Intent(context, SearchedTrendsActivity.class);
search.setAction(Intent.ACTION_SEARCH);
search.putExtra(SearchManager.QUERY, trend);
context.startActivity(search);
}
});
holder.text.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final String search = trend;
new AlertDialog.Builder(context).setTitle(context.getResources().getString(R.string.delete_saved_search)).setMessage(context.getResources().getString(R.string.cache_dialog_summary)).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(context, context.getString(R.string.deleting_search), Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
long id = -1;
Twitter twitter = Utils.getTwitter(context, AppSettings.getInstance(context));
try {
ResponseList<SavedSearch> searches = twitter.savedSearches().getSavedSearches();
for (int i = 0; i < searches.size(); i++) {
String name = searches.get(i).getName();
Log.v("talon_saved_searches", name);
if (name.equals(search)) {
id = searches.get(i).getId();
Log.v("talon_saved_searches", "id to delete: " + id);
}
}
if (id != -1) {
twitter.destroySavedSearch(id);
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, context.getString(R.string.success), Toast.LENGTH_SHORT).show();
SavedSearchesActivity.getSearches();
}
});
} else {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
}
});
}
} catch (Exception e) {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
}
});
}
}
}).start();
}
}).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create().show();
return false;
}
});
}
use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.
the class FavoriterUtils method getFavoriters.
public List<User> getFavoriters(Context context, long tweetId) {
List<User> users = new ArrayList<User>();
Twitter twitter = Utils.getTwitter(context);
try {
Status stat = twitter.showStatus(tweetId);
if (stat.isRetweet()) {
tweetId = stat.getRetweetedStatus().getId();
}
long[] ids = getFavoritersIds(tweetId);
users = twitter.lookupUsers(ids);
} catch (TwitterException e) {
e.printStackTrace();
}
return users;
}
use of twitter4j.Twitter in project twitter-2-weibo by rjyo.
the class AuthServlet method doGet.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpServletRouter r = new HttpServletRouter(request);
r.setPattern("/:type");
response.setContentType("text/plain");
PrintWriter writer = response.getWriter();
String serverPath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
HttpSession session = request.getSession();
if (r.is(":type", "weibo")) {
try {
Oauth oauth = new Oauth();
String redirectUrl = oauth.authorize("code");
response.setStatus(302);
response.setHeader("Location", redirectUrl);
log.info("Redirecting Weibo...");
} catch (WeiboException e) {
log.error(e);
}
} else if (r.is(":type", "twitter")) {
log.info("hello world~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
try {
TwitterFactory factory = new TwitterFactory();
Twitter t = factory.getInstance();
twitter4j.auth.RequestToken requestToken = t.getOAuthRequestToken(serverPath + "/callback/twitter");
response.setStatus(302);
log.info(requestToken.getAuthenticationURL());
response.setHeader("Location", requestToken.getAuthenticationURL());
session.setAttribute(Keys.SESSION_REQUEST_TOKEN, requestToken);
log.info("Redirecting Twitter...");
} catch (TwitterException e) {
log.error(e);
}
writer.close();
} else {
response.setStatus(200);
writer.println("Wrong parameter, not working!");
writer.close();
}
}
Aggregations