michael-lazar / rtv

Browse Reddit from your terminal
MIT License
4.64k stars 274 forks source link

Using "preview.redd.it" instead of "i.redd.it" #653

Closed isamert closed 5 years ago

isamert commented 5 years ago

I want to use preview.redd.it images instead of i.redd.it links. I tried to rewrite the image link that rtv hands over to my script but that does not work, I'm getting Error 403 Forbidden error. Here is a random i.redd.it image:

https://i.redd.it/fja0oxrr90h21.jpg

And here is the preview.redd.it counterpart:

https://preview.redd.it/fja0oxrr90h21.jpg?width=640&crop=smart&auto=webp&s=SOME_SESSION_HASH_I_ASSUME

So rewriting the url https://i.redd.it/fja0oxrr90h21.jpg into https://preview.redd.it/fja0oxrr90h21.jpg does not work as preview.redd.it requires some extra parameters. (Actually the link won't work besides the original parameters, even changing width parameter gives an error.) That means previews needs to be requested explicitly, as far as I understand. Is there a way to accomplish this in rtv or can you add it as an option?

Rationale: I'm living in Turkey and i.redd.it is blocked but preview.redd.it works without problems (or at least it works with simple DNS change). More interestingly, after loading an image trough preview.redd.it I can also open it in full resolution in i.redd.it in same browser session. This may seem like a very niche use-case but I believe it will help a lot of people.

michael-lazar commented 5 years ago

Hi,

I sympathise with your problem, but I doubt I will find the time to work on this personally because it's such a niche request. Here's how I think you could go about it if you want to get your hands dirty:

https://github.com/michael-lazar/rtv/blob/master/rtv/content.py#L241

Replace

data['url_full'] = sub.url

with

try:
    data['url_full'] = sub.preview['images'][0]['source']['url']
except (AttributeError, ValueError, TypeError):
    data['url_full'] = sub.url

Of course, the devil is in the details and you would still need to properly handle videos, links to other media types, links to external images, etc.

isamert commented 5 years ago

Thank you! As I mentioned, I only need to replace "i.redd.it" with previews so I just checked for them instead of replacing all the links with previews and it seems to work for me. I don't expect this feature in upstream but at least if anyone needs something like this, presence of this issue may help them. Thanks again for this great project!