So I was thinking to use some request to send etc 20 request to a site and the first one to serve a value from one of those site should use that value and continue my code basically. So whenever there is a value inside a String or whatever then just continue the code. However I got stuck. What I have done so far is that I have been able to send only one request:
my_key = load["MyKey"] # My own key for website.
website_key = webkey
url = webUrl
Myclient = MyClient(my_key)
task = Task(url, website_key)
values = client.createTask(task)
value.join()
value = values.get_response()
print(value)
So basically with .join is searches for the value from the website and then return it as a get_response whenever its ready. However when I do this, It will only do one and then end.
And what I want to do is basically to send like etc 25 of them and then whenever hits the value first then end the other one and continue or end the program pretty much.
What would be the best solution for that?
Related
I would like to pick up a specific keyword from a discord message as quickly as possible in Python, I'm currently using a request but the issue is that it takes too much time to actualize and grab the new message (it takes 200-500ms to receive messages), I am sure there is a better way of doing it.
def retrieve_messages(channelid):
while True:
##DISCORD
headers = {'authorization':""}
r= requests.get('https://discord.com/api/v9/channels/xxxxxxxxxxxxxxx/messages',headers=headers)
jsonn = json.loads(r.text)
for value in jsonn:
s1=str(value['content'])
s2=(re.findall('code:(0x......................)', s1))
if s2 !=[]:
print(s2)
retrieve_messages('')
According to the reference, the default number of messages returned from the endpoint is 50 (Link: https://discord.com/developers/docs/resources/channel#get-channel-messages). Using the limit-parameter it should be possible to maybe only get 1 or 5, which should limit the amount of time it takes to retrieve the messages and the time it takes to loop through them.
I'm reading data from a json file using requests.. and wish to store those values in an excel file. The issue I'm running into is I don't know the interval at which this json file is being updated, so it's hard to make my scheduler perfect.. I end up with duplicate values. I can't seem to find any libraries that could check for me.
In this scenario, I end up with a duplicate at 1:10 and wish to not add it since it is the same as the one at 1:05. In some instances, there may be more than one duplicate at a time and I wish to check this before it evens gets added rather than added and then deleted.
Is there a simpler way to approach this problem?
Example:
timestamp stock buy sell
1:05 1 10 10
1:10 1 10 10
1:15 1 20 16
def do_this():
previous_data = []
try:
response = requests.get(JSON_LINK)
previous_data = response.content
except Exception:
print('Failed in main')
if(previous_data == response.content):
print('same data')
else:
print('new data')
I realized this isn't going to work at previous_data = [] will always end up taking in whatever the current response is, not the previous response data.
You could try searching the existing data for the same record you have. If the record exists then its an update, if not its a delete.
You could also cache the previous request. When a new request comes in, compare it to the cache and remove duplicates. This approach only works if you care about previous and next requests.
"I don't know the interval at which this json file is being updated" - you can add a custom header to requests with the time the request was store this time as 'latest'. When another request comes in, check the header time value, if its older than latest, throw it away.
Considering that JSON can have the same values at different times.
There are mostly two way to check for equality of objects- one is by char to char checking and other is by matching the hash values.
So, one approach will be to create a HashSet. Whenever a new response comes you compute the hash and check if this hash exists or not. If exists, then simply discard the response, if it doesn't exist you store the hash value in the hashSet and response value in your excel.
NOTE: Hash matching consumes less time than string matching.
I am trying to iterate through some json data, but the information is found on several pages. I don't have a problem working through the first page, however it will just skip over the next set. The weird thing is, it will execute fine while in debug mode. I'm guessing its a timing issue while working with the json loads, but I tried putting sleep timers around that code and the issue persisted.
url = apipath + query + apikey
response = requests.get(url)
data = json.loads(response.text)
for x in data["results"]:
nameList.append(x["name"])
latList.append(x["geometry"]["location"]["lat"])
lonList.append(x["geometry"]["location"]["lng"])
pagetoken = "pagetoken=" + data["next_page_token"]
url = apipath + pagetoken + apikey
response = requests.get(url)
data = json.loads(response.text)
for x in data["results"]:
nameList.append(x["name"])
latList.append(x["geometry"]["location"]["lat"])
lonList.append(x["geometry"]["location"]["lng"])
I would venture to guess that data["results"] equates to a None value and therefore calling for x in None: would result in the program skipping your for loop. Have you tried putting a print above the for loop? Perhaps try print(data["results"]) before going into your loop to ensure the data you want exists. If that returns None then maybe try just print(data) and see what the program is reading.
Well it did end up being a timing issue. I placed a 2 second timer before the second request and it now will load the data just fine. I guess Python just couldn't keep up.
I'm building a text game and need to store 2 things in a single variable: a string, and a request for input. Note that I don't mean to store the output of the request - I mean that if the variable is called, both the string and the request itself are printed, after which the user answers the request.
raw_input("Do you do X or Y?") therefore doesn't work for me, because I need to store the request before I deploy it.
Some background about my approach:
Everything's stored in a dictionary, where the keys are the user's current location and the values are possible choices:
dict = {location1: (location2, location3), location2: (location1, location4)...}
So, I'll print location1, which will simultaneously print the string that describes that location, and make a request for input. The input triggers the program to print the next appropriate location, and the program keeps on going.
I'm trying to work out a recursive function that does this.
For each location, the request for input is worded differently, which is why I don't just build the request into my recursive function.
Sidenote: if anyone has any other suggestions/different approaches I should use instead, please share those too!
For each location, the request for input is worded differently,
Simply create another dictionary for input request corresponding to each location. For ex:
requests_dict = {
'location1': 'Please enter xxx: ',
'location2': 'Do you do X or Y: ', # and so on
}
Then use that dict to print the request.
user_input = raw_input(requests_dict['location2'])
Of course, you would like to make the last code based on some logic so that which one of that dicts is called, but I think you get the idea.
Update:
responses_dict = {}
user_input = raw_input(requests_dict['location2'])
responses_dict[user_input] = 'You are in %s' % user_input
I'm writing a little python app to fetch the followers of a given tumblr, and I think I may have found a bug in the paging logic.
The tumblr I am testing with has 593 followers and I know the API is block limited to 20 per call. After successful authentication, the fetch logic looks like this:
offset = 0
while True:
response = client.followers(blog, limit=20, offset=offset)
bunch = len(response["users"])
if bunch == 0:
break
j = 0
while j < bunch:
print response["users"][j]["name"]
j = j + 1
offset += bunch
What I observe is that on the third call into the API with offset=40, the first name returned on the list is one I saw in the previous group. It's actually the 38th name. This behavior (seeing one or more names I've seen before) repeats randomly from that point on, though not in every call to the API. Some calls give me a fresh 20 names. It's repeatable across multiple test runs. The sequence I see them in is the same as on Tumblr's site, I just see many of them twice.
An interesting coincidence is that the total number of of non-unique followers returned is the same as what the "Followers" count indicates on the blog itself (593). But only 516 of them are unique.
For what it's worth, running the query on Tumblr's console page returns the same results regardless of the language I choose, so I'm not inclined to think this is a bug in the PyTumblr client, but something lower, at the API level.
Any ideas?