Skip to content

Instantly share code, notes, and snippets.

@Zulko
Forked from howdydoody123/analyze.py
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save Zulko/f8735acf94cc97d6c53d to your computer and use it in GitHub Desktop.

Select an option

Save Zulko/f8735acf94cc97d6c53d to your computer and use it in GitHub Desktop.

Revisions

  1. Zulko revised this gist Aug 22, 2014. 1 changed file with 10 additions and 13 deletions.
    23 changes: 10 additions & 13 deletions analyze.py
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,20 @@
    '''
    Rewrite with Twittcher ;)
    The result looks like this at every occurence:
    >>> ('Found one ! Current count:', {'teargas': 0, 'camera': 12, 'racist': 28,
    'rifle': 4, 'white': 66, 'milk': 1})
    Result (every 20 seconds):
    >>> Most common words: [('ferguson', 41), ('http', 28), ('protests', 9),
    ('missouri', 9), ('leave', 8), ('continue', 8),...]
    '''

    import re
    from collections import Counter
    from twittcher import SearchWatcher

    watched_words = ["racist", "white", "teargas", "rifle", "camera", "milk"]
    wordcounts = {word:0 for word in watched_words}
    counter = Counter()

    def action(tweet):
    found_at_least_one = False
    for word in watched_words:
    if word in tweet.text:
    wordcounts[word] += 1
    found_at_least_one = True
    if found_at_least_one:
    print( "Found one ! Current count:", wordcounts)
    words = [w for w in re.findall("\w+",tweet.text) if len(w)>3]
    counter.update([w.lower() for w in words])
    print ("Most common words: %s"%(counter.most_common(20)))

    bot = SearchWatcher("ferguson", action=action)
    bot.watch_every(20)
    bot.watch_every(20) # 20 seconds
  2. Zulko revised this gist Aug 21, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion analyze.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    '''
    Rewrite with Twittcher ;)
    The result looks like this at every occurence:
    >>> ('Found one ! Current count:', {'teargas': 0, 'camera': 12, 'racist': 28, 'rifle': 4, 'white': 66, 'milk': 1})
    >>> ('Found one ! Current count:', {'teargas': 0, 'camera': 12, 'racist': 28,
    'rifle': 4, 'white': 66, 'milk': 1})
    '''

    from twittcher import SearchWatcher
  3. Zulko revised this gist Aug 21, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions analyze.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    '''
    Rewrite with Twittcher ;)
    The result looks like this at every occurence:
    >>> ('Found one ! Current count:', {'teargas': 0, 'camera': 12, 'racist': 28, 'rifle': 4, 'white': 66, 'milk': 1})
    '''

    from twittcher import SearchWatcher
  4. Zulko revised this gist Aug 21, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion analyze.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    '''
    Rewrite with Twittcher ;) (Not tested)
    Rewrite with Twittcher ;)
    '''

    from twittcher import SearchWatcher

    watched_words = ["racist", "white", "teargas", "rifle", "camera", "milk"]
  5. Zulko revised this gist Aug 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion analyze.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    '''
    Rewrite with Twittcher ;) (Not tested)
    '''
    import twittcher
    from twittcher import SearchWatcher

    watched_words = ["racist", "white", "teargas", "rifle", "camera", "milk"]
    wordcounts = {word:0 for word in watched_words}
  6. Zulko revised this gist Aug 21, 2014. 1 changed file with 14 additions and 66 deletions.
    80 changes: 14 additions & 66 deletions analyze.py
    Original file line number Diff line number Diff line change
    @@ -1,71 +1,19 @@
    '''
    A script for analyzing twitter stats on Ferguson
    Rewrite with Twittcher ;) (Not tested)
    '''
    import tweepy
    import twittcher

    def get_api():
    '''
    Creates an instance of the tweepy OAuth class
    '''
    with open('config') as f:
    api_key = f.readline().strip()
    api_secret = f.readline().strip()
    access_token = f.readline().strip()
    access_token_secret = f.readline().strip()
    auth = tweepy.OAuthHandler(api_key, api_secret)
    auth.set_access_token(access_token, access_token_secret)
    return auth
    watched_words = ["racist", "white", "teargas", "rifle", "camera", "milk"]
    wordcounts = {word:0 for word in watched_words}

    def action(tweet):
    found_at_least_one = False
    for word in watched_words:
    if word in tweet.text:
    wordcounts[word] += 1
    found_at_least_one = True
    if found_at_least_one:
    print( "Found one ! Current count:", wordcounts)

    class CustomStreamListener(tweepy.StreamListener):
    '''
    Sub class of StreamListener to handle searching
    Ferguson tweets for various keywords
    '''
    count = 0
    racist = 0
    white = 0
    ftp = 0
    teargas = 0
    rifle = 0
    camera = 0
    milk = 0
    loot = 0

    def on_status(self, status):
    self.count += 1
    lowered = status.text.lower()
    if 'racist' in lowered:
    self.racist += 1
    if 'white' in lowered:
    self.white += 1
    if 'ftp' in lowered:
    self.ftp += 1
    if 'tear gas' in lowered:
    self.teargas += 1
    if 'rifle' in lowered:
    self.rifle += 1
    if 'camera' in lowered:
    self.camera += 1
    if 'milk' in lowered:
    self.milk += 1
    if 'loot' in lowered:
    self.loot += 1
    print '----------GOT A TWEET-----------'
    print 'total is %d' % self.count
    print 'number with \'racist\' is %d' % self.racist
    print 'number with \'white\' is %d' % self.white
    print 'number with \'ftp\' is %d' % self.ftp
    print 'number with \'tear gas\' is %d' % self.teargas
    print 'number with \'rifle\' is %d' % self.rifle
    print 'number with \'camera\' is %d' % self.camera
    print 'number with \'milk\' is %d' % self.milk
    print 'number with \'loot\' is %d' % self.loot
    print '--------------------------------'


    if __name__ == '__main__':
    auth = get_api()
    l = CustomStreamListener()
    streaming_api = tweepy.Stream(auth, l)
    streaming_api.filter(track=['Ferguson'])
    bot = SearchWatcher("ferguson", action=action)
    bot.watch_every(20)
  7. @howdydoody123 howdydoody123 revised this gist Aug 21, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions analyze.py
    Original file line number Diff line number Diff line change
    @@ -63,6 +63,7 @@ def on_status(self, status):
    print 'number with \'loot\' is %d' % self.loot
    print '--------------------------------'


    if __name__ == '__main__':
    auth = get_api()
    l = CustomStreamListener()
  8. @howdydoody123 howdydoody123 revised this gist Aug 21, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion analyze.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@

    def get_api():
    '''
    Creates an instance of the tweepy API class
    Creates an instance of the tweepy OAuth class
    '''
    with open('config') as f:
    api_key = f.readline().strip()
    @@ -18,6 +18,10 @@ def get_api():


    class CustomStreamListener(tweepy.StreamListener):
    '''
    Sub class of StreamListener to handle searching
    Ferguson tweets for various keywords
    '''
    count = 0
    racist = 0
    white = 0
  9. @howdydoody123 howdydoody123 revised this gist Aug 21, 2014. 1 changed file with 1 addition and 10 deletions.
    11 changes: 1 addition & 10 deletions analyze.py
    Original file line number Diff line number Diff line change
    @@ -61,15 +61,6 @@ def on_status(self, status):

    if __name__ == '__main__':
    auth = get_api()
    # api = tweepy.API(auth)
    l = CustomStreamListener()
    streaming_api = tweepy.Stream(auth, l)
    streaming_api.filter(track=['Ferguson'])
    # result = api.search('#Ferguson', count=100)
    # print len(result)
    # count = 0
    # for tweet in result:
    # lowered = tweet.text.lower()
    # if 'racism' in lowered:
    # count += 1
    # print count
    streaming_api.filter(track=['Ferguson'])
  10. @howdydoody123 howdydoody123 created this gist Aug 21, 2014.
    75 changes: 75 additions & 0 deletions analyze.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    '''
    A script for analyzing twitter stats on Ferguson
    '''
    import tweepy

    def get_api():
    '''
    Creates an instance of the tweepy API class
    '''
    with open('config') as f:
    api_key = f.readline().strip()
    api_secret = f.readline().strip()
    access_token = f.readline().strip()
    access_token_secret = f.readline().strip()
    auth = tweepy.OAuthHandler(api_key, api_secret)
    auth.set_access_token(access_token, access_token_secret)
    return auth


    class CustomStreamListener(tweepy.StreamListener):
    count = 0
    racist = 0
    white = 0
    ftp = 0
    teargas = 0
    rifle = 0
    camera = 0
    milk = 0
    loot = 0

    def on_status(self, status):
    self.count += 1
    lowered = status.text.lower()
    if 'racist' in lowered:
    self.racist += 1
    if 'white' in lowered:
    self.white += 1
    if 'ftp' in lowered:
    self.ftp += 1
    if 'tear gas' in lowered:
    self.teargas += 1
    if 'rifle' in lowered:
    self.rifle += 1
    if 'camera' in lowered:
    self.camera += 1
    if 'milk' in lowered:
    self.milk += 1
    if 'loot' in lowered:
    self.loot += 1
    print '----------GOT A TWEET-----------'
    print 'total is %d' % self.count
    print 'number with \'racist\' is %d' % self.racist
    print 'number with \'white\' is %d' % self.white
    print 'number with \'ftp\' is %d' % self.ftp
    print 'number with \'tear gas\' is %d' % self.teargas
    print 'number with \'rifle\' is %d' % self.rifle
    print 'number with \'camera\' is %d' % self.camera
    print 'number with \'milk\' is %d' % self.milk
    print 'number with \'loot\' is %d' % self.loot
    print '--------------------------------'

    if __name__ == '__main__':
    auth = get_api()
    # api = tweepy.API(auth)
    l = CustomStreamListener()
    streaming_api = tweepy.Stream(auth, l)
    streaming_api.filter(track=['Ferguson'])
    # result = api.search('#Ferguson', count=100)
    # print len(result)
    # count = 0
    # for tweet in result:
    # lowered = tweet.text.lower()
    # if 'racism' in lowered:
    # count += 1
    # print count