-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamFlix.cpp
More file actions
164 lines (130 loc) · 4.82 KB
/
StreamFlix.cpp
File metadata and controls
164 lines (130 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "StreamFlix.h"
#include <fstream>
#include <iostream>
#include <mutex>
#include <regex>
#include <thread>
#include <bits/ostream.tcc>
#include <TMDBServiceProvider.h>
#include <json/single_include/nlohmann/json.hpp>
#include "MovieDatabase.h"
using json = nlohmann::json;
void StreamFlix::DisplayMovies(const std::string& title, const MovieDatabase& movieDatabase)
{
std::cout << "______________________________________________________" << std::endl;
std::cout << title << std::endl;
std::cout << "______________________________________________________" << std::endl;
const MovieList& movies = movieDatabase.GetMovies();
for (auto& movie : movies)
{
std::cout << movie.GetTitle() << " | " << movie.GetRating() << std::endl;
}
}
void StreamFlix::DisplayMoviesSortedByTitle(const std::string& title, const MovieDatabase& movieDatabase)
{
std::cout << "______________________________________________________" << std::endl;
std::cout << title << " (Sorted Alphabetically)" << std::endl;
std::cout << "______________________________________________________" << std::endl;
const MovieList& moviesSortedByTitle = movieDatabase.GetMoviesSortedByTitle();
for (auto& movie : moviesSortedByTitle)
{
std::cout << movie.GetTitle() << " | " << movie.GetRating() << std::endl;
}
}
void StreamFlix::DisplayMoviesSortedByRating(const std::string& title, const MovieDatabase& movieDatabase)
{
std::cout << "______________________________________________________" << std::endl;
std::cout << title << " (Sorted by rating)" << std::endl;
std::cout << "______________________________________________________" << std::endl;
const MovieList& moviesSortedByRating = movieDatabase.GetMoviesSortedByRating();
for (auto& movie : moviesSortedByRating)
{
std::cout << movie.GetTitle() << " | " << movie.GetRating() << std::endl;
}
}
std::string StreamFlix::LoadAPIKeyFromJson(const char* str)
{
std::ifstream file(str);
try
{
if (!file.is_open())
{
std::cerr << "Error opening file: " << str << std::endl;
return "";
}
json jsonData;
file >> jsonData;
file.close();
auto apiKey = jsonData.at("api_key").get<std::string>();
return apiKey;
}
catch (const json::exception& e)
{
std::cerr << "Error parsing JSON: " << e.what() << std::endl;
file.close();
return "";
}
}
void StreamFlix::Run()
{
MovieDatabase popularMovies;
MovieDatabase nowPlayingMovies;
static std::string TMDB_API_KEY = LoadAPIKeyFromJson("api_key.json");
TMDBServiceProvider tmdbServiceProvider(TMDB_API_KEY);
json popularMovieJson;
json nowPlayingMovieJson;
std::mutex coutMutex;
std::thread popularThread([&]
{
{
std::lock_guard lock(coutMutex);
std::cout << "Popular movies thread ID: " << std::this_thread::get_id() << std::endl;
}
for (int i = 1; i <= 5; ++i)
{
auto popularMoviesJsonString = tmdbServiceProvider.GetPopularMovies(i);
popularMovieJson = json::parse(popularMoviesJsonString);
for (auto& movie : popularMovieJson["results"])
{
popularMovies.AddMovie(movie["title"], movie["vote_average"]);
}
}
});
std::thread nowPlayingThread([&]
{
{
std::lock_guard lock(coutMutex);
std::cout << "Now playing movies thread ID: " << std::this_thread::get_id() << std::endl;
}
auto nowPlayingMoviesJsonString = tmdbServiceProvider.GetNowPlayingMovies(1);
nowPlayingMovieJson = json::parse(nowPlayingMoviesJsonString);
for (auto& movie : nowPlayingMovieJson["results"])
{
nowPlayingMovies.AddMovie(movie["title"], movie["vote_average"]);
}
});
popularThread.join();
nowPlayingThread.join();
DisplayMovies("POPULAR", popularMovies);
DisplayMoviesSortedByTitle("POPULAR", popularMovies);
DisplayMoviesSortedByRating("POPULAR", popularMovies);
DisplayMovies("NOW PLAYING", nowPlayingMovies);
DisplayMoviesSortedByTitle("NOW PLAYING", nowPlayingMovies);
DisplayMoviesSortedByRating("NOW PLAYING", nowPlayingMovies);
std::regex pattern(".*[dD]es.*");
MovieList matchingMovies;
const auto& movies = popularMovies.GetMovies();
std::copy_if(movies.begin(), movies.end(), std::back_inserter(matchingMovies),
[&pattern](const Movie& movie)
{
return std::regex_search(movie.GetTitle(), pattern);
});
for (auto& movie : matchingMovies)
{
std::cout << "Matching movie: " << movie.GetTitle() << std::endl;
}
}
void StreamFlix::Shutdown()
{
std::cout << std::endl << "Bye Bye" << std::endl;
}