Search in sources :

Example 1 with GeoLocation

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;
}
Also used : URISyntaxException(java.net.URISyntaxException) GeoLocation(twitter4j.GeoLocation) URI(java.net.URI) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 2 with GeoLocation

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);
        }
    }
}
Also used : Context(android.content.Context) Rect(android.graphics.Rect) Bundle(android.os.Bundle) PostTweetModel(com.github.moko256.twicalico.model.base.PostTweetModel) Criteria(android.location.Criteria) Uri(android.net.Uri) ImageView(android.widget.ImageView) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) LocationListener(android.location.LocationListener) TwitterStringUtils(com.github.moko256.twicalico.text.TwitterStringUtils) Editable(android.text.Editable) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) ClipData(android.content.ClipData) Manifest(android.Manifest) ImageKeyboardEditText(com.github.moko256.twicalico.widget.ImageKeyboardEditText) Single(rx.Single) Menu(android.view.Menu) Schedulers(rx.schedulers.Schedulers) View(android.view.View) ActionBar(android.support.v7.app.ActionBar) Parcelable(android.os.Parcelable) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SwitchCompat(android.support.v7.widget.SwitchCompat) ActivityCompat(android.support.v4.app.ActivityCompat) AppCompatActivity(android.support.v7.app.AppCompatActivity) ViewGroup(android.view.ViewGroup) Color(android.graphics.Color) CompositeSubscription(rx.subscriptions.CompositeSubscription) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) Toolbar(android.support.v7.widget.Toolbar) GeoLocation(twitter4j.GeoLocation) PermissionChecker(android.support.v4.content.PermissionChecker) Location(android.location.Location) Snackbar(android.support.design.widget.Snackbar) PostTweetModelCreator(com.github.moko256.twicalico.model.impl.PostTweetModelCreator) LocationManager(android.location.LocationManager) TextWatcher(android.text.TextWatcher) Rect(android.graphics.Rect) Intent(android.content.Intent) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) Uri(android.net.Uri) CompositeSubscription(rx.subscriptions.CompositeSubscription) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) RecyclerView(android.support.v7.widget.RecyclerView) GeoLocation(twitter4j.GeoLocation)

Example 3 with GeoLocation

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();
}
Also used : HashtagDataSource(com.klinker.android.twitter.data.sq_lite.HashtagDataSource) ArrayList(java.util.ArrayList) Trend(twitter4j.Trend) Twitter(twitter4j.Twitter) DrawerActivity(com.klinker.android.twitter.activities.drawer_activities.DrawerActivity) Activity(android.app.Activity) TrendsArrayAdapter(com.klinker.android.twitter.adapters.TrendsArrayAdapter) GeoLocation(twitter4j.GeoLocation) LinearLayout(android.widget.LinearLayout) GeoLocation(twitter4j.GeoLocation) Location(android.location.Location)

Example 4 with GeoLocation

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;
    }
}
Also used : Twitter(twitter4j.Twitter) Trends(twitter4j.Trends) GeoLocation(twitter4j.GeoLocation) GeoLocation(twitter4j.GeoLocation) Location(android.location.Location)

Example 5 with GeoLocation

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);
}
Also used : Status(twitter4j.Status) QueryResult(twitter4j.QueryResult) Twitter(twitter4j.Twitter) GeoLocation(twitter4j.GeoLocation) TwitterEndpoint(org.apache.camel.component.twitter.TwitterEndpoint)

Aggregations

GeoLocation (twitter4j.GeoLocation)9 Twitter (twitter4j.Twitter)5 Location (android.location.Location)4 ArrayList (java.util.ArrayList)4 QueryResult (twitter4j.QueryResult)3 Status (twitter4j.Status)3 Activity (android.app.Activity)2 LinearLayout (android.widget.LinearLayout)2 IOException (java.io.IOException)2 Query (twitter4j.Query)2 TwitterException (twitter4j.TwitterException)2 Manifest (android.Manifest)1 ClipData (android.content.ClipData)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Color (android.graphics.Color)1 Rect (android.graphics.Rect)1 Address (android.location.Address)1 Criteria (android.location.Criteria)1 Geocoder (android.location.Geocoder)1