Skip to content

Instantly share code, notes, and snippets.

@eightBitter
Last active September 8, 2018 00:23
Show Gist options
  • Select an option

  • Save eightBitter/3da726a2510ef4f08651f1346cf20b9d to your computer and use it in GitHub Desktop.

Select an option

Save eightBitter/3da726a2510ef4f08651f1346cf20b9d to your computer and use it in GitHub Desktop.
Goal of this script is to find all places that have a Twitter trend available, tapping into Twitter's trends/available API feed, pulling out specific fields, and populating a pipe delimited file.
# goal of this script is to find all places that have a Twitter trend available, tapping into Twitter's trends/available API feed, pulling out specific fields, and populating a pipe delimited file.
# import libraries
import twitter
import re
import json
import fileinput
import collections
import os
import sys
# import needed object
from twitter import (Trend)
# build new class to access Twitter API's trends/available feed
class ExtendedApi(twitter.Api):
def TrendsAvailable(self):
url = "https://api.twitter.com/1.1/trends/available.json"
resp = self._RequestUrl(url, verb='GET')
data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))
return data
# insert Twitter API credentials
api = ExtendedApi(consumer_key=[],
consumer_secret=[],
access_token_key=[],
access_token_secret=[])
# pull out response from ExtendedApi
trends = api.TrendsAvailable()
# set up output file
output = open("trends.txt", "w")
# get the data in a useable data type
jsonDumps = json.dumps({'trends': trends})
jsonLoads = json.loads(jsonDumps)
# loop JSON objects
for jsonLoad in jsonLoads['trends']:
# write targeted output to txt file as pipe delimited
output.write(jsonLoad['name'] + "|" + jsonLoad['country'] + "|" + str(jsonLoad['woeid']) + "|" + "\n")
output.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment