Skip to content

Instantly share code, notes, and snippets.

@ingoclaro
Last active August 1, 2016 23:04
Show Gist options
  • Save ingoclaro/bc8f4acf4fa61e438bc94de11cdc96b3 to your computer and use it in GitHub Desktop.
Save ingoclaro/bc8f4acf4fa61e438bc94de11cdc96b3 to your computer and use it in GitHub Desktop.
twitter client
# run
# pip install requests
# to install dependency
# If you need some help debugging your regexes,
# check your regex here: https://regex101.com/
# Documentation for regex's are here: https://docs.python.org/2/library/re.html
# Hint: you can use re.findall(p, text) to get all matches
import requests
import json
import re
from collections import Counter
def getTweets():
url = 'https://api.twitter.com/1.1/search/tweets.json?q=place%3A300bcc6e23a88361%20filter%3Asafe%20%3A%29&count=100'
r = requests.get(url, headers={'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAN%2FTwAAAAAAAsSph2PC2NVjZgP0A%2FWjjR2ajCuQ%3DydfRjZjxYmiEPynaEslzjcdvo6uJ39rUl9ZoP92mH8Fskh5wha'})
j = json.loads(r.text)
return j
# get complete response
print(getTweets())
# match against 1st tweet
p = re.compile(ur'(#\w+)')
test_str = getTweets()["statuses"][0]["text"]
print(re.findall(p, test_str))
def getHashtags(tweets):
# regex to get hashtags
p = re.compile(ur'#(\w+)')
results = []
for status in tweets["statuses"]:
matches = re.findall(p, status["text"])
results.extend(matches)
return results
tweets = getTweets()
results = getHashtags(tweets)
c = Counter(results)
print(c.most_common())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment