How to handle strings in headers using requests Python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have REST API for which I'm trying to fetch data. The API is working perfectly if I use value in string format, but the API request doesn't work if a placeholder is used instead of value with string data type or concatenated string as in example below:
import requests
import pandas as pd
import time
from datetime import date
url = 'http://abc.io/api/v1/modules/get-sale-manager'
payload = {'brand_id': 'sdfghjkl', 'from_timestamp': '1119405211','to_timestamp': '111940511'} #here value's are used
params=payload
headers = {'access_token': 'xxxxxxxxx','secret_key':'xxxxxxx'}
response = requests.get(url, headers=headers,params=payload)
response.json()
Instead I want to use placeholder and supply calculated values as following:
payload = {'brand_id': str(brand_id), 'from_timestamp': str(from_timestamp),'to_timestamp':'to_timestamp'} #here value's are used
And I would like this also:
payload = {'brand_id': "'"+brand_id+"'", 'from_timestamp': "'"+from_timestamp+"'",'to_timestamp': "'"+timestamp_too+"'"}
But none of these two methods work. I get 500 status code, but I'm sure there's no error at server as when the values are supplied directly.

I think the problem is in your datetime serialization. You should convert datetime to timestamp like below
from_time_str = str(int(time.mktime(from_timestamp.timetuple())))
to_time_str = str(int(time.mktime(to_timestamp.timetuple())))
payload = {
'brand_id': str(brand_id),
'from_timestamp': from_time_str,
'to_timestamp': from_time_str
}
response = requests.get(url, headers=headers,params=payload)

Related

What is the best way to fetch all data (posts and their comments) from Reddit? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a requirement to analyse all the comments about a subreddit, (e.g. r/dogs, say from 2015 onward) using the Python Reddit API Wrapper (PRAW). I'd like the data to be stored in a JSON.
I have found a way to get all of the top, new, hot, etc. submissions as a JSON using PRAW:
new_submissions = reddit.subreddit("dogs").new()
top_submissions = reddit.subreddit("dogs").top("all")
However, these are only the submissions for the subreddit, and do not include comments. How can I get the comments for these posts as well?
You can use the Python Pushshift.io API Wrapper (PSAW) to get all the most recent submissions and comments from a specific subreddit, and can even do more complex queries (such as searching for specific text inside a comment). The docs are available here.
For example, you can use the get_submissions() function to get the top 1000 submissions from r/dogs from 2015:
import datetime as dt
import praw
from psaw import PushshiftAPI
r = praw.Reddit(...)
api = PushshiftAPI(r)
start_epoch=int(dt.datetime(2015, 1, 1).timestamp()) # Could be any date
submissions_generator = api.search_submissions(after=start_epoch, subreddit='dogs', limit=1000) # Returns a generator object
submissions = list(submissions_generator) # You can then use this, store it in mongoDB, etc.
Alternatively, to get the first 1000 comments from r/dogs in 2015, you can use the search_comments() function:
start_epoch=int(dt.datetime(2015, 1, 1).timestamp()) # Could be any date
comments_generator = api.search_comments(after=start_epoch, subreddit='dogs', limit=1000) # Returns a generator object
comments = list(comments_generator)
As you can see, PSAW still uses PRAW, and so returns PRAW objects for submissions and comments, which may be handy.

Check if board, list, or card exists in trello [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is there a way to check if a board, list, or card exists in Trello? Would like to create a python script that will display the list of the mentioned components if the name already exists.
I checked the API it seems there is no query function.
Hoping for your inputs. Thanks in advance.
Got it now. I was able to use the search API from trello. https://developer.atlassian.com/cloud/trello/rest/api-group-search/#api-search-get
Sharing the code:
def search_board(board_name):
url = "https://api.trello.com/1/search"
querystring = {"query": board_name, "key": key, "token": token}
response = requests.request("GET", url, params=querystring)
print(response)
board_id = ""
print(response.json())
if response.json()["boards"]:
board_id = response.json()["boards"][0]["id"]
print(board_id)
else:
return board_id

Sending a variable in python payload - http client [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to send a variable in python using a payload created by postman, I tried using {{}} and it
didn't work for me, the place where I need the variable is indicated as VARIABLE_SPOT, please let me know what I did wrong in order to send a variable and not a text.
payload = "{\r\n \"some_key\": \"VARIABLE_SPOT\",\r\n }
Just Declare Your Payload Like Below
payload = {"some_key": VARIABLE_SPOT}
if you want to stringfy it run json.dumps(payload)
You can create a dict and covert it to string:
import json
some_variable = 'some_variable'
payload_dict = {"some_key": some_variable}
payload_text = json.dumps(payload_dict)
payload_text is your payload now.
Result:
> print(payload_text)
{"some_key": "some_variable"}
> print(type(payload_text))
<class 'str'>

Python youtube api status.rejectionReason [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've looked through the documentation here: https://developers.google.com/youtube/v3/docs/videos
But am unsure how to access status.rejectionReason with python. I've been using youtube-uploader for my uploading, and I dont believe there is any commands to return the reason a video was rejected. The ideal scenario would be to get a list of all my videos, check which ones have been rejected, then return the links of the ones that have been rejected.
From what I can see the rejectionReason is within a JSON "videos resource" format. You can access this with Python's built-in JSON library:
from json import load
with open('video.json') as file: # Opens the JSON file and assigns it to the variable 'file' within the loop
data = load(f) # Loads the file into a dictionary which you can access with key:value pairs
The JSON file provided as a sample on the site follows this format for the rejectionReason:
"status": {
"uploadStatus": string,
"failureReason": string,
"rejectionReason": string,
"privacyStatus": string,
"publishAt": datetime,
"license": string,
"embeddable": boolean,
"publicStatsViewable": boolean
}
So your final script would look like this I believe:
from json import *
def get_rejection_reason(file):
with open(file) as f:
data = load(f)
return data["status"]["rejectionReason"]
get_rejection_reason("video.json")

How do I make a REST query in python with a comparison statement? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How do I make a REST query in python with a comparison statement? Like (in quasi code):
result = foo where bar > 10
I want to make a rest api query from python 2.7 using requests. What I want is to get all articles that have been updated during the last 24 hours.
In javascript it looks like this and it works great:
http://myDatabase.domain.io/api/v1/article/q={"update_date":{"$gt":"2018-08-27 13:44"}}
I just can't recreate this with python. Does anyone know how?
Assuming
that's actually ?q=, i.e. a query string
and the query should be JSON (it looks like it)
and the endpoint returns JSON:
import requests, json
query = json.dumps(
{"update_date": {"$gt": "2018-08-27 13:44"}}
)
resp = requests.get(
url="http://myDatabase.domain.io/api/v1/article/",
params={"q": query},
)
resp.raise_for_status()
data = resp.json()
print(data)

Categories