gists.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3.7
  2. import requests
  3. import os
  4. from datetime import datetime
  5. def sizeof_fmt(num, suffix='B'):
  6. for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
  7. if abs(num) < 1024.0:
  8. return "%3.1f%s%s" % (num, unit, suffix)
  9. num /= 1024.0
  10. return "%.1f%s%s" % (num, 'Yi', suffix)
  11. def main():
  12. API_URL = "https://api.github.com/users/master5o1/gists"
  13. directory = os.path.join('content', 'posts', f'gist')
  14. if not os.path.exists(directory) or not os.path.isdir(directory):
  15. os.mkdir(directory)
  16. print("Fetching gists.")
  17. r = requests.get(API_URL)
  18. if r.status_code != 200:
  19. return
  20. data = r.json()
  21. for gist in data:
  22. if not gist.get('public'):
  23. continue
  24. gist_id = gist.get('id')
  25. files = gist.get('files')
  26. date = gist.get('created_at')
  27. description = gist.get('description')
  28. url = gist.get('html_url')
  29. titles = list(files.keys())
  30. title = titles[0].strip(".md")
  31. print(f"GIST {gist_id} {title}")
  32. shortdate = date.split("T", maxsplit=1)[0]
  33. text = f"""---
  34. title: "{title}"
  35. date: {date}
  36. tags: ["gist", "{shortdate}"]
  37. draft: false
  38. ---
  39. {description}\n\n
  40. <!--more-->\n\n
  41. [gist link]({url})\n\n
  42. """
  43. sections = []
  44. attachments = ["| Attachment | Type | Size |\n| - | - | - |"]
  45. for attachment in files.values():
  46. name = attachment.get("filename")
  47. url = attachment.get("raw_url")
  48. mime = attachment.get("type")
  49. language = attachment.get("language")
  50. size = attachment.get("size")
  51. size = sizeof_fmt(size)
  52. section = []
  53. if len(list(files.keys())) > 1:
  54. attachments.append(f"| [{name}]({url}) | {mime} | {size} |")
  55. section = [f"### [{name}]({url}) -- {mime}, {size}"]
  56. rf = requests.get(url)
  57. body = rf.text
  58. if language == "Markdown":
  59. section.append(body)
  60. else:
  61. language = '' if language is None else language.lower()
  62. section.append(f"```{language}")
  63. section.append(body)
  64. section.append("```")
  65. sections.append("\n".join(section))
  66. if len(attachments) > 1:
  67. text = text + "\n".join(attachments) + "\n\n"
  68. text = text + "\n***\n\n"
  69. text = text + "\n***\n".join(sections) + "\n\n"
  70. filehash = gist_id if len(gist_id) < 6 else f"{gist_id[:3]}{gist_id[-3:]}"
  71. filename = f"{filehash}-{title}.md"
  72. subdir = directory
  73. # subdir = os.path.join(directory, shortdate)
  74. # if not os.path.exists(subdir) or not os.path.isdir(subdir):
  75. # os.mkdir(subdir)
  76. path = os.path.join(subdir, filename)
  77. print(f"\toutput: {path}")
  78. with open(path, 'w+') as file:
  79. file.write(text)
  80. if __name__ == '__main__':
  81. main()