Skip to content

Instantly share code, notes, and snippets.

@linxlunx
Created July 18, 2015 04:35
Show Gist options
  • Save linxlunx/3a4e37dd441aa5dabdf6 to your computer and use it in GitHub Desktop.
Save linxlunx/3a4e37dd441aa5dabdf6 to your computer and use it in GitHub Desktop.
#LebaranBabes
#!/usr/bin/env python
import urllib
import urllib2
import json
import oauth2 as oauth
class Lebaran:
def __init__(self):
# twitter access key
self.consumer_key = ''
self.consumer_secret = ''
self.access_key = ''
self.access_secret = ''
# alchemi api key
self.alchemi_key = ''
def twitter_search(self):
consumer = oauth.Consumer(key=self.consumer_key, secret=self.consumer_secret)
token = oauth.Token(key=self.access_key, secret=self.access_secret)
client = oauth.Client(consumer, token)
header, response = client.request('https://api.twitter.com/1.1/search/tweets.json?q=lebaranbabes&include_entities=true&count=50',method="GET")
return response
def face_detect(self, media_url):
encoded_url = urllib.quote_plus(media_url)
face_url = 'http://access.alchemyapi.com/calls/url/URLGetRankedImageFaceTags?apikey={0}&outputMode=json&url={1}'.format(self.alchemi_key, encoded_url)
img_data = urllib2.urlopen(face_url).read()
img_json = json.loads(img_data)
if img_json['status'] == 'OK':
if len(img_json['imageFaces']) > 0:
age_range = img_json['imageFaces'][0]['age']['ageRange']
gender = img_json['imageFaces'][0]['gender']['gender']
return_resp = {
'age' : age_range,
'gender' : gender,
}
else:
return_resp = False
else:
return_resp = False
return return_resp
if __name__ == '__main__':
babes = Lebaran()
data = babes.twitter_search()
convert = json.loads(data)
for i in convert['statuses']:
if 'media' in i['entities']:
img_url = i['entities']['media'][0]['media_url']
face = babes.face_detect(img_url)
if face:
print 'gender: {0}| age range: {1}'.format(face['gender'], face['age'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment