|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <fstream> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +string newApiOp; |
| 8 | +string fileType; |
| 9 | +bool javascriptFile; |
| 10 | +string repoSearchType; |
| 11 | + |
| 12 | +string singleRepoOwner; |
| 13 | +string singleRepoName; |
| 14 | +string singleUrl; |
| 15 | + |
| 16 | +string repoName; |
| 17 | +string repoFullName; |
| 18 | +string repoDesc; |
| 19 | +string repoHtmlUrl; |
| 20 | + |
| 21 | +string fileName = "newApi++File"; |
| 22 | +string filePath = "C:\\code\\api++"; |
| 23 | +string fullFilePath; |
| 24 | +string fullFilePathConfirm; |
| 25 | + |
| 26 | +string fileIdValue = "apiresultsapi++"; |
| 27 | + |
| 28 | +string fileTitleLink; |
| 29 | +string fileTitleLinkCustom = "#"; |
| 30 | +string fileTitleClassName; |
| 31 | + |
| 32 | +string fileFullNameClassName; |
| 33 | + |
| 34 | +int main() |
| 35 | +{ |
| 36 | + int apiSys(); |
| 37 | + cout << "api++ starting..." << endl; |
| 38 | + cout << "Reading prefs file...(FUTURE FEATURE)" << endl; |
| 39 | + cout << "Start new api operation? [Y/N] "; |
| 40 | + cin >> newApiOp; |
| 41 | + if (newApiOp == "y" || newApiOp == "Y") { |
| 42 | + cout << "Embed the code directly into a HTML file or put it in a JavaScript file? [H (HTML)/J (JavaScript)] "; |
| 43 | + cin >> fileType; |
| 44 | + if (fileType == "h" || fileType == "H") { |
| 45 | + cout << "api++ will embed the code into a HTML file." << endl; |
| 46 | + javascriptFile = false; |
| 47 | + return apiSys(); |
| 48 | + } |
| 49 | + else if (fileType == "j" || fileType == "J") { |
| 50 | + cout << "api++ will put the code into a .js file." << endl; |
| 51 | + javascriptFile = true; |
| 52 | + return apiSys(); |
| 53 | + } |
| 54 | + } |
| 55 | + else { |
| 56 | + cout << "exiting..." << endl; |
| 57 | + system("pause"); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + return 0; |
| 61 | +} |
| 62 | +int apiSys() { |
| 63 | + int fileSys(); |
| 64 | + cout << "This application currently only supports GitHub's API system." << endl; |
| 65 | + cout << "For your own reference the base URL is https://api.github.com/" << endl; |
| 66 | + cout << "Choose whether you would like to embed a single repository or a list (found using filters). [R (single repo)/L (list of repos) "; |
| 67 | + cin >> repoSearchType; |
| 68 | + if (repoSearchType == "R" || repoSearchType == "r") { |
| 69 | + cout << "Please type the owner of the repository you are trying to embed: "; |
| 70 | + cin >> singleRepoOwner; |
| 71 | + cout << "Please type the name of the repository you are trying to embed: "; |
| 72 | + cin >> singleRepoName; |
| 73 | + singleUrl = "https://api.github.com/repos/" + singleRepoOwner + "/" + singleRepoName; |
| 74 | + cout << "This is the URL api++ will be using: " << singleUrl << endl; |
| 75 | + cout << "Here is the repo HTML URL (the URL a user would go to view the repo.)" << endl; |
| 76 | + repoHtmlUrl = "https://github.com/" + singleRepoOwner + "/" + singleRepoName; |
| 77 | + cout << repoHtmlUrl << endl; |
| 78 | + |
| 79 | + cout << "api++ needs to know what parts of the repo you will want to be displayed." << endl; |
| 80 | + cout << "Include title? [Y/N] "; |
| 81 | + cin >> repoName; |
| 82 | + cout << "Include the full name? [Y/N] "; |
| 83 | + cin >> repoFullName; |
| 84 | + cout << "Include the description? [Y/N] "; |
| 85 | + cin >> repoDesc; |
| 86 | + return fileSys(); |
| 87 | + } |
| 88 | +} |
| 89 | +int fileSys() { |
| 90 | + ofstream file; |
| 91 | + //get file info |
| 92 | + if (javascriptFile == true) { |
| 93 | + cout << "What should the JavaScript file be named? (no spaces) "; |
| 94 | + cin >> fileName; |
| 95 | + } |
| 96 | + else if (javascriptFile == false) { |
| 97 | + cout << "What should the HTML file be named? (no spaces) "; |
| 98 | + cin >> fileName; |
| 99 | + } |
| 100 | + cout << "Where should the file be placed? (no spaces) "; |
| 101 | + cin >> filePath; |
| 102 | + if (javascriptFile == true) { |
| 103 | + fullFilePath = filePath + "\\\\" + fileName + ".js"; |
| 104 | + } |
| 105 | + else if (javascriptFile == false) { |
| 106 | + fullFilePath = filePath + "\\\\" + fileName + ".html"; |
| 107 | + } |
| 108 | + cout << "Is this good? [Y/N]" << endl; |
| 109 | + cout << fullFilePath << endl; |
| 110 | + cin >> fullFilePathConfirm; |
| 111 | + |
| 112 | + if (fullFilePathConfirm == "Y" || fullFilePathConfirm == "y") { |
| 113 | + ofstream file((fullFilePath).c_str()); |
| 114 | + if (javascriptFile == false) { |
| 115 | + file << "<!DOCTYPE html>" << endl; |
| 116 | + file << "<html>" << endl; |
| 117 | + file << "<head>" << endl; |
| 118 | + file << "<title>api++ auto generated file</title>" << endl; |
| 119 | + file << "</head>" << endl; |
| 120 | + file << "<body>" << endl; |
| 121 | + file << "<script>" << endl; |
| 122 | + } |
| 123 | + file << "//JavaScript code generated by API++" << endl; |
| 124 | + file << "//API++ by Piccolowen" << endl; |
| 125 | + file << "window.onload = apifunc();" << endl; |
| 126 | + file << "async function apifunc() {" << endl; |
| 127 | + if (repoSearchType == "R" || repoSearchType == "r") { |
| 128 | + file << "const url = '" << singleUrl << "';" << endl; |
| 129 | + } |
| 130 | + file << "const response = await fetch(url);" << endl; |
| 131 | + file << "const result = await response.json();" << endl; |
| 132 | + |
| 133 | + cout << "For this code to work the code needs to place the results in a HTML element with a certain ID value. What should that value be? (no spaces) "; |
| 134 | + cin >> fileIdValue; |
| 135 | + cout << "ID value: " << fileIdValue << endl; |
| 136 | + |
| 137 | + file << "const apiresult = document.getElementById('" << fileIdValue << "');" << endl; |
| 138 | + file << "console.log('api++ system working...')" << endl; |
| 139 | + file << "console.log('api++ system by Piccolowen https://github.com/Piccolowen')" << endl; |
| 140 | + file << "console.log('api++ result:');" << endl; |
| 141 | + file << "console.log(result);" << endl; |
| 142 | + |
| 143 | + //repo title |
| 144 | + if (repoName == "Y" || repoName == "y") { |
| 145 | + file << "console.log('name result: ' + result.name)" << endl; |
| 146 | + cout << "Should the title element have link? [C (custom link)/R (repo link)/N (no)" << endl; |
| 147 | + cin >> fileTitleLink; |
| 148 | + if (fileTitleLink == "R" || fileTitleLink == "r") { |
| 149 | + file << "const title = document.createElement('a')" << endl; |
| 150 | + file << "title.href = result.html_url" << endl; |
| 151 | + } |
| 152 | + else if (fileTitleLink == "C" || fileTitleLink == "c") { |
| 153 | + cout << "Custom URL: "; |
| 154 | + cin >> fileTitleLinkCustom; |
| 155 | + file << "const title = document.createElement('a')" << endl; |
| 156 | + file << "title.href = '" << fileTitleLinkCustom << "'" << endl; |
| 157 | + } |
| 158 | + else { |
| 159 | + file << "const title = document.createElement('p')" << endl; |
| 160 | + } |
| 161 | + file << "title.target = '_blank'" << endl; |
| 162 | + cout << "What should the class value be for the title? (the class name allows styling with CSS) [no spaces] "; |
| 163 | + cin >> fileTitleClassName; |
| 164 | + cout << "Class name: " << fileTitleClassName << endl; |
| 165 | + file << "title.className = '" << fileTitleClassName << "'" << endl; |
| 166 | + file << "title.textContent = result.name" << endl; |
| 167 | + file << "apiresult.appendChild(title)" << endl; |
| 168 | + } |
| 169 | + //repo fullname |
| 170 | + if (repoFullName == "Y" || repoFullName == "y") { |
| 171 | + file << "console.log('full name result: ' + result.full_name)" << endl; |
| 172 | + file << "const fullname = document.createElement('p')" << endl; |
| 173 | + cout << "What should the class value be for the full name? (the class name allows styling with CSS) [no spaces] "; |
| 174 | + cin >> fileFullNameClassName; |
| 175 | + cout << "Class name: " << fileFullNameClassName << endl; |
| 176 | + file << "fullname.className = '" << fileFullNameClassName << "'" << endl; |
| 177 | + file << "fullname.textContent = result.full_name" << endl; |
| 178 | + file << "apiresult.appendChild(fullname)" << endl; |
| 179 | + } |
| 180 | + //repo desc |
| 181 | + if (repoDesc == "Y" || repoDesc == "y") { |
| 182 | + file << "console.log('desc result: ' + result.description)" << endl; |
| 183 | + file << "const desc = document.createElement('p')" << endl; |
| 184 | + cout << "What should the class value be for the description? (the class name allows styling with CSS) [no spaces] "; |
| 185 | + cin >> fileFullNameClassName; |
| 186 | + cout << "Class name: " << fileFullNameClassName << endl; |
| 187 | + file << "desc.className = '" << fileFullNameClassName << "'" << endl; |
| 188 | + file << "desc.textContent = result.description" << endl; |
| 189 | + file << "apiresult.appendChild(desc)" << endl; |
| 190 | + } |
| 191 | + file << "}" << endl; |
| 192 | + |
| 193 | + if (javascriptFile == false) { |
| 194 | + file << "</script>" << endl; |
| 195 | + file << "</body>" << endl; |
| 196 | + file << "</html>" << endl; |
| 197 | + cout << "Make sure to add an element with ID value: " << fileIdValue << "!" << endl; |
| 198 | + } |
| 199 | + } |
| 200 | + else { |
| 201 | + return fileSys(); |
| 202 | + } |
| 203 | +} |
0 commit comments