samsymons / RedditKit.rb

[Deprecated] A Ruby wrapper for the reddit API
https://redditkit.com/
MIT License
152 stars 26 forks source link

Can't request the "SEEN IT" section with #search + link. #21

Open siers opened 10 years ago

siers commented 10 years ago

So, if you put a link into the reddit's search box, then it will show you where has that link already been submitted. It will redirect to /submit?url=your_url. Now, RedditKit doesn't do redirects.

With this code, it does:

module RedditKit
  class Client
    def middleware
      @middleware ||= Faraday::Builder.new do |builder|
        builder.use Faraday::Request::UrlEncoded
        builder.use RedditKit::Response::RaiseError
        builder.use RedditKit::Response::ParseJSON
        # *These two lines below allow the redirects to happen.*
        builder.use FaradayMiddleware::FollowRedirects
        builder.adapter :net_http
      end
    end
  end
end

And without the redirects, client.search('http://www.youtube.com/watch?v=XAAgXaMCdfI') just returned nil. Now, it returns nil only when it hasn't been submitted, a listing otherwise.

BukhariH commented 10 years ago

Thanks for identifying this problem but as always this is a problem with reddit's appalling api design. APIs should not redirect & clients most certainly should not follow them.

Problem

Well try searching this non-existent url: http://reddit.com/search.json?q=http://www.youtube.com/watch?v=XAAgX54433aMCdfI2

You can see the response is:

"{}"

However, if you search this non-existent phrase: http://reddit.com/search.json?q=fhhfhkjdfhkjfsfhjksdhjkdhjhj45784375437895hiudfhjkvkjkczjdasudouiryr35345783789rhsfhjkfsafioui73478923yudhgsSDFSJFHSJFHJKFJKHSSFKHJKJHSFKJHF24874872378dfkjdhajIUOQWUQWQE

The response is:

{"kind": "Listing", "data": {"modhash": "1rrvj60ghsw34387e669712d48255154140bd129cefbb3cf80", "children": [], "after": null, "before": null}}

This is stupid. This brakes RedditKit. This is reddit's api.

Solution If you look at the source code: https://github.com/reddit/reddit/blob/master/r2/r2/controllers/front.py#L871

You can see reddit detects whether something is a url then redirects the query to /submit?url={YOUR_URL}.

What we need to effectively do is detect whether something is a url then query: reddit.com/submit.json?url={YOUR_URL}

This will return:

