Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
nak0823 committed Nov 25, 2023
1 parent 4e18c0d commit 97bd732
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 0 deletions.
106 changes: 106 additions & 0 deletions CCTV/CameraSearcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Leaf.xNet;
using System.Drawing;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;

namespace OpenCCTV.CCTV
{
internal class CameraSearcher
{
/// <summary>
/// Finds Cameras with ease.
/// </summary>
/// <param name="countryCode">The country code</param>
public static void FindCameras(string countryCode)
{
Console.Clear();
Console.Title = $"CCTV Browser by Serialized - Searching Cameras in {countryCode}";
Interface.InterfaceMaker.PrintLogo();

List<string> camerasList = new List<string>();

HttpRequest hr = new HttpRequest()
{
IgnoreProtocolErrors = true,
};

hr.SslCertificateValidatorCallback += (obj, cert, ssl, error) => (cert as X509Certificate2).Verify();

try
{
int lastPage = GetLastPage(hr, countryCode);

for (int i = 0; i < lastPage; i++)
{
string[] cameras = GetCameras(hr, countryCode, i);

for (int j = 0; j < cameras.Length; j++)
{
camerasList.Add(cameras[j]);
Interface.InterfaceMaker.WriteLine(Color.White, $"{countryCode}-{camerasList.Count}", $"{cameras[j]}\n");
}
}

Console.WriteLine();
Interface.InterfaceMaker.WriteLine(Color.White, "A", $"Export All Cameras\n");
Interface.InterfaceMaker.WriteLine(Color.White, "B", $"Open All Cameras\n");
Interface.InterfaceMaker.WriteLine(Color.White, "C", $"Back\n");
Interface.InterfaceMaker.WriteLine(Color.White, ">", $"");

string userInput = Console.ReadLine();

switch (userInput)
{
case "A":
File.WriteAllLines($"{countryCode}.txt", camerasList);
Interface.InterfaceMaker.WriteLine(Color.White, "~", "Exported All Cameras!\n");
Utils.CountryMenu();
break;

case "B":
foreach (string cameras in camerasList)
Program.OpenWebpage(cameras);

break;

case "C":
Utils.CountryMenu();
break;
}
}
catch (Exception ex)
{
Interface.InterfaceMaker.WriteLine(Color.White, "Error", "Couldn't Obtain Cameras.\n");
}
}

/// <summary>
/// Checks how many pages there are to check
/// </summary>
/// <param name="hr">The HttpRequest</param>
/// <param name="country">The country</param>
/// <returns>The number of pages</returns>
private static int GetLastPage(HttpRequest hr, string country)
{
var camResp = hr.Get($"http://www.insecam.org/en/bycountry/{country}");
var lastPageMatch = Regex.Match(camResp.ToString(), @"pagenavigator\(""\?page="", (\d+)");
return lastPageMatch.Success ? int.Parse(lastPageMatch.Groups[1].Value) : 0;
}

/// <summary>
/// Gets the list of open cameras.
/// </summary>
/// <param name="hr"></param>
/// <param name="country"></param>
/// <param name="page"></param>
/// <returns>A list of cameras</returns>
private static string[] GetCameras(HttpRequest hr, string country, int page)
{
var camResp = hr.Get($"http://www.insecam.org/en/bycountry/{country}/?page={page}");
return Regex.Matches(camResp.ToString(), @"http://\d+.\d+.\d+.\d+:\d+")
.Cast<Match>()
.Select(match => match.Value)
.ToArray();
}
}
}
49 changes: 49 additions & 0 deletions CCTV/CountrySearcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Leaf.xNet;
using Newtonsoft.Json;
using System.Drawing;
using System.Security.Cryptography.X509Certificates;

namespace OpenCCTV.CCTV
{
internal class CountrySearcher
{
/// <summary>
/// Displays the amount of cameras per country.
/// </summary>
public static void DisplayCountryInfo()
{
HttpRequest hr = new HttpRequest()
{
IgnoreProtocolErrors = true,
};

hr.SslCertificateValidatorCallback += (obj, cert, ssl, error) => (cert as X509Certificate2).Verify();

try
{
HttpResponse countryResp = hr.Get("http://www.insecam.org/en/jsoncountries/");
string countryJson = countryResp.ToString();
var countryData = JsonConvert.DeserializeObject<Utils.Country>(countryJson);

if (countryData != null && countryData.Countries != null)
{
foreach (var country in countryData.Countries)
{
if (country.Value.Country == "-") continue;

Interface.InterfaceMaker.PrintCountryInfo(
country.Key.PadRight(2),
country.Value.Country.PadRight(25),
country.Value.Count);
}
}

Console.WriteLine();
}
catch (Exception ex)
{
Interface.InterfaceMaker.WriteLine(Color.White, "Error", "Couldn't obtain Country Statistics.\n");
}
}
}
}
55 changes: 55 additions & 0 deletions CCTV/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Drawing;

