Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

2013-01-09 Sort Daily Programmer

By: a guest on Mar 10th, 2013  |  syntax: Python  |  size: 2.34 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2.  
  3. import sys,requests,re
  4.  
  5. URL = 'http://www.reddit.com/r/dailyprogrammer/.json?limit=none&after='
  6. # output format
  7. # [Easy / Medium / Hard] #<ID>: "<Title>" <URL>
  8.  
  9. # title format:
  10. # [01/21/13] Challenge #118 [Easy] Date Localization
  11. # [easy] challenge #2
  12. def format_title(title):
  13.   m = re.search(r'\[easy.*\]|\[intermediate\]|\[difficult\]|\[hard\]',title,flags=re.IGNORECASE)
  14.  
  15.   difficulty = ""
  16.   if m:
  17.     difficulty = m.group()
  18.     for x,y in [("easy","Easy"),("intermediate","Medium"),("difficult","hard")]:
  19.       difficulty = re.sub(x,y,difficulty,flags=re.IGNORECASE)
  20.    
  21.   m = re.search(r'#\d*',title)
  22.  
  23.   number = ""
  24.   if m:
  25.     number = m.group()
  26.   m = re.search(r'#\d*\s\[\w*\]\s(\w*.*$)',title)
  27.  
  28.   name = ""
  29.   if m:
  30.     name = m.group(1)
  31.  
  32.   return difficulty.title() + " " + number + ": " + name
  33.  
  34. def get_posts():
  35.  
  36.   r = requests.get(URL)
  37.   j = r.json()
  38.   posts = j['data']['children']
  39.   titles = []
  40.   while True:
  41.     for post in posts:
  42.       title = post['data']['title']
  43.       if "#" not in title or "Challenge" not in title:
  44.         continue
  45.       title = format_title(title)
  46.       url = post['data']['url']
  47.       titles.append(title + " " + url)
  48.  
  49.     if j['data']['after'] == None:
  50.           break
  51.     r = requests.get(URL+j['data']['after'])
  52.     j = r.json()
  53.     posts = j['data']['children']
  54.   return titles
  55.  
  56. def sort_level(post):
  57.   first_word = (post.split())[0]
  58.   second_word = (post.split())[1]
  59.   second_len = len(second_word)
  60.   second_word = int(second_word[1:second_len-1])
  61.  
  62.   if "Easy" in first_word:
  63.     return (0,second_word)
  64.   if "Medium" in first_word:
  65.     return (1,second_word)
  66.   if "Hard" in first_word:
  67.     return (2,second_word)
  68.   return 3
  69.  
  70. def sort_number(post):
  71.   first_word = (post.split())[0]
  72.   second_word = (post.split())[1]
  73.   second_len = len(second_word)
  74.   second_word = int(second_word[1:second_len-1])
  75.   if "Easy" in first_word:
  76.     return (second_word,0)
  77.   if "Medium" in first_word:
  78.     return (second_word,1)
  79.   if "Hard" in first_word:
  80.     return (second_word,2)
  81.   return 3
  82.  
  83. def main():
  84.   posts = get_posts()
  85.   print "List One:"
  86.   list1 = sorted(posts, key=sort_level)
  87.   for post  in list1:
  88.     print post
  89.   print "List Two:"
  90.   list1 = sorted(posts, key=sort_number)
  91.   for post  in list1:
  92.     print post
  93. if __name__ == '__main__':
  94.   main()