{"kind": "Listing", "data": {"modhash": "qqxkyfpfkzbb0b02851d1ed46d470b2500f50c42d7d46c879e", "children": [{"kind": "t3", "data": {"domain": "youtube.com", "banned_by": null, "media_embed": {"content": "<iframe class=\"embedly-embed\" src=\"//cdn.embedly.com/widgets/media.html?src=http%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "width": 600, "scrolling": false, "height": 338}, "subreddit": "Bad_Cop_No_Donut", "selftext_html": null, "selftext": "", "likes": null, "secure_media": {"oembed": {"provider_url": "http://www.youtube.com/", "description": "Video shows an officer tripping and pushing students rushing the field after Vandegrift High School's state soccer win Saturday. The Georgetown Police Department is investigating.", "title": "Officer trips, pushes students after soccer match; police investigating", "url": "http://www.youtube.com/watch?v=XAAgXaMCdfI", "type": "video", "author_name": "kxan", "height": 338, "width": 600, "html": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "thumbnail_width": 480, "version": "1.0", "provider_name": "YouTube", "thumbnail_url": "https://i.embed.ly/1/image?url=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=b1e305db91cf4aa5a86b732cc9fffceb", "thumbnail_height": 360, "author_url": "http://www.youtube.com/user/kxan"}, "type": "youtube.com"}, "link_flair_text": null, "id": "23kjd4", "gilded": 0, "secure_media_embed": {"content": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "width": 600, "scrolling": false, "height": 338}, "clicked": false, "stickied": false, "author": "mkultraman", "media": {"oembed": {"provider_url": "http://www.youtube.com/", "description": "Video shows an officer tripping and pushing students rushing the field after Vandegrift High School's state soccer win Saturday. The Georgetown Police Department is investigating.", "title": "Officer trips, pushes students after soccer match; police investigating", "url": "http://www.youtube.com/watch?v=XAAgXaMCdfI", "type": "video", "author_name": "kxan", "height": 338, "width": 600, "html": "<iframe class=\"embedly-embed\" src=\"//cdn.embedly.com/widgets/media.html?src=http%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "thumbnail_width": 480, "version": "1.0", "provider_name": "YouTube", "thumbnail_url": "http://i1.ytimg.com/vi/XAAgXaMCdfI/hqdefault.jpg", "thumbnail_height": 360, "author_url": "http://www.youtube.com/user/kxan"}, "type": "youtube.com"}, "score": 38, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "default", "subreddit_id": "t5_2qqdb", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 11, "saved": false, "is_self": false, "permalink": "/r/Bad_Cop_No_Donut/comments/23kjd4/officer_trips_pushes_students_after_soccer_match/", "name": "t3_23kjd4", "created": 1398087129.0, "url": "http://www.youtube.com/watch?v=XAAgXaMCdfI", "author_flair_text": null, "title": "Officer trips, pushes students after soccer match; police investigating", "created_utc": 1398058329.0, "ups": 49, "num_comments": 2, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "youtube.com", "banned_by": null, "media_embed": {"content": "<iframe class=\"embedly-embed\" src=\"//cdn.embedly.com/widgets/media.html?src=http%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "width": 600, "scrolling": false, "height": 338}, "subreddit": "news", "selftext_html": null, "selftext": "", "likes": null, "secure_media": {"type": "youtube.com", "oembed": {"provider_url": "http://www.youtube.com/", "description": "Video shows an officer tripping and pushing students rushing the field after Vandegrift High School's state soccer win Saturday. The Georgetown Police Department is investigating.", "title": "Officer trips, pushes students after soccer match; police investigating", "url": "http://www.youtube.com/watch?v=XAAgXaMCdfI", "author_name": "kxan", "height": 338, "width": 600, "html": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "thumbnail_width": 480, "version": "1.0", "provider_name": "YouTube", "thumbnail_url": "https://i.embed.ly/1/image?url=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=b1e305db91cf4aa5a86b732cc9fffceb", "type": "video", "thumbnail_height": 360, "author_url": "http://www.youtube.com/user/kxan"}}, "link_flair_text": null, "id": "23l967", "gilded": 0, "secure_media_embed": {"content": "<iframe class=\"embedly-embed\" src=\"https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "width": 600, "scrolling": false, "height": 338}, "clicked": false, "stickied": false, "author": "FictionFawkes", "media": {"type": "youtube.com", "oembed": {"provider_url": "http://www.youtube.com/", "description": "Video shows an officer tripping and pushing students rushing the field after Vandegrift High School's state soccer win Saturday. The Georgetown Police Department is investigating.", "title": "Officer trips, pushes students after soccer match; police investigating", "url": "http://www.youtube.com/watch?v=XAAgXaMCdfI", "author_name": "kxan", "height": 338, "width": 600, "html": "<iframe class=\"embedly-embed\" src=\"//cdn.embedly.com/widgets/media.html?src=http%3A%2F%2Fwww.youtube.com%2Fembed%2FXAAgXaMCdfI%3Ffeature%3Doembed&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXAAgXaMCdfI&image=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FXAAgXaMCdfI%2Fhqdefault.jpg&key=2aa3c4d5f3de4f5b9120b660ad850dc9&type=text%2Fhtml&schema=youtube\" width=\"600\" height=\"338\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "thumbnail_width": 480, "version": "1.0", "provider_name": "YouTube", "thumbnail_url": "http://i1.ytimg.com/vi/XAAgXaMCdfI/hqdefault.jpg", "type": "video", "thumbnail_height": 360, "author_url": "http://www.youtube.com/user/kxan"}}, "score": 1, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "", "subreddit_id": "t5_2qh3l", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 0, "saved": false, "is_self": false, "permalink": "/r/news/comments/23l967/video_officer_tripping_and_pushing_students/", "name": "t3_23l967", "created": 1398117833.0, "url": "http://www.youtube.com/watch?v=XAAgXaMCdfI", "author_flair_text": null, "title": "Video: officer tripping and pushing students rushing the field after Vandegrift High School's state soccer win Saturday. The Georgetown Police Department is investigating.", "created_utc": 1398089033.0, "ups": 1, "num_comments": 0, "visited": false, "num_reports": null, "distinguished": null}}], "after": null, "before": null}}

if there are no results then this will returned:

"{}"