namespace OpenCCTV.CCTV
{
/// <summary>
/// Utility class containing various visible output methods.
/// </summary>
internal class Utils
{
/// <summary>
/// Menu that helps displaying country statistics and user input.
/// </summary>
public static void CountryMenu()
{
SetConsoleTitle("CCTV Browser by Serialized - Displaying Information By Country");
Interface.InterfaceMaker.WriteLine(Color.White, "~", "Retrieving Country Statistics\n\n");

/* Display the fetched information. */
CountrySearcher.DisplayCountryInfo();

Interface.InterfaceMaker.WriteLine(Color.White, "~", "Enter Your Wanted Country\n");
Interface.InterfaceMaker.WriteLine(Color.White, ">", "");

Console.ForegroundColor = ConsoleColor.White;
string countryInput = Console.ReadLine();
CameraSearcher.FindCameras(countryInput);
}

/// <summary>
/// Set Console.Title and Flush Console.
/// </summary>
/// <param name="title">The consoles title.</param>
private static void SetConsoleTitle(string title)
{
Console.Clear();
Console.Title = title;
Interface.InterfaceMaker.PrintLogo();
}

/// <summary>
/// Deserializable class holding Country Information
/// </summary>
public class Country
{
public string? Status { get; set; }
public Dictionary<string, CountryInfo>? Countries { get; set; }
}

public class CountryInfo
{
public string? Country { get; set; }
public int Count { get; set; }
}
}
}
119 changes: 119 additions & 0 deletions Interface/InterfaceMaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System.Drawing;
using Console = Colorful.Console;

namespace OpenCCTV.Interface
{
/// <summary>
/// Provides methods for the interface.
/// </summary>
public class InterfaceMaker
{
/// <summary>
/// Represents the ASCII 'header' for the application.
/// </summary>
private static readonly string[] _asciiLogo =
{
" ",
" ██████╗ ██████╗ ███████╗███╗ ██╗ ██████╗ ██████╗████████╗██╗ ██╗",
"██╔═══██╗██╔══██╗██╔════╝████╗ ██║██╔════╝██╔════╝╚══██╔══╝██║ ██║",
"██║ ██║██████╔╝█████╗ ██╔██╗ ██║██║ ██║ ██║ ██║ ██║",
"██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║██║ ██║ ██║ ╚██╗ ██╔╝",
"╚██████╔╝██║ ███████╗██║ ╚████║╚██████╗╚██████╗ ██║ ╚████╔╝ ",
" ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ",
" ",
};

/// <summary>
/// Represents the colors for the ASCII logo.
/// </summary>
private static readonly List<Color> _colorList = new List<Color>()
{
Color.White,
ColorTranslator.FromHtml("#FCEE2B"),
ColorTranslator.FromHtml("#E2D626"),
ColorTranslator.FromHtml("#C8BD21"),
ColorTranslator.FromHtml("#AFA51D"),
ColorTranslator.FromHtml("#958C18"),
ColorTranslator.FromHtml("#7B7413"),
};

/// <summary>
/// Prints the ASCII logo to the console with customizable colors (scheme set to yellow -> black).
/// </summary>
public static void PrintLogo()
{
for (int i = 0; i < _asciiLogo.Length - 1; i++)
{
Console.SetCursorPosition(Console.WindowWidth / 2 - _asciiLogo[0].Length / 2, i);
Console.WriteLine(_asciiLogo[i], _colorList[i]);

switch (i)
{
case 1:
Colors.Primary = _colorList[i];
break;

case 2:
Colors.Secondary = _colorList[i];
break;
}
}

// Bottom padding.
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
}

/// <summary>
/// Override method to display formatted and colorized WriteLine.
/// </summary>
/// <param name="color">The color of the message.</param>
/// <param name="prefix">The prefix of the message.</param>
/// <param name="msg">The message to be displayed.</param>
public static void WriteLine(Color color, string prefix, string msg)
{
Console.Write(" [", Colors.Secondary);
Console.Write(prefix, Color.White);
Console.Write("]", Colors.Secondary);
Console.Write("", Color.White);
Console.Write(msg, color);
}

/// <summary>
/// Method to print country information formatted and colorized.
/// </summary>
/// <param name="countryCode">The two characters to identify a country.</param>
/// <param name="countryName">The name of the country.</param>
/// <param name="count">The count of cameras.</param>
public static void PrintCountryInfo(string countryCode, string countryName, int count)
{
Console.Write(" [", Colors.Secondary);
Console.Write($"{countryCode}", Color.White);
Console.Write("]", Colors.Secondary);
Console.Write("", Color.White);
Console.Write($"{countryName}", Colors.Primary);
Console.Write("", Color.White);

switch (count == 1)
{
case true:
Console.WriteLine(count + " Camera", Color.White);
break;

case false:
Console.WriteLine(count + " Cameras", Color.White);
break;
}
}
}

/// <summary>
/// Provides Color variables for the interface.
/// </summary>
public class Colors
{
public static Color Primary { get; set; }
public static Color Secondary { get; set; }
}
}
16 changes: 16 additions & 0 deletions OpenCCTV.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="Leaf.xNet" Version="5.2.10" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions OpenCCTV.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34202.233
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenCCTV", "OpenCCTV.csproj", "{6E7AA6DD-E5E8-42C4-ADE8-A25C39A36C18}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6E7AA6DD-E5E8-42C4-ADE8-A25C39A36C18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E7AA6DD-E5E8-42C4-ADE8-A25C39A36C18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E7AA6DD-E5E8-42C4-ADE8-A25C39A36C18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E7AA6DD-E5E8-42C4-ADE8-A25C39A36C18}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5A07CEC4-DDD4-4118-AD8F-66F2A66E457C}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 97bd732

Please sign in to comment.