open-source-ios-apps/.github/osia_convert.rb

312 lines
6.6 KiB
Ruby
Raw Normal View History

2016-05-16 20:29:04 +03:00
require_relative 'osia_helper'
require 'date'
2016-05-11 17:22:10 +03:00
2019-05-07 15:16:24 +03:00
# Constants
2016-05-13 18:50:39 +03:00
README = 'README.md'
2016-05-13 19:22:57 +03:00
ARCHIVE = 'ARCHIVE.md'
APPSTORE = 'APPSTORE.md'
2019-05-07 14:53:46 +03:00
LATEST = 'LATEST.md'
NOT_ENGLISH = '🌐'
ARCHIVE_TAG = 'archive'
2019-05-08 04:39:12 +03:00
LATEST_NUM = 15
2019-05-07 15:16:24 +03:00
# Helpers
def app_store_total(j)
apps = j['projects']
s = apps.reject { |x| x['itunes'].nil? }
count = 1
s.each do |x|
tags = x['tags']
if tags.nil?
t = "#{count} "
count = count + 1
else
unless tags.include?(ARCHIVE_TAG)
t = "#{count} #{tags}"
count = count + 1
end
end
end
count
end
2016-05-13 19:22:57 +03:00
def apps_archived(apps)
a = apps.select { |a| a['tags'] != nil }.select { |b| b['tags'].include?(ARCHIVE_TAG) }
2017-03-28 20:11:16 +03:00
a.sort_by { |k, v| k['title'].downcase }
2016-05-13 19:22:57 +03:00
end
2016-05-11 17:22:10 +03:00
def apps_for_cat(apps, id)
2016-05-13 19:22:57 +03:00
f = apps.select do |a|
tags = a['tags']
if tags.nil?
true
else
!(tags.include?(ARCHIVE_TAG))
2016-05-13 19:22:57 +03:00
end
end
s = f.select do |a|
2016-08-25 17:35:26 +03:00
cat = a['category-ids']
2016-05-11 18:07:03 +03:00
cat.class == Array ? cat.include?(id) : (cat == id)
end
2016-06-12 07:06:12 +03:00
s.sort_by { |k, v| k['title'].downcase }
2016-05-11 18:07:03 +03:00
end
2019-05-08 04:39:12 +03:00
def apps_latest(apps, num)
2019-05-07 14:53:46 +03:00
a = apps.select { |a| a['date_added'] != nil }
.sort_by { |k, v| DateTime.parse(k['date_added']) }
.reverse
2019-05-08 04:39:12 +03:00
a[0..num - 1]
2019-05-07 14:53:46 +03:00
end
def output_apps(apps, appstoreonly)
2016-05-11 17:22:10 +03:00
o = ''
2016-05-11 18:07:03 +03:00
apps.each do |a|
2016-09-03 04:39:38 +03:00
name = a['title']
link = a['source']
itunes = a['itunes']
homepage = a['homepage']
desc = a['description']
tags = a['tags']
stars = a['stars']
lang = a['lang']
2016-05-11 17:22:10 +03:00
2016-09-03 04:39:38 +03:00
date_added = a['date_added']
screenshots = a['screenshots']
license = a['license']
2016-07-28 18:15:37 +03:00
t = "[#{name}](#{link})"
2016-05-11 17:22:10 +03:00
2016-09-03 04:39:38 +03:00
if desc.nil?
t << ' '
else
t << ": #{desc} " if desc.size>0
end
unless itunes.nil?
t << "[` App Store`](#{itunes}) "
end
if appstoreonly
next if itunes.nil?
end
2016-09-03 04:39:38 +03:00
o << "- #{t} \n"
o << " <details>\n\t<summary>"
2016-09-03 04:39:38 +03:00
details = ""
2016-09-03 04:39:38 +03:00
unless tags.nil?
details << '<code>swift</code> ' if tags.include? 'swift'
2016-09-03 04:39:38 +03:00
tags.each do |t|
details << "<code>#{t.downcase}</code> " if t.downcase != 'swift'
2016-05-11 17:22:10 +03:00
end
2016-09-03 04:39:38 +03:00
end
details << "#{NOT_ENGLISH} " unless lang.nil?
2016-09-03 04:39:38 +03:00
unless stars.nil?
details << output_stars(stars)
end
o << details
o << "</summary>"
details_list = []
unless homepage.nil?
details_list.push homepage
end
2016-05-11 17:22:10 +03:00
2016-09-03 04:39:38 +03:00
unless date_added.nil?
date = DateTime.parse(date_added)
formatted_date = date.strftime "%B %e, %Y"
details_list.push "Added #{formatted_date}"
end
unless license.nil?
license_display = license == 'other' ? "`#{license}`" : "[`#{license}`](http://choosealicense.com/licenses/#{license}/)"
2016-09-03 04:39:38 +03:00
details_list.push "License: #{license_display}"
end
details = "\n\n\t"
2016-09-03 04:39:38 +03:00
details << details_list[0]
details_list[1..-1].each { |x| details << "<br> #{x}" }
unless screenshots.nil? || screenshots.empty?
details << "<br>"
details << "\n\t"
screenshots.each_with_index do |s, i|
details << "<a href='#{screenshots[i]}'><code>Screenshot #{i+1}</code></a> "
end
2016-09-03 04:39:38 +03:00
end
details << "\n "
details << "</details>\n\n"
2016-09-03 04:39:38 +03:00
o << details
2016-05-11 17:22:10 +03:00
end
o
end
2016-08-08 23:20:47 +03:00
def output_badges(count)
date = DateTime.now
date_display = date.strftime "%B %e, %Y"
2017-03-21 17:51:59 +03:00
date_display = date_display.gsub ' ', '%20'
2016-08-08 23:20:47 +03:00
b = "![](https://img.shields.io/badge/Projects-#{count}-green.svg) [![](https://img.shields.io/badge/Twitter-@opensourceios-blue.svg)](https://twitter.com/opensourceios) ![](https://img.shields.io/badge/Updated-#{date_display}-lightgrey.svg)"
end
2016-05-16 20:29:04 +03:00
def output_stars(number)
case number
when 100...200
2018-06-16 05:20:26 +03:00
'⭐'
2016-05-16 20:29:04 +03:00
when 200...500
2018-06-16 05:20:26 +03:00
'⭐⭐'
2016-05-16 20:29:04 +03:00
when 500...1000
2018-06-16 05:20:26 +03:00
'⭐⭐⭐'
2016-05-16 20:29:04 +03:00
when 1000...2000
2018-06-16 05:20:26 +03:00
'⭐⭐⭐⭐'
2016-05-16 20:29:04 +03:00
when 2000...100000
2018-06-16 05:20:26 +03:00
'⭐⭐⭐⭐⭐'
2016-05-16 20:29:04 +03:00
else
''
end
end
def write_archive(j, subtitle)
2019-05-07 15:16:24 +03:00
t = j['title']
apps = j['projects']
archived = apps_archived apps
footer = j['footer']
2019-05-07 15:16:24 +03:00
output = "\# #{t} Archive\n\n"
output << subtitle
2019-05-07 15:16:24 +03:00
output << "\n"
archived.each do |a|
t = a['title']
s = a['source']
output << "- [#{t}](#{s})\n"
end
output << "\n"
output << footer
2019-05-07 15:16:24 +03:00
file = ARCHIVE
File.open(file, 'w') { |f| f.write output }
puts "wrote #{file}"
end
def write_latest(j, num, subtitle)
2019-05-07 15:16:24 +03:00
t = j['title']
apps = j["projects"]
footer = j['footer']
2019-05-08 04:39:12 +03:00
latest = apps_latest(apps, num)
2019-05-07 15:16:24 +03:00
output = "\# #{t} Latest\n\n"
output << subtitle
2019-05-07 15:16:24 +03:00
output << "\n"
count = 1
latest.each do |a|
t = a['title']
s = a['source']
output << "#{count}. [#{t}](#{s})\n"
count = count + 1
end
output << "\n"
output << footer
2019-05-07 15:16:24 +03:00
file = LATEST
File.open(file, 'w') { |f| f.write output }
puts "wrote #{file}"
end
def write_list(j, file, subtitle, appstoreonly = false)
t = j['title']
desc = j['description']
h = j['header']
f = j['footer']
2016-05-13 18:50:39 +03:00
cats = j['categories']
apps = j['projects']
2016-05-11 17:22:10 +03:00
2018-04-22 23:48:15 +03:00
sponsor = j['sponsor']
2016-05-13 18:50:39 +03:00
output = '# ' + t
output << "\n\n"
2017-06-15 19:20:00 +03:00
output << desc
2019-05-20 04:59:08 +03:00
output << "\n\n#{subtitle}\n\n"
if appstoreonly == false
output << output_badges(apps.count)
2018-04-22 23:48:15 +03:00
unless sponsor.length == 0
output << "\n\n"
output << sponsor
output << "\n"
end
end
2016-08-09 15:58:50 +03:00
output << "\n\nJump to\n\n"
2016-05-13 18:50:39 +03:00
cats.each do |c|
2016-06-06 19:00:38 +03:00
title = c['title']
m = title.match /\[.*?\]/
title = m[0].sub('[', '').sub(']', '') unless m.nil?
temp = "#{' ' unless c['parent'] == nil }- [#{title}](\##{c['id']}) \n"
2016-05-13 18:50:39 +03:00
output << temp
end
2016-05-11 17:22:10 +03:00
2016-07-29 18:18:19 +03:00
output << "- [Thanks](#thanks)\n"
output << "- [Contact](#contact)\n"
2016-05-13 18:50:39 +03:00
output << "\n"
output << h
output << "\n"
2016-05-11 17:22:10 +03:00
2016-05-13 18:50:39 +03:00
cats.each do |c|
temp = "\n#\##{'#' unless c['parent']==nil } #{c['title']} \n \n"
2016-05-11 17:22:10 +03:00
2016-05-13 18:50:39 +03:00
d = c['description']
temp << "#{d}" unless d.nil?
2016-05-11 20:53:59 +03:00
2016-05-13 18:50:39 +03:00
temp << "[back to top](#readme) \n \n"
output << temp
2016-05-11 20:53:59 +03:00
2016-05-13 18:50:39 +03:00
cat_apps = apps_for_cat(apps, c['id'])
output << output_apps(cat_apps, appstoreonly)
2016-05-13 18:50:39 +03:00
end
output << "\n"
output << f
2016-05-11 17:22:10 +03:00
File.open(file, 'w') { |f| f.write output }
puts "wrote #{file}"
2016-05-11 17:22:10 +03:00
end
2019-05-07 15:16:24 +03:00
# Script begins
2019-05-07 14:53:46 +03:00
2016-05-16 20:29:04 +03:00
j = get_json
2016-05-11 17:22:10 +03:00
subtitle_readme = j['subtitle']
write_list(j, README, subtitle_readme)
2019-05-19 17:16:27 +03:00
subtitle_app_store = "List of **#{app_store_total j}** open-source apps published on the App Store (complete list [here](https://github.com/dkhamsing/open-source-ios-apps))."
write_list(j, APPSTORE, subtitle_app_store, true)
subtitle_archive = "This is an archive of the [main list](https://github.com/dkhamsing/open-source-ios-apps) for projects that are no longer maintained / old.\n\n"
write_archive(j, subtitle_archive)
subtitle_latest = "These are the #{LATEST_NUM} latest entries from the [main list](https://github.com/dkhamsing/open-source-ios-apps).\n\n"
write_latest(j, LATEST_NUM, subtitle_latest)