r/dailyprogrammer • u/fvandepitte 0 0 • Jan 18 '16
[2016-01-18] Challenge #250 [Easy] Scraping /r/dailyprogrammer
Description
As you all know, we have a not very wel updated list of all the challenges.
Today we are going to build a webscraper that creates that list for us, preferably using the reddit api.
Normally when I create a challenge I don't mind how you format input and output, but now, since it has to be markdown, I do care about the output.
Our List of challenges consist of a 4-column table, showing the Easy, Intermediate and Hard challenges, as wel as an extra's.
The code code behind looks like this (minus the white line behind Easy | Intermediate | Hard | Weekly/Bonus):
Easy | Intermediate | Hard | Weekly/Bonus
-----|--------------|------|-------------
| []() | []() | []() | **-** |
| [[2015-09-21] Challenge #233 [Easy] The house that ASCII built](/r/dailyprogrammer/comments/3ltee2/20150921_challenge_233_easy_the_house_that_ascii/) | []() | []() | **-** |
| [[2015-09-14] Challenge #232 [Easy] Palindromes](/r/dailyprogrammer/comments/3kx6oh/20150914_challenge_232_easy_palindromes/) | [[2015-09-16] Challenge #232 [Intermediate] Where Should Grandma's House Go?](/r/dailyprogrammer/comments/3l61vx/20150916_challenge_232_intermediate_where_should/) | [[2015-09-18] Challenge #232 [Hard] Redistricting Voting Blocks](/r/dailyprogrammer/comments/3lf3i2/20150918_challenge_232_hard_redistricting_voting/) | **-** |Input
Not really, we need to be able to this.
Output
The entire table starting with the latest entries on top. There won't be 3 challenges for each week, so take considuration. But challenges from the same week are with the same index number (e.g. #1, #243).
Note We have changed the names from Difficult to Hard at some point
Bonus 1
It would also be nice if we could have the header generated. These are the 4 links you see at the top of /r/dailyprogrammer.
This is just a list and the source looks like this:
1. [Challenge #242: **Easy**] (/r/dailyprogrammer/comments/3twuwf/20151123_challenge_242_easy_funny_plant/)
2. [Challenge #242: **Intermediate**](/r/dailyprogrammer/comments/3u6o56/20151118_challenge_242_intermediate_vhs_recording/)
3. [Challenge #242: **Hard**](/r/dailyprogrammer/comments/3ufwyf/20151127_challenge_242_hard_start_to_rummikub/)
4. [Weekly #24: **Mini Challenges**](/r/dailyprogrammer/comments/3o4tpz/weekly_24_mini_challenges/)Bonus 2
Here we do want to use an input.
We want to be able to generate just a one or a few rows by giving the rownumber(s)
Input
213Output
| [[2015-09-07] Challenge #213 [Easy] Cellular Automata: Rule 90](/r/dailyprogrammer/comments/3jz8tt/20150907_challenge_213_easy_cellular_automata/) | [[2015-09-09] Challenge #231 [Intermediate] Set Game Solver](/r/dailyprogrammer/comments/3ke4l6/20150909_challenge_231_intermediate_set_game/) | [[2015-09-11] Challenge #231 [Hard] Eight Husbands for Eight Sisters](/r/dailyprogrammer/comments/3kj1v9/20150911_challenge_231_hard_eight_husbands_for/) | **-** |Input
229
228
227
226Output
| [[2015-08-24] Challenge #229 [Easy] The Dottie Number](/r/dailyprogrammer/comments/3i99w8/20150824_challenge_229_easy_the_dottie_number/) | [[2015-08-26] Challenge #229 [Intermediate] Reverse Fizz Buzz](/r/dailyprogrammer/comments/3iimw3/20150826_challenge_229_intermediate_reverse_fizz/) | [[2015-08-28] Challenge #229 [Hard] Divisible by 7](/r/dailyprogrammer/comments/3irzsi/20150828_challenge_229_hard_divisible_by_7/) | **-** |
| [[2015-08-17] Challenge #228 [Easy] Letters in Alphabetical Order](/r/dailyprogrammer/comments/3h9pde/20150817_challenge_228_easy_letters_in/) | [[2015-08-19] Challenge #228 [Intermediate] Use a Web Service to Find Bitcoin Prices](/r/dailyprogrammer/comments/3hj4o2/20150819_challenge_228_intermediate_use_a_web/) | [[08-21-2015] Challenge #228 [Hard] Golomb Rulers](/r/dailyprogrammer/comments/3hsgr0/08212015_challenge_228_hard_golomb_rulers/) | **-** |
| [[2015-08-10] Challenge #227 [Easy] Square Spirals](/r/dailyprogrammer/comments/3ggli3/20150810_challenge_227_easy_square_spirals/) | [[2015-08-12] Challenge #227 [Intermediate] Contiguous chains](/r/dailyprogrammer/comments/3gpjn3/20150812_challenge_227_intermediate_contiguous/) | [[2015-08-14] Challenge #227 [Hard] Adjacency Matrix Generator](/r/dailyprogrammer/comments/3h0uki/20150814_challenge_227_hard_adjacency_matrix/) | **-** |
| [[2015-08-03] Challenge #226 [Easy] Adding fractions](/r/dailyprogrammer/comments/3fmke1/20150803_challenge_226_easy_adding_fractions/) | [[2015-08-05] Challenge #226 [Intermediate] Connect Four](/r/dailyprogrammer/comments/3fva66/20150805_challenge_226_intermediate_connect_four/) | [[2015-08-07] Challenge #226 [Hard] Kakuro Solver](/r/dailyprogrammer/comments/3g2tby/20150807_challenge_226_hard_kakuro_solver/) | **-** |Note As /u/cheerse points out, you can use the Reddit api wrappers if available for your language
55
u/hutsboR 3 0 Jan 18 '16 edited Jan 18 '16
I don't really like posting top level comments that aren't solutions but I've got to say that this problem is a bitch. If you're not using a wrapper you have to manually hit the API (and you'll probably want to authenticate with script-based OAuth2) and paginate through all of the submissions (luckily it's
<1000, so you won't run into trouble) and then you have to try to separate them into weeks. Separating the challenges into weeks is difficult. You can't rely on dates because of submissions that fall out of schedule. You can't assume that they're ordered such that once you find the hard submission for week x the previous two submissions will be the intermediate and easy challenges respectively. You're stuck trying to extract information from the submission's title and they're not all formatted the same. (difficult/hard, challenge number format, typos, spacing, bonus/practical/weekly/monthly...) I'm not saying that this is necessarily difficult but there's probably a ton of edge cases. If you manage to figure this out, you then have to figure out how the hell to properly place it inside of a table. An issue I immediately noticed is that there are cases where certain weeks have multiple challenges of the same difficulty, how am I supposed to choose which one goes in which column? Do I have to fall back on submission date and assume the earlier one goes in the easy column? That's another layer of complexity. This is not as simple of a task as it may seem on the surface, definitely not an easy. This feels like something someone would have you do at work rather than a programming challenge.21
u/CamusPlague Jan 18 '16
Is this easy? I need to rethink my ability. Thought I was finally getting the hang of "easy".
9
u/aloisdg Jan 18 '16 edited Jan 18 '16
Summary
Got it but with some twists. I coded everything in .NETFiddle, so sorry if I made some mistakes. I miss VS. You can try my code here. I put the code on GitHub.
Caveat
Weekly/Bonusinto anOthercategory.Laziness is the main reason behind this...
Code
Output
4
u/jrm2k6 Jan 19 '16
Oh, how funny. I actually have an open source project to do that. dailyprogrammer newsletter! Is it cool if I put a link to it here?
5
u/cheers- Jan 18 '16
Wrappers: https://github.com/reddit/reddit/wiki/API-Wrappers
4
u/j_random0 Jan 18 '16 edited Jan 18 '16
Download test:
https://api.reddit.com/r/dailyprogrammer.xml
https://api.reddit.com/r/dailyprogrammer.json
https://api.reddit.com/r/dailyprogrammer.rss
All these don't go all the way back to beginning however.
Get top 100. http://blog.tankorsmash.com/?p=378
Get the 'more' page via API.
https://www.reddit.com/r/dailyprogrammer.json?count=25&after=t3_3u6o56
https://www.reddit.com/r/dailyprogrammer.xml?count=25&after=t3_3u6o56
The
after=t3_<base36>part is probably the short URL segment in post URL... The "t3_" prepended to it that is. Still not sure where those come from.The Reddit short-URL (top right margin) for this post uses the same <base36> as this subreddit post (clicking title within post). Substituting regular comment post codes don't work even though subreddit posts have "comment" in URL sometimes.
4
u/T-Fowl Jan 18 '16
Took me some time, and it could be a lot cleaner. However it is currently 3 am and I have plans for today. Language is Scala and this is my
firstsecond time posting on dailyprogrammer. Any advice is welcomed!Neither of the bonuses are implemented, nor are weekly bonuses (for the above mentioned reason), however my program can produce all challenges back to #3 (1 & 2 have different formatting that changed between weeks) and it handles challenges with multiple parts (e.g. #244 where the easy challenge had 2 parts).
Code ( Formatting might be a bit off from entering it into reddit )
Result:
[2015-12-09] Challenge #244 [Intermediate] Higher order functions Array language (part 2)
3
u/drguildo Jan 19 '16
I hope you don't take offence but maybe you should get a native English speaker to proof read? There are a few spelling and grammar errors.
3
u/Blackshell 2 0 Jan 20 '16
Wrote a solution in Python 3. Must have
prawinstalled. You can see its output all nicely rendered as Markdown here: https://github.com/fsufitch/dailyprogrammer/blob/master/250_easy/posts_by_week.mdSolution below:
It's not a very hard problem, but it certainly poses interesting challenges for those of us without experience working with API programming.
2
u/changed_perspective Jan 18 '16
Hey just wanted to say thanks op for this challenge!! Hopefully a or the best solution can be used to update challenge list! Can't wait to get started on this in the morning.
2
u/cheers- Jan 18 '16 edited Jan 18 '16
Java Bonus 2
It uses DOM and XPATH to query the rss file. Order of threads is based only on date(too many edge cases) You can pass on or more weeks(e.g. 248) and it will generate rows of the table for each week.
| [2016-01-04] Challenge #248 [Easy] Draw Me Like One Of Your Bitmaps | [2016-01-06] Challenge #248 [Intermediate] A Measure of Edginess | [2016-01-08] Challenge #248 [Hard] NotClick game | - |
| [2015-12-28] Challenge #247 [Easy] Secret Santa | [2015-12-30] Challenge #247 [Intermediate] Moving (diagonally) Up in Life | [2016-01-01] CHallenge #247 [Hard] Zombies on the highways! | - |
2
u/CleverError Jan 19 '16
Heres my solution written in Swift. It's also on GitHub
I decided to group the posts based on the calendar week the thread was posted on. If there are more than one post for a cell in the table, there are ordered chronologically separated by newlines.
Sample Output
The full output can can seen here
r/DailyProgrammer is a Trending Subreddit of the Day!
2
u/antigravcorgi Jan 20 '16
1
u/j_random0 Jan 18 '16 edited Jan 18 '16
Currently running this to see what will happen lol.
If it works I'll have a list of <base36> id's to process later...
UPDATE: took several minutes to only get the first 27. :/
1
u/j_random0 Jan 18 '16 edited Jan 18 '16
This will take awhile, and I'm not going to work on it all the time... But interesting enough to give a try.
The titles should be validated as not containing double-['s... Think about that later!
1
u/j_random0 Jan 19 '16 edited Jan 19 '16
Where's a good place to upload files? Found one! links.html I made an ad hoc script (regular expressions not proper parsing, delicate regular expressions...) that went back ~750 links. I know you want something as a cron job but that only has to get the most recent.
This was reeeally sloppy, it didn't quit so oldest are duplicated and there is no
<br>at end of lines. :/If it did get them all it should just be simpler text processing to format. The results looked like this:
And so on...
UPDATE Result after some awk. BTW Uploads are harder than you would think! But this one worked well. http://filebin.ca/2U2eJZvpUHFq/links.md
1
u/quails4 Jan 19 '16 edited Jan 19 '16
I had a go using C# and RedditSharp, it dumps the output to a file so I've put it on pastebin http://pastebin.com/n7yqV9RC
Is the output correct? I wasn't 100% sure what was expected.
1
u/cruyff8 Jan 18 '16
My partial solution that uses the rss feed on reddit, and python to format the entries into a list of tuples of dates, levels and titles.