use of twitter4j.GeoLocation in project ddf by codice.
the class TwitterSource method getMetacard.
private Metacard getMetacard(Status status) {
MetacardImpl metacard = new MetacardImpl();
metacard.setSourceId(id);
metacard.setId(String.valueOf(status.getId()));
metacard.setTitle(status.getText());
metacard.setMetadata("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<Resource>" + "<name>" + status.getText() + "</name>" + "</Resource>");
metacard.setCreatedDate(status.getCreatedAt());
metacard.setModifiedDate(status.getCreatedAt());
metacard.setEffectiveDate(status.getCreatedAt());
metacard.setPointOfContact(status.getUser().getName());
if (status.getURLEntities() != null && status.getURLEntities().length > 0) {
try {
metacard.setResourceURI(new URI(status.getURLEntities()[0].getExpandedURL()));
} catch (URISyntaxException e) {
LOGGER.error("Unable to set resource URI.", e);
}
} else if (status.getMediaEntities() != null && status.getMediaEntities().length > 0) {
try {
metacard.setResourceURI(new URI(status.getMediaEntities()[0].getExpandedURL()));
} catch (URISyntaxException e) {
LOGGER.error("Unable to set resource URI.", e);
}
} else if (status.getExtendedMediaEntities() != null && status.getExtendedMediaEntities().length > 0) {
try {
metacard.setResourceURI(new URI(status.getExtendedMediaEntities()[0].getExpandedURL()));
} catch (URISyntaxException e) {
LOGGER.error("Unable to set resource URI.", e);
}
}
GeoLocation geoLocation = status.getGeoLocation();
if (geoLocation != null) {
metacard.setLocation("POINT (" + geoLocation.getLongitude() + " " + geoLocation.getLatitude() + ")");
}
return metacard;
}
use of twitter4j.GeoLocation in project twicalico by moko256.
the class PostActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
model = PostTweetModelCreator.getInstance(GlobalApplication.twitter, getContentResolver());
subscription = new CompositeSubscription();
rootViewGroup = findViewById(R.id.activity_tweet_send_layout_root);
toolbar = findViewById(R.id.activity_tweet_send_toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
userIcon = findViewById(R.id.activity_tweet_send_user_icon);
GlideApp.with(this).load(GlobalApplication.userCache.get(GlobalApplication.userId).get400x400ProfileImageURLHttps()).circleCrop().into(userIcon);
counterTextView = findViewById(R.id.tweet_text_edit_counter);
editText = findViewById(R.id.tweet_text_edit);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
model.setTweetText(s.toString());
counterTextView.setText(String.valueOf(model.getTweetLength()) + " / " + String.valueOf(model.getMaxTweetLength()));
counterTextView.setTextColor(model.isValidTweet() ? Color.GRAY : Color.RED);
}
@Override
public void afterTextChanged(Editable s) {
}
});
editText.setImageAddedListener(imageUri -> {
if (model.getUriList().size() < model.getUriListSizeLimit()) {
addedImagesAdapter.getImagesList().add(imageUri);
model.getUriList().add(imageUri);
addedImagesAdapter.notifyItemChanged(addedImagesAdapter.getImagesList().size() - 1);
possiblySensitiveSwitch.setEnabled(true);
return true;
} else {
return false;
}
});
editText.setHint(model.isReply() ? R.string.reply : R.string.post);
imagesRecyclerView = findViewById(R.id.activity_tweet_send_images_recycler_view);
addedImagesAdapter = new AddedImagesAdapter(this);
imagesRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
imagesRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.right = Math.round(getResources().getDisplayMetrics().density);
}
});
addedImagesAdapter.setLimit(model.getUriListSizeLimit());
addedImagesAdapter.setOnAddButtonClickListener(v -> startActivityForResult(Intent.createChooser(new Intent(Intent.ACTION_GET_CONTENT).addCategory(Intent.CATEGORY_OPENABLE).putExtra(Intent.EXTRA_MIME_TYPES, new String[] { "image/*", "video/*" }).putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true).setType("*/*"), getString(R.string.add_image)), REQUEST_GET_IMAGE));
addedImagesAdapter.setOnDeleteButtonListener(position -> {
addedImagesAdapter.getImagesList().remove(position);
model.getUriList().remove(position);
addedImagesAdapter.notifyDataSetChanged();
boolean enabled = model.getUriList().size() > 0;
possiblySensitiveSwitch.setEnabled(enabled);
possiblySensitiveSwitch.setChecked(possiblySensitiveSwitch.isChecked() && enabled);
});
addedImagesAdapter.setOnImageClickListener(position -> {
Intent open = new Intent(Intent.ACTION_VIEW).setData(model.getUriList().get(position)).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(open, getString(R.string.open_image)));
});
imagesRecyclerView.setAdapter(addedImagesAdapter);
possiblySensitiveSwitch = findViewById(R.id.activity_tweet_possibly_sensitive_switch);
possiblySensitiveSwitch.setEnabled(addedImagesAdapter.getImagesList().size() > 0);
possiblySensitiveSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> model.setPossiblySensitive(isChecked));
addLocationSwitch = findViewById(R.id.activity_tweet_location_switch);
addLocationSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PermissionChecker.PERMISSION_GRANTED) {
subscription.add(getLocation().subscribe(it -> {
model.setLocation(new GeoLocation(it.getLatitude(), it.getLongitude()));
locationText.setVisibility(View.VISIBLE);
locationText.setText(getString(R.string.lat_and_lon, it.getLatitude(), it.getLongitude()));
}, Throwable::printStackTrace));
} else {
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 400);
}
} else {
locationText.setVisibility(View.GONE);
}
});
locationText = findViewById(R.id.activity_tweet_location_result);
locationText.setVisibility(View.GONE);
if (getIntent() != null) {
if (!model.isReply()) {
model.setInReplyToStatusId(getIntent().getLongExtra(INTENT_EXTRA_IN_REPLY_TO_STATUS_ID, -1));
}
String text = getIntent().getStringExtra(INTENT_EXTRA_TWEET_TEXT);
if (text != null) {
editText.setText(text);
editText.setSelection(text.length());
} else {
editText.setText("");
}
ArrayList<Uri> uris = getIntent().getParcelableArrayListExtra(INTENT_EXTRA_IMAGE_URI);
if (uris != null) {
addedImagesAdapter.getImagesList().addAll(uris);
model.getUriList().addAll(uris);
possiblySensitiveSwitch.setEnabled(true);
}
}
}
use of twitter4j.GeoLocation in project Talon-for-Twitter by klinker24.
the class LocalTrends method getTrends.
public void getTrends() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings);
int i = 0;
while (!connected && i < 5) {
try {
Thread.sleep(1500);
} catch (Exception e) {
}
i++;
}
twitter4j.Trends trends;
if (sharedPrefs.getBoolean("manually_config_location", false)) {
// chicago to default
trends = twitter.getPlaceTrends(sharedPrefs.getInt("woeid", 2379574));
} else {
Location location = mLastLocation;
ResponseList<twitter4j.Location> locations = twitter.getClosestTrends(new GeoLocation(location.getLatitude(), location.getLongitude()));
trends = twitter.getPlaceTrends(locations.get(0).getWoeid());
}
final ArrayList<String> currentTrends = new ArrayList<String>();
for (Trend t : trends.getTrends()) {
String name = t.getName();
currentTrends.add(name);
}
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
try {
if (currentTrends != null) {
listView.setAdapter(new TrendsArrayAdapter(context, currentTrends));
listView.setVisibility(View.VISIBLE);
} else {
Toast.makeText(context, getResources().getString(R.string.no_location), Toast.LENGTH_SHORT).show();
}
LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
spinner.setVisibility(View.GONE);
} catch (Exception e) {
// not attached to activity
}
}
});
HashtagDataSource source = HashtagDataSource.getInstance(context);
for (String s : currentTrends) {
Log.v("talon_hashtag", "trend: " + s);
if (s.contains("#")) {
// we want to add it to the auto complete
Log.v("talon_hashtag", "adding: " + s);
// could be much more efficient by querying and checking first, but I
// just didn't feel like it when there is only ever 10 of them here
source.deleteTag(s);
// add it to the userAutoComplete database
source.createTag(s);
}
}
} catch (Throwable e) {
e.printStackTrace();
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(context, getResources().getString(R.string.no_location), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// not attached to activity
}
}
});
}
}
}).start();
}
use of twitter4j.GeoLocation in project Talon-for-Twitter by klinker24.
the class LocalTrendsFragment method getTrends.
@Override
protected Trends getTrends() {
try {
Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings);
Trends trends;
if (sharedPrefs.getBoolean("manually_config_location", false)) {
// chicago to default
trends = twitter.getPlaceTrends(sharedPrefs.getInt("woeid", 2379574));
} else {
mGoogleApiClient.connect();
connected = false;
int i = 0;
while (!connected && i < 5) {
try {
Thread.sleep(1500);
} catch (Exception e) {
}
i++;
}
Location location = mLastLocation;
ResponseList<twitter4j.Location> locations = twitter.getClosestTrends(new GeoLocation(location.getLatitude(), location.getLongitude()));
trends = twitter.getPlaceTrends(locations.get(0).getWoeid());
}
return trends;
} catch (Exception e) {
return null;
}
}
use of twitter4j.GeoLocation in project camel by apache.
the class SearchConsumer method search.
private List<Exchange> search(Query query) throws TwitterException {
Integer numberOfPages = 1;
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getLang())) {
query.setLang(endpoint.getProperties().getLang());
}
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getCount())) {
query.setCount(endpoint.getProperties().getCount());
}
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getNumberOfPages())) {
numberOfPages = endpoint.getProperties().getNumberOfPages();
}
if (ObjectHelper.isNotEmpty(endpoint.getProperties().getLatitude()) && ObjectHelper.isNotEmpty(endpoint.getProperties().getLongitude()) && ObjectHelper.isNotEmpty(endpoint.getProperties().getRadius())) {
GeoLocation location = new GeoLocation(endpoint.getProperties().getLatitude(), endpoint.getProperties().getLongitude());
query.setGeoCode(location, endpoint.getProperties().getRadius(), Unit.valueOf(endpoint.getProperties().getDistanceMetric()));
LOG.debug("Searching with additional geolocation parameters.");
}
LOG.debug("Searching with {} pages.", numberOfPages);
Twitter twitter = getTwitter();
QueryResult qr = twitter.search(query);
List<Status> tweets = qr.getTweets();
for (int i = 1; i < numberOfPages; i++) {
if (!qr.hasNext()) {
break;
}
qr = twitter.search(qr.nextQuery());
tweets.addAll(qr.getTweets());
}
if (endpoint.getProperties().isFilterOld()) {
for (int i = 0; i < tweets.size(); i++) {
setLastIdIfGreater(tweets.get(i).getId());
}
}
return TwitterEventType.STATUS.createExchangeList(endpoint, tweets);
}
Aggregations