'why I can't download pics from facebook at full size using python
For my CS exam I have written a python code to download images from a user account using Graph APIs but I have faced a problem I am not able to fix:
When I open the pic in the browser to download it it is located at the following URL:
https://scontent.fbzo1-2.fna.fbcdn.net/v/t39.30808-6/276154675_133885455881794_6427257905137797718_n.jpg?_nc_cat=108&ccb=1-5&_nc_sid=9267fe&_nc_ohc=YLBzlwDAyFMAX-xSuA2&_nc_ht=scontent.fbzo1-2.fna&oh=00_AT8Id6JhclEoVKJSWjxj2QPFxJJV4AiyuLQRfaEP02-81A&oe=625D705A
but when I open it from a python application it tries to download the pic from a different URL:
https://scontent.fbzo1-2.fna.fbcdn.net/v/t39.30808-6/276154675_133885455881794_6427257905137797718_n.jpg?stp=dst-jpg_s720x720&_nc_cat=108&ccb=1-5&_nc_sid=2d5d41&_nc_ohc=YLBzlwDAyFMAX-xSuA2&_nc_ht=scontent.fbzo1-2.fna&edm=AJfPMC4EAAAA&oh=00_AT8deCIWQLP87HEgyDOa1GIAcGBb9L7bElubSoHzidxvvQ&oe=625D705A
and it has a different resolution from the original!
I will post here some code, if you need the full code, I can add it. this is the downloading function:
# retrieve the post id to download the image from fb posst
# download and save image from permalink url
def download_and_save_img(img_url, img_name):
# retrieve only the img name / the file extention seams to generate an error -> setting it manualy
last_slash = img_name.rfind('/')
file_ext = img_name.rfind('.')
img_name = img_name[last_slash+1:file_ext]
# download the image with a GET request and write it to an img file
# print(img_url)
img_response = requests.get(img_url)
image = open("fl-tw-fb/" + img_name + '.jpg', "wb")
image.write(img_response.content)
image.close()
And here is the call
response = get_img_link_from_fb_postID(post_id['post_id'])
response_json = json.loads(response)
print(response_json)
img_url = response_json['attachments']['data'][0]['media']['image']['src']
# finaly download the img from fb and save it
download_and_save_img(img_url, line)
when printing the response_json I get:
{
"attachments":{
"data":[
{
"media":{
"image":{
"height":405,
"src":"https://scontent.fbzo1-1.fna.fbcdn.net/v/t39.30808-6/278102009_133887052548301_9150026685369816171_n.jpg?stp=dst-jpg_s720x720&_nc_cat=100&ccb=1-5&_nc_sid=2d5d41&_nc_ohc=WF5mnEaaoKcAX9GxsvH&_nc_ht=scontent.fbzo1-1.fna&edm=AJfPMC4EAAAA&oh=00_AT9T6TUvuRni6TEcLz7oteYsH8E_6yko2FRSEXTH9MPppQ&oe=625BDBB7",
"width":720
}
}
}
]
},
"id":"102956292308044_133887055881634"
}
Any idea?
Edit: adding code for @WizKid:
# retrieve the image link present in a given post_id
def get_img_link_from_fb_postID(post_id):
# (extended) access token
at = 'xxx' # removed
# build the url
url = 'https://graph.facebook.com/v13.0/' + post_id + '?fields=attachments%7Bmedia%7D&access_token=' + at
try:
response = requests.get(url)
except Exception as e:
print(f"Error in get img link from post_id: {e}")
return response.content
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
