retrieve Soundcloud user resource from Soundcloud track resource - python

I am trying to 'trace back' from a given Soundcloud track resource object to the Soundcloud user resource object that created it. Given my understanding of the Soundcloud API this is what I have done.
1st: Retrieve a specific track
track = client.get('/tracks', title="White Lies (feat. Jenni Potts)",
limit=1)
This should return a collection of one track (by Odesza).
2nd: Gather info about tracks user from track resource
white_lies_perma = track[0].user["permalink"]
white_lies_uID = track[0].user["id"]
3rd: Get the user (hopefully) using the above filters
users = client.get('/users',
id=white_lies_uID,
permalink=white_lies_perma)
This should return 1 user, however the length of the resource collection is 50 (the API default). This is strange.
Printing the values:
for user in users:
print(user.id)
79582
2684450
3765692
9918957
12470953
etc...
This should just be a collection of length 1, containing a resource with id 18604897.
Furthermore, this code should reasonably return "Oklahoma City" twenty times over. However when run it gives me "Cape St Francis", "Augusta", "London U.K".
client = soundcloud.Client(client_id='xxxxx')
okcUsers = client.get('/users',city="Oklahoma City",limit=20)
for okcUser in okcUsers:
print okcUser.city
Is something wrong with Soundcloud's API?

Not sure how you implemented those filters in your 3rd call, but I think you should get the information you needed if you do
users = client.get('/users/%d' % white_lies_uID)
Since you do not need to do a search but just call the correct /users/ endpoint. To see which endpoints are available for a specific user go to https://developers.soundcloud.com/docs/api/reference#users and scroll down to below the list of fields a user contains. There you see a list of endpoints that can be called once you have a user id.

Related

Getting information from one users collection when cycling through messages collection - Firestore

My database is structured with a users collection and a messages collection (seen below). There will only be 1-2-1 messaging in the app (no chat rooms).The messages collection's documents are named as user1:user2.
I'm trying to create a page in my app where a user can see all the users they have chats with and then can click on each of those chats and open up the message chain with that user. I'm confident I know how to create the second part of this (the message chain) but I'm not sure how to pull back all of the current chats a user has.
So far I've got
def load_chats(self):
messages = self.my_firestore.db.collection(u'messages').stream()
#self.users = self.my_firestore.db.collection(u'users').stream()
for doc in messages:
if self.local_id in doc.id: #This is checking if "user1" is in "user1:user2", for example
chat_messages.add_widget(ChatButton) #Adds a new button each time it finds "user1" in the doc.id within messages
In this for loop, when I'm creating the button for the chats I need information from the users collection so I can display the name of the person the chat is with.
However, I'm not sure how to do this - beyond putting a for loop within for doc in messages which cycles through self.users and returns the user information if it matches "user2". This would create a huge number of reads so I'm not even entertaining my crazy idea!
Any help would be much appreciated, let me know if it's not clear.
You could add a Participants array field to your chat groups:
data = {
'GroupId': 'userOneID:userTwoID',
'Participants': ['userOneID', 'userTwoID']
}
Then you could query for chat groups where a user is either userOne or userTwo in a chat group:
chatGroups = self.my_firestore.db.collection(u'messages') \
.where('Participants', 'array_contains', 'userID') \
.stream()

Twitter bot - Replying to specific users using Tweepy

I'm trying to write my first twitter bot, and what I specifically want it to do is to reply when a specific user tweets something. Instead of searching for every user that mentions the word 'taco' I want to only search for when a small list of people mention 'taco'. For example, my bot only looks for tweets from Bob and Sue that mention 'taco', but not tweets from Fred. I'm not finding what I need.
From the Tweepy documentation, you should probably use the method API.user_timeline to read a specific user tweets.
API.user_timeline([user_id/screen_name])
Returns the 20 most recent statuses posted from the
authenticating user or the user specified.
Parameters:
user_id – Specifies the ID of the user
screen_name – Specifies the screen name of the user
You could accomplish this through the user timeline API endpoint; however, depending on how many terms and users you want to track, you'd have to worry about rate limits (the user timeline endpoint rate limit is pretty high, 300/app auth/15 mins, 150/user auth/15 mins = 450/15 mins), and also the fact that you'd have to call the endpoint manually at some time interval.
Another way to do this is by using the streaming API endpoint, specifically the user stream. Follow everyone you want to reply to, and then create some rules for specific phrases. As followed users post tweets, they should stream to your user stream endpoint. You'd just have to have a listener running, with some logic for tracked users/phrases.
track = {
'taco': ['Bob', 'Sue'],
'salsa': ['John', 'Steve'],
'guacamole': ['Mary', 'Fred']
}
You'd subclass Tweepy's StreamListener:
class TacoListener(tweepy.StreamListener):
def on_status(self, status):
# Note, I rolled my own Twitter API wrapper, and just had a glance at the Tweepy docs, so some of this syntax might be incorrect, change it as required
# I think Tweepy has syntax like: status.text; I'll just refer to the fields as they appear in the Twitter JSON payload
for k, v in track.items():
if k in status.get('text') and status.get('screen_name') in v:
tweet = ""
for name in v:
tweet += "#" + name
tweet += " are talking about " + k + "! Yum."
api.update_status(status=tweet)

