Generate OPML files for following web-things
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.rb 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # frozen_string_literal: true
  2. require 'dotenv'
  3. require 'sinatra/base'
  4. require 'sinatra/reloader'
  5. require 'octokit'
  6. require 'redis'
  7. require 'json'
  8. require './opml'
  9. Octokit.auto_paginate = true
  10. class MyApp < Sinatra::Base
  11. configure :development do
  12. register Sinatra::Reloader
  13. also_reload 'opml.rb'
  14. Dotenv.load
  15. end
  16. configure do
  17. set :r, Redis.new(url: ENV.fetch('REDIS_URL'))
  18. set :client, Octokit::Client.new(
  19. client_id: ENV.fetch('GITHUB_CLIENT_ID'),
  20. client_secret: ENV.fetch('GITHUB_CLIENT_SECRET'),
  21. per_page: 100,
  22. auto_traversal: true,
  23. auto_paginate: true
  24. )
  25. end
  26. def get_starred_repos_from_cache(user)
  27. from_cache = settings.r.get "#{user}.repos"
  28. if from_cache
  29. time = settings.r.get "#{user}.repos.time" || Time.now.to_i
  30. return [JSON.parse(from_cache), time]
  31. end
  32. repos = []
  33. starred = settings.client.starred(user)
  34. starred.each do |repo|
  35. repos.push repo[:full_name]
  36. end
  37. time = Time.now.to_i
  38. settings.r.set "#{user}.repos", repos
  39. settings.r.set "#{user}.repos.time", time
  40. # Keep the starred repo list for 1 day
  41. settings.r.expire "#{user}.repos", 60*60*24
  42. [repos, time]
  43. end
  44. get '/' do
  45. File.read File.join 'public', 'index.html'
  46. end
  47. post '/submit/:type' do
  48. if params[:type] == 'github'
  49. username = params[:username]
  50. redirect "/github/#{username}/starred.opml"
  51. else
  52. "Invalid Type"
  53. end
  54. end
  55. get '/github/:user/starred.opml' do
  56. user = params[:user]
  57. repos, time = get_starred_repos_from_cache(user)
  58. opml = OPML.new do
  59. @title = "Releases: #{user}/starred"
  60. # TODO: Fix this
  61. @date_created = Time.now.rfc822
  62. @date_modified = Time.now.rfc822
  63. @owner_name = user
  64. end
  65. repos.each do |r|
  66. title = "#{r} Release Notes"
  67. html_url = "https://github.com/#{r}/releases"
  68. rss_url = "#{html_url}.atom"
  69. opml.add_outline(
  70. text: title, description: "#{r}/releases", html_url: html_url,
  71. xml_url: rss_url, title: title,
  72. type: 'rss', version: 'RSS2'
  73. )
  74. end
  75. filename = "#{user}-starred-releases.opml"
  76. response.headers['Content-Disposition'] = "attachment; filename=#{filename};"
  77. response.headers['Content-Type'] = 'application/octet-stream'
  78. response.headers['Content-Transfer-Encoding'] = 'binary'
  79. opml.xml
  80. end
  81. end