Skip to content

Instantly share code, notes, and snippets.

@vittodevit
Created November 19, 2020 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vittodevit/137e8771691f8e24237b1240c4bc1feb to your computer and use it in GitHub Desktop.
Save vittodevit/137e8771691f8e24237b1240c4bc1feb to your computer and use it in GitHub Desktop.
/*
AUTORE: Vittorio Lo Mele
DATA: 19/11/2020
DESCRIZIONE: Programma per l'apertura automatica dei link delle videolezioni
in base all'orario del computer.
FORMATO CSV ORARIO PER RIGA: Giorno Settimana,Ora Inizio in secondi,Ora fine in secondi,link
ESEMPIO RIGA: LUN,29100,32100,https://example.com
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
#include <cstring>
using namespace std;
string convertiGiornoSettimana(int giorno);
int main()
{
//apri file dell'orario
ifstream fileOrario;
fileOrario.open("orario.csv");
if(!fileOrario.is_open()){
cout << "Errore apertura file orario" << endl;
return 1;
}
//ottieni informazioni su data ed ora
time_t dataSistema = time(0);
tm* dataoraLocale = localtime(&dataSistema);
//converti in informazioni leggibili dal programma
string giornoSettimana = convertiGiornoSettimana(dataoraLocale->tm_wday);
int minutiaSecondi = dataoraLocale->tm_hour * 3600;
int oreaSecondi = dataoraLocale->tm_min * 60;
int secondiDallaMezzanotte = oreaSecondi + minutiaSecondi;
//cerca nel file
string rigaEntry;
bool trovato = false;
while (getline(fileOrario, rigaEntry)) {
istringstream streamRigaEntry(rigaEntry);
string buffer, entryGiornoSettimana, entryLink;
int entryIntervalloInizio, entryIntervalloFine;
int indice = 0;
//imposta come delimitatore la virgola (lavoriamo su CSV) e sposta i vari parametri nelle loro variabili
while (getline(streamRigaEntry, buffer, ',')) {
//in base alla posizione nella riga sposta il rispettivo dato nella sua variabile
switch (indice) {
case 0:
entryGiornoSettimana = buffer;
break;
case 1:
entryIntervalloInizio = stoi(buffer);
break;
case 2:
entryIntervalloFine = stoi(buffer);
break;
case 3:
entryLink = buffer;
break;
}
indice++;
}
//controlla se la riga corrisponde alla data ed ora sistema
if (giornoSettimana == entryGiornoSettimana) {
//controlla se il timestamp attuale è compreso nell'intervallo del csv
if (secondiDallaMezzanotte > entryIntervalloInizio && secondiDallaMezzanotte < entryIntervalloFine) {
//link trovato!
trovato = true;
//lancia link
cout << "Link " << entryLink << " trovato per l'ora attuale. Aprendo con il browser di default...";
//compatibilità
#ifdef __linux__
string comando = "x-www-browser " + entryLink;
#elif _WIN32
string comando = "start " + entryLink;
#else
#error Unsupported OS
#endif
const int lunghezzaComando = comando.length();
char comandosistema[lunghezzaComando + 1];
strcpy(comandosistema, comando.c_str());
system(comandosistema);
}
}
}
if (!trovato) {
cout << "Nessun link trovato per l'ora attuale :(" << endl;
}
return 0;
}
/*
Funzione per convertire l'output della data di sistema in un giorno della settimana
leggibile da un umano
*/
string convertiGiornoSettimana(int giorno) {
switch (giorno) {
case 0:
return "DOM";
break;
case 1:
return "LUN";
break;
case 2:
return "MAR";
break;
case 3:
return "MER";
break;
case 4:
return "GIO";
break;
case 5:
return "VEN";
break;
case 6:
return "SAB";
break;
default:
return "BOH";
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment