Set Twitter search or stream criteria for the selection of tweets
3896
def searchTag(self,HTAG="#python"):
        """"""
        self.t = Twython(app_key           =self.app_key           ,
                        app_secret         =self.app_secret        ,
                        oauth_token        =self.oauth_token       ,
                        oauth_token_secret =self.oauth_token_secret)

        search =self.t.search(q=HTAG,count=100,result_type="recent")
        ss=search[:]
        search = self.t.search(q=HTAG,count=150,max_id=ss[-1]['id']-1,result_type="recent")
        #search = t.search(q=HTAG,count=150,since_id=ss[-1]['id'],result_type="recent")
        while seach:
            ss+=search[:]
            search = self.t.search(q=HTAG,count=150,max_id=ss[-1]['id']-1,result_type="recent")
        self.ss=ss
308825
def _stream_search(self, *args, **kwargs):
        """"""
        for hit in scan(
            self.elastic, query=kwargs.pop("body", None), scroll="10m", **kwargs
        ):
            hit["_source"]["_id"] = hit["_id"]
            yield hit["_source"]
1334335
def geo_search(user_id, search_location):
    
    url = "https://api.twitter.com/1.1/geo/search.json"
    params =  {"query" : search_location }
    response = make_twitter_request(url, user_id, params).json()
    return response
962789
def get_dweets_for(thing_name, key=None, session=None):
    
    if key is not None:
        params = {'key': key}
    else:
        params = None
    return _request('get', '/get/dweets/for/{0}'.format(thing_name), params=params, session=None)
88403
def tweet(ctx, message):
    """"""
    if not valid_tweet(message):
        click.echo("Message is too long for twitter.")
        click.echo("Message:" + message)
        ctx.exit(2)

    if not ctx.obj['DRYRUN']:
        ctx.obj['TWEEPY_API'].update_status(message)
    else:
        click.echo("Tweet not sent due to dry-run mode.")
158859
def search(ont, searchterm):
    
    namedGraph = get_named_graph(ont)
    query = """
    SELECT ?c ?l WHERE {{
    GRAPH <{g}>  {{
    ?c rdfs:label ?l
    FILTER regex(?l,'{s}','i')
    }}
    }}
    """.format(s=searchterm, g=namedGraph)
    bindings = run_sparql(query)
    return [(r['c']['value'],r['l']['value']) for r in bindings]
6163
def retweet?(status)
      filters = load_filters
      retweet = true
      if !filters.nil?
        filters.each_pair do |path, value|
          array = []
          array << value
          array.flatten.each do |filter_value|
            path_value = StreamBot::ArrayPath.get_path(status, path)
            if path_value.eql?(filter_value) || path_value.include?(filter_value)
              on_match.trigger(status, path, filter_value)
              retweet = false
            end
          end
        end
      end
      retweet
    end
755039
public Iterable<DContact> queryByTwitter(Object parent, java.lang.String twitter) {
    return queryByField(parent, DContactMapper.Field.TWITTER.getFieldName(), twitter);
  }
991041
@Override
    public void getSavedSearches() {
        getDispatcher().invokeLater(new AsyncTask(SAVED_SEARCHES, listeners) {
            @Override
            public void invoke(List<TwitterListener> listeners) throws TwitterException {
                ResponseList<SavedSearch> savedSearches = twitter.getSavedSearches();
                for (TwitterListener listener : listeners) {
                    try {
                        listener.gotSavedSearches(savedSearches);
                    } catch (Exception e) {
                        logger.warn("Exception at getSavedSearches", e);
                    }
                }
            }
        });
    }
473899
def filter(self, track=None, follow=None, locations=None, event=None,
               record_keepalive=False):
        
        if locations is not None:
            if type(locations) == list:
                locations = ','.join(locations)
            locations = locations.replace('\\', '')

        url = 'https://stream.twitter.com/1.1/statuses/filter.json'
        params = {
            "stall_warning": True,
            "include_ext_alt_text": True
        }
        if track:
            params["track"] = track
        if follow:
            params["follow"] = follow
        if locations:
            params["locations"] = locations
        headers = {'accept-encoding': 'deflate, gzip'}
        errors = 0
        while True:
            try:
                log.info("connecting to filter stream for %s", params)
                resp = self.post(url, params, headers=headers, stream=True)
                errors = 0
                for line in resp.iter_lines(chunk_size=1024):
                    if event and event.is_set():
                        log.info("stopping filter")
                        # Explicitly close response
                        resp.close()
                        return
                    if not line:
                        log.info("keep-alive")
                        if record_keepalive:
                            yield "keep-alive"
                        continue
                    try:
                        yield json.loads(line.decode())
                    except Exception as e:
                        log.error("json parse error: %s - %s", e, line)
            except requests.exceptions.HTTPError as e:
                errors += 1
                log.error("caught http error %s on %s try", e, errors)
                if self.http_errors and errors == self.http_errors:
                    log.warning("too many errors")
                    raise e
                if e.response.status_code == 420:
                    if interruptible_sleep(errors * 60, event):
                        log.info("stopping filter")
                        return
                else:
                    if interruptible_sleep(errors * 5, event):
                        log.info("stopping filter")
                        return
            except Exception as e:
                errors += 1
                log.error("caught exception %s on %s try", e, errors)
                if self.http_errors and errors == self.http_errors:
                    log.warning("too many exceptions")
                    raise e
                log.error(e)
                if interruptible_sleep(errors, event):
                    log.info("stopping filter")
                    return
521080
public static void main(String[] args) {
        try {
            Twitter twitter = new TwitterFactory().getInstance();
            List<SavedSearch> savedSearches = twitter.getSavedSearches();
            for (SavedSearch savedSearch : savedSearches) {
                System.out.println("[name:" + savedSearch.getName() + " query:" + savedSearch.getQuery() + " id:"
                        + savedSearch.getId() + "]");
            }
            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to retrieve saved searches: " + te.getMessage());
            System.exit(-1);
        }
    }
1214782
public function search(string $query, array $arguments = null)
    {
        $args = ['q' => $query];
        if (is_array($arguments)) {
            $args = array_merge($arguments, $args);
        }
        return new SearchIterator($this, 'search/tweets', $args);
    }
843540
public static void main(String[] args) throws TwitterException {
        if (args.length < 1) {
            System.out.println("Usage: java twitter4j.examples.PrintFilterStream [follow(comma separated numerical user ids)] [track(comma separated filter terms)]");
            System.exit(-1);
        }

        TwitterStream twitterStream = new TwitterStreamFactory().getInstance().addListener(new StatusListener() {
            @Override
            public void onStatus(Status status) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
            }

            @Override
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
                System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
            }

            @Override
            public void onScrubGeo(long userId, long upToStatusId) {
                System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
            }

            @Override
            public void onStallWarning(StallWarning warning) {
                System.out.println("Got stall warning:" + warning);
            }

            @Override
            public void onException(Exception ex) {
                ex.printStackTrace();
            }
        });

        ArrayList<Long> follow = new ArrayList<Long>();
        ArrayList<String> track = new ArrayList<String>();
        for (String arg : args) {
            if (isNumericalArgument(arg)) {
                for (String id : arg.split(",")) {
                    follow.add(Long.parseLong(id));
                }
            } else {
                track.addAll(Arrays.asList(arg.split(",")));
            }
        }
        long[] followArray = new long[follow.size()];
        for (int i = 0; i < follow.size(); i++) {
            followArray[i] = follow.get(i);
        }
        String[] trackArray = track.toArray(new String[track.size()]);

        // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
        twitterStream.filter(new FilterQuery(0, followArray, trackArray));
    }
516367
def sample(self, **params):
        
        url = 'https://stream.twitter.com/%s/statuses/sample.json' \
              % self.streamer.api_version
        self.streamer._request(url, params=params)
948548
def urlize_tweet(tweet):
    
    if tweet['retweeted']:
        text = 'RT {user}: {text}'.format(
            user=TWITTER_USERNAME_URL.format(screen_name=tweet['user']['screen_name']),
            text=tweet['text'])
    else:
        text = tweet['text']

    for hashtag in tweet['entities']['hashtags']:
        text = text.replace(
            '#%s' % hashtag['text'],
            TWITTER_HASHTAG_URL.format(hashtag=hashtag['text']))

    for mention in tweet['entities']['user_mentions']:
        text = text.replace(
            '@%s' % mention['screen_name'],
            TWITTER_USERNAME_URL.format(screen_name=mention['screen_name']))

    urls = tweet['entities']['urls']
    for url in urls:
        text = text.replace(
            url['url'], TWITTER_URL.format(
                url=url['expanded_url'], display_url=url['display_url']))

    if 'media' in tweet['entities']:
        for media in tweet['entities']['media']:
            text = text.replace(
                media['url'],
                TWITTER_MEDIA_URL.format(
                    url=media['expanded_url'],
                    display_url=media['display_url']))

    return mark_safe(text)
1362965
private static void assignVideosToTweet(Tweets tweet, String videoId, String videoName)
    {
        Set<Video> videos = new HashSet<Video>();
        Video video = new Video();
        video.setVideoId(videoId);
        video.setVideoName(videoName);
        videos.add(video);
        tweet.setVideos(videos);
    }
1335619
@Override
    public void search(final Query query) {
        getDispatcher().invokeLater(new AsyncTask(SEARCH, listeners) {
            @Override
            public void invoke(List<TwitterListener> listeners) throws TwitterException {
                QueryResult result = twitter.search(query);
                for (TwitterListener listener : listeners) {
                    try {
                        listener.searched(result);
                    } catch (Exception e) {
                        logger.warn("Exception at search", e);
                    }
                }
            }
        });
    }