Trello API ~ simply getting the contents of a list?

I am trying to get the contents of a list on I a board I created to understand some things about my work flow.
The API seems quite complex, and I have been at it for hours. I have an API key, as well as a Secret key.
I tried the following from their docs: https://api.trello.com/1/lists/4eea4ffc91e31d174600004a/cards?key=[application_key]&token=[optional_auth_token]
However, I am not sure where these letters/numbers are coming from: 4eea4ffc91e31d174600004a.
I read the following page: https://developers.trello.com/apis (which gave me the link to the url above), but there is no info on how to get 4eea4ffc91e31d174600004a.
I simply want to visit a url that gives me json or something of that vein, with all the contents of a list (e.g. the cards + their names). Then I can visit that link programmatically and do my analysis.
Edit: Using the trello developer sandbox: https://developers.trello.com/sandbox/ I found the id of a list, which I substituted into 4eea4ffc91e31d174600004a, but now I get the following: Taco says “invalid token”, but what does he know? He's just a dog.
I used the Secret key as the token, but I guess that's not the token. So the question boils down to how can I get a token?
Thanks
So the full answer is:
To get the cards in a list, three things are required:
API Key
List ID
Token
(Secret key isn't needed anywhere)
To get the List ID, the simplest way to do that is to use the developer sandbox https://developers.trello.com/sandbox/ then hit 'get lists' + 'execute' and obtain the id of the list of interest.
Note if you have multiple boards, you will need to specify the board id here: Trello.get('/boards/[board_id]/lists', success, error);. You can get the board id by hitting 'get boards' + 'execute', then looking for the id of interest.
To get a token, you need to go here: https://trello.com/1/connect?key=[application_key]&name=MyApp&response_type=token
Then you can make the call:
https://api.trello.com/1/lists/[list_id]/cards?key=[application_key]&token=[optional_auth_token]
I recently worked on Trello data using its RESTful API and using Python's py-trello. As the question is about accessing Trello list and its cards, will limit my solution to list access using Trello RESTful API and py-trello.
1. Using Trello RESTful API:
Following you need to form your URI to access the desired list and its cards JSON
i) API Key: You can get the key from https://trello.com/app-key
ii) Token: On the above page itself, you'll find link to the Token generation. Posting the snap shot below for convenience-
iii) list id: you can find this in the board JSON
Using these 3 items, below is how the RESTful URI to access a list is going to look like:
list_json_url = "https://api.trello.com/1/lists/replace_this_with_ur_list_id?cards=all&key=replace_this_with_ur_api_key&token=replace_this_with_your_token
Below is how i'm loading above list JSON in my Python program :
with urllib.request.urlopen(list_json_url) as fj:
data = json.load(fj)
2. Using py-trello:
There can be better ways to do this using py-trello's filtering features, i have just taken this from my entire multi-boards analysis code and added a condition to match a list id:
from trello import TrelloClient
API_KEY = "XXXXXXX"
API_TOKEN = "XXXXXXXXXX"
client = TrelloClient(api_key=API_KEY, token=API_TOKEN)
for board in client.list_boards():
for l in board.list_lists():
if l.id = "ur_list_id":
#do your list analysis here
That is the list id which you can get by an API call to the boards
You'll find details here.
https://developers.trello.com/apis#boards
Go to link: https://trello.com/app-key
Click in TOKEN
Click in Allow
Copy your token
DONE

Twython getting tweets from user

I am using Twython to get a stream of tweets. I used this tutorial, expect that I am not using GPIO.
My code is the following:
import time
from twython import TwythonStreamer
TERMS='#stackoverflow'
APP_KEY='MY APP KEY'
APP_SECRET='MY APP SECRET'
OAUTH_TOKEN='MY OATH TOKEN'
OAUTH_TOKEN_SECRET='MY OATH TOKEN SECRET'
class BlinkyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
print data['text'].encode('utf-8')
try:
stream = BlinkyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(track=TERMS)
except KeyboardInterrupt
That outputs a stream of all tweets containing #stackoverflow. But I want to output the tweet if it is from a certain user, e.g. #StackStatus.
I am running this on a Raspberry Pi.
How would I do that? Any help is appreciated!
Edit: if there is another, other or easier, way to execute some script when a new tweet is placed by some user, please let me know, this would solve my question as well!
The 'follow' parameter does not work as stated above by teknoboy. Correct usage is with the user's ID, not their screen name. You can get user IDs using http://gettwitterid.com/.
The third parameter available is Location - you can use 1, 2 or 3 of then as desired. They become linked with "OR", not 'AND'.
Example Usage:
SearchTerm = 'abracadabra' # If spaces are included, they are 'OR', ie finds tweets with any one of the words, not the whole string.
Tweeter = '25073877' # This is Donald Trump, finds tweets from him or mentioning him
Place = '"47.405,-177.296,1mi"' # Sent from within 1 mile of Lat, Long
stream.statuses.filter(track=SearchTerm, follow=Tweeter, location=Place)
you should supply the filter with the follow parameter to stream specific users' tweets.
if you wish to only follow one user, you can define
FOLLOW='StackStatus'
and change the appropriate line to
stream.statuses.filter(track=TERMS, follow=FOLLOW)
if you wish to see all the user's tweets, regardless of keyword, you can omit the track parameter:
stream.statuses.filter(follow=FOLLOW)

Retrieving all "Yahoo Answers" Questions that contain a certain word . Issue with rate limiting

So, I'm trying to compile a database of all the questions in Yahoo Answers that contain a certain word. I am currently doing this with the following script I wrote, using Pynswers wrapper class for calling Yahoo API
from Answers import Answers
app = Answers()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')
app.appid = '...'
questions = app.questionSearch({'query':'tornado',})
#Write all column headings
sheet.write(0,0, 'Question')
sheet.write(0,1,'Answer')
sheet.write(0,2, 'Date')
sheet.write(0,3,'Number of Answers')
for i, value in enumerate(questions):
content = value['Content'].strip()
chosenAnswer = value['ChosenAnswer'].strip()
date = value['Date'].strip()
numAnswers = value['NumAnswers'].strip()
#Write values into respect columns, (row, column)
sheet.write(i+1,0,content)
sheet.write(i+1,1,chosenAnswer)
sheet.write(i+1,2,date)
sheet.write(i+1,3,numAnswers)
wbk.save('C://test.xls')
The issue is that I only get about 10 responses from this query, and I can't figure out a way to expand the range of questions that I get. Any ideas?
Pynswers appears to be a very loose wrapper around the Yahoo API itself. The API documentation shows to use a "start" and "results" field in the request:
So, perhaps you can do the following:
first_50 = app.questionSearch({'query':'tornado', 'start' : 0, 'results' : 50})
next_50 = app.questionSearch({'query':'tornado', 'start' : 50, 'results' : 50})
Edit
Also, in regard to "rate limiting", Yahoo states in regard to their API (this section taken on March 7, 2013):
How many times can I call YQL in a minute/hour/day?
Rate limits in YQL
are based on your authentication. If you use IP-based authentication,
then you are limited to 2,000 calls/hour/IP to the public YQL Web
service URL (/v1/public/) or 20,000 calls/hour/IP to the private YQL
Web service URL (/v1/yql/) that requires OAuth authorization. See the
YQL Web Service URLs for the public and private URLs. Applications
(identified by an Access Key) are limited to 100,000 calls/day/key*.
However, in order to make sure the service is available for everyone
we ask that you don't call YQL more than 0.2 times/second or 1,000
times/hour for IP authenticated users and 2.7 times/second or 10,000
times/hour.
*Please don't create multiple keys to 'avoid' rate limits. If you would like us to increase your limit please contact us with details of
your project and we'll do our best to accommodate you.
Obviously, you'll need to be careful with your code to ensure that you're getting the information you need without exceeding the rate limit. So, getting "all" the answers might not be practical.

Categories