-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jörn Roth
committed
May 1, 2021
1 parent
c524d6e
commit 9b1deac
Showing
57 changed files
with
101,788 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
#include "SheetsAPI.h" | ||
#include <string> | ||
#include <cpr/cpr.h> | ||
#include "json.hpp" | ||
#include "logger.h" | ||
#include "string_format.hpp" | ||
|
||
using namespace nlohmann; | ||
using std::vector; | ||
using std::string; | ||
using std::unique_ptr; | ||
|
||
SheetsAPI::SheetsAPI() : sheetId(""), api_key("") { | ||
downloading = false; | ||
Logger::d("default constr"); | ||
} | ||
|
||
SheetsAPI::SheetsAPI(string sheetId_, string api_key_) : sheetId(sheetId_), api_key(api_key_) { | ||
downloading = false; | ||
Logger::d(string_format("sid:%s", sheetId.c_str())); | ||
} | ||
|
||
unique_ptr<json> getSheetValuesJson(const string& sheetId, const string& range, const string api_key) { | ||
cpr::Url url = cpr::Url("https://sheets.googleapis.com/v4/spreadsheets/" + sheetId + "/values/" + range + "?key=" + api_key); | ||
//, cpr::Parameters{ { "key", api_key } } | ||
cpr::Response r = cpr::Get(url); | ||
if (r.status_code != 200) { | ||
Logger::e("HTTP returned " + r.status_code); | ||
return NULL; | ||
} | ||
Logger::i(r.text); | ||
json j = "{\"range\": \"'The Truth'!A5:A14\",\"majorDimension\" : \"ROWS\",\"values\" : [[\"Ewanye\"],[\"Fra\"],[\"Make\"],[\"Zerumi\"],[\"Pitt\"],[\"Miruna\"],[\"Pv\"],[\"Lynn\"],[\"Lele\"],[\"Rey\"]]}"_json; | ||
return std::make_unique<json>(j); | ||
} | ||
|
||
vector<string> getValuesAsArray(const string& sheetId, const string& range, const string api_key) { | ||
unique_ptr<json> j = getSheetValuesJson(sheetId, range, api_key); | ||
vector<string> v; | ||
if (j == NULL) return v; | ||
if (!j->contains("values") || !(*j)["values"].is_array()) { | ||
return v; | ||
} | ||
for (auto& row : (*j)["values"]) { | ||
if (!row.is_array()) { | ||
continue; | ||
} | ||
for (auto& cell : row) { | ||
v.push_back(cell); | ||
} | ||
} | ||
return v; | ||
} | ||
|
||
vector<string> getValuesAs1DArray(const string& sheetId, const string& range, const string api_key) { | ||
//unique_ptr<json> j = NULL; | ||
unique_ptr<json> j = getSheetValuesJson(sheetId, range, api_key); | ||
Logger::i("downloading names s2"); | ||
|
||
//json j = "{\"range\": \"'The Truth'!A5:A14\",\"majorDimension\" : \"ROWS\",\"values\" : [[\"Ewanye\"],[\"Fra\"],[\"Make\"],[\"Zerumi\"],[\"Pitt\"],[\"Miruna\"],[\"Pv\"],[\"Lynn\"],[\"Lele\"],[\"Rey\"]]}"_json; | ||
|
||
Logger::i("downloading names s3"); | ||
vector<string> v; | ||
Logger::i("downloading names s4"); | ||
if (j == NULL) return v; | ||
Logger::i("downloading names s5"); | ||
if (!j->contains("values") || !(*j)["values"].is_array()) { | ||
return v; | ||
} | ||
for (auto& row : (*j)["values"]) { | ||
if (!row.is_array()) { | ||
continue; | ||
} | ||
for (auto& cell : row) { | ||
v.push_back(cell); | ||
} | ||
} | ||
return v; | ||
} | ||
|
||
vector<vector<string>> getValuesAs2DArray(const string& sheetId, const string& range, const string api_key) { | ||
vector<vector<string>> v; | ||
if(0==0) return v; | ||
unique_ptr<json> j = getSheetValuesJson(sheetId, range, api_key); | ||
if (j == NULL || 0 == 0) return v; | ||
if (!j->contains("values") || !(*j)["values"].is_array()) { | ||
return v; | ||
} | ||
for (auto& row : (*j)["values"]) { | ||
if (!row.is_array()) { | ||
continue; | ||
} | ||
vector<string> sub_vector; | ||
for (auto& cell : row) { | ||
sub_vector.push_back(cell); | ||
} | ||
v.push_back(sub_vector); | ||
} | ||
return v; | ||
} | ||
|
||
void SheetsAPI::downloadNames() { | ||
try { | ||
Logger::d("downloading names"); | ||
const string range = "%27The%20Truth%27%21A5%3AA14"; // "'The Truth'!A5:A14"; | ||
Logger::i("downloaded names s1"); | ||
Logger::d(string_format("ran:%s", range.c_str())); | ||
Logger::d(string_format("key len=%d", api_key.length())); | ||
Logger::d(string_format("sid len=%d", sheetId.length())); | ||
Logger::d(string_format("key=%s", api_key.c_str())); | ||
Logger::d(string_format("sid=%s", sheetId.c_str())); | ||
names_cache = std::make_shared<vector<string>>(getValuesAs1DArray(sheetId, range, api_key)); | ||
Logger::i("downloaded names"); | ||
} catch (std::exception e) { | ||
Logger::e("failed to download names"); | ||
Logger::e(e.what()); | ||
} | ||
} | ||
|
||
void SheetsAPI::downloadWing(int wing) { | ||
try { | ||
Logger::i("downloading wing"); | ||
string range; | ||
switch (wing) { | ||
case 4: | ||
range = "'The Truth'!B22:E31"; | ||
break; | ||
case 5: | ||
range = "'The Truth'!B5:E14"; | ||
break; | ||
case 6: | ||
range = "'The Truth'!G5:I14"; | ||
break; | ||
case 7: | ||
range = "'The Truth'!K5:M14"; | ||
break; | ||
default: | ||
return; | ||
} | ||
roles_cache[wing] = getValuesAs2DArray(sheetId, range, api_key); | ||
Logger::i("downloaded wing"); | ||
} catch (std::exception) { | ||
Logger::e("failed to download wing"); | ||
} | ||
downloading = false; | ||
} | ||
|
||
void SheetsAPI::requestWing(int wing) { | ||
if (downloading) return; | ||
downloading = true; | ||
Logger::i("getting"); | ||
if (names_cache == NULL) { | ||
Logger::i("getting names"); | ||
std::thread t2(&SheetsAPI::downloadNames, this); | ||
//t2.join(); | ||
} | ||
std::thread t1(&SheetsAPI::downloadWing, this, wing); | ||
|
||
|
||
} | ||
|
||
bool SheetsAPI::hasWing(int wing) { | ||
if (names_cache == NULL) return false; | ||
return roles_cache.count(wing) == 1; | ||
} | ||
|
||
vector<vector<string>> SheetsAPI::getWing(int wing) { | ||
return roles_cache[wing]; | ||
} | ||
|
||
shared_ptr<vector<string>> SheetsAPI::getNames() { | ||
return names_cache; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
#include <vector> | ||
#include <string> | ||
#include <map> | ||
#include <memory> | ||
|
||
using std::vector; | ||
using std::string; | ||
using std::map; | ||
using std::shared_ptr; | ||
|
||
class SheetsAPI { | ||
public: | ||
SheetsAPI(); | ||
SheetsAPI(string sheetId, string api_key); | ||
void downloadNames(); | ||
void downloadWing(int wing); | ||
bool hasWing(int wing); | ||
void requestWing(int wing); | ||
vector<vector<string>> getWing(int wing); | ||
shared_ptr<vector<string>> getNames(); | ||
private: | ||
bool downloading; | ||
map<int, vector<vector<string>>> roles_cache; | ||
shared_ptr<vector<string>> names_cache; | ||
const string sheetId; | ||
const string api_key; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SheetsApiTest.cpp : This file contains the 'main' function. Program execution begins and ends there. | ||
// | ||
|
||
#include <iostream> | ||
#include "SheetsAPI.h" | ||
#include "logger.h" | ||
#include <vector> | ||
#include <string> | ||
#include <memory> | ||
|
||
|
||
using std::shared_ptr; | ||
|
||
int main() | ||
{ | ||
std::cout << "Hello World!\n"; | ||
string key = "AIzaSyDXE1rxSTrA_6g0T8ax9goY4MUB4UO5MSg"; | ||
string id = "19AEHyOVnXCzTlVmmKROu7AHLn9NYN7JzjLCNm7KE9Tc"; | ||
Logger::init(""); | ||
SheetsAPI api(id, key); | ||
api.downloadNames(); | ||
shared_ptr<vector<string>> names = api.getNames(); | ||
|
||
std::cout << names->size() << std::endl; | ||
for(string s: *names) { | ||
std::cout << s << std::endl; | ||
} | ||
} | ||
|
||
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu | ||
// Debug program: F5 or Debug > Start Debugging menu | ||
|
||
// Tips for Getting Started: | ||
// 1. Use the Solution Explorer window to add/manage files | ||
// 2. Use the Team Explorer window to connect to source control | ||
// 3. Use the Output window to see build output and other messages | ||
// 4. Use the Error List window to view errors | ||
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project | ||
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<VCProjectVersion>16.0</VCProjectVersion> | ||
<Keyword>Win32Proj</Keyword> | ||
<ProjectGuid>{a323a6ab-255c-4ade-b6c8-c4092ba8bb6f}</ProjectGuid> | ||
<RootNamespace>SheetsApiTest</RootNamespace> | ||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v142</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v142</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<VcpkgUseStatic>true</VcpkgUseStatic> | ||
<VcpkgTriplet> | ||
</VcpkgTriplet> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<ConformanceMode>true</ConformanceMode> | ||
<LanguageStandard>stdcpp17</LanguageStandard> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="logger.cpp" /> | ||
<ClCompile Include="SheetsAPI.cpp" /> | ||
<ClCompile Include="SheetsApiTest.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="json.hpp" /> | ||
<ClInclude Include="logger.h" /> | ||
<ClInclude Include="SheetsAPI.h" /> | ||
<ClInclude Include="string_format.hpp" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<Filter Include="Source Files"> | ||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | ||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||
</Filter> | ||
<Filter Include="Header Files"> | ||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> | ||
</Filter> | ||
<Filter Include="Resource Files"> | ||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | ||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | ||
</Filter> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="SheetsApiTest.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
<ClCompile Include="SheetsAPI.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
<ClCompile Include="logger.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="logger.h"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
<ClInclude Include="SheetsAPI.h"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
<ClInclude Include="string_format.hpp"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
<ClInclude Include="json.hpp"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.