Advertisement
nitestryker

Wallpaper downloader in C

Nov 30th, 2023 (edited)
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8.  
  9. /*
  10.  * wallpaper.c
  11.  *
  12.  * Usage gcc wallaper.c -o wallpaper
  13.  * run ./wallpaper
  14.  * Version: 1.0.3 -
  15.  *
  16.  * Copyright (c) 2023 Nitestryker
  17.  *
  18.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  19.  * of this software and associated documentation files (the "Software"), to deal
  20.  * in the Software without restriction, including without limitation the rights
  21.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22.  * copies of the Software, and to permit persons to whom the Software is
  23.  * furnished to do so, subject to the following conditions:
  24.  *
  25.  * The above copyright notice and this permission notice shall be included in all
  26.  * copies or substantial portions of the Software.
  27.  *
  28.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34.  * SOFTWARE.
  35.  */
  36.  
  37. #define WALLPAPER_VERSION "1.0.3"
  38. #define AUTHOR_NAME "Nitestryker"
  39.  
  40. // Color codes for the text
  41. #define HEADER "\033[95m"
  42. #define OKBLUE "\033[94m"
  43. #define OKGREEN "\033[92m"
  44. #define WARNING "\033[93m"
  45. #define FAIL "\033[91m"
  46. #define ENDC "\033[0m"
  47. #define BOLD "\033[1m"
  48. #define UNDERLINE "\033[4m"
  49.  
  50. #define MAX_INPUT_LENGTH 100
  51.  
  52. #ifdef _WIN32
  53. #include <windows.h>
  54. #include <Lmcons.h>
  55. #else
  56. #include <sys/stat.h>
  57. #include <unistd.h>
  58. #endif
  59.  
  60. bool directoryExists(const char *path) {
  61.     struct stat statbuf;
  62.     return (stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode));
  63. }
  64.  
  65. void createDirectory(const char *path) {
  66. #ifdef _WIN32
  67.     CreateDirectory(path, NULL);
  68. #else
  69.     mkdir(path, 0700); // Read, write, execute permissions for the user
  70. #endif
  71. }
  72.  
  73. void getUserInput(char *input, int length) {
  74.     printf("Please enter your input: ");
  75.     if (fgets(input, length, stdin) != NULL) {
  76.         size_t len = strlen(input);
  77.         if (len > 0 && input[len - 1] == '\n') {
  78.             input[len - 1] = '\0';
  79.         }
  80.     } else {
  81.         fprintf(stderr, "Error reading input.\n");
  82.     }
  83. }
  84.  
  85. static char picturesPath[256]; // Static variable to store the pictures path
  86.  
  87. int initializePicturesDirectory() {
  88. #ifdef _WIN32
  89.     char username[UNLEN + 1];
  90.     DWORD username_len = UNLEN + 1;
  91.     GetUserName(username, &username_len);
  92.     snprintf(picturesPath, sizeof(picturesPath), "C:\\Users\\%s\\Pictures", username);
  93. #else
  94.     const char *homeDir = getenv("HOME");
  95.     if (homeDir == NULL) {
  96.         fprintf(stderr, "Unable to find home directory.\n");
  97.         return 1;
  98.     }
  99.     snprintf(picturesPath, sizeof(picturesPath), "%s/Pictures", homeDir);
  100. #endif
  101.  
  102.     if (!directoryExists(picturesPath)) {
  103.         printf("The Pictures directory does not exist. Creating one...\n");
  104.         createDirectory(picturesPath);
  105.         if (!directoryExists(picturesPath)) {
  106.             fprintf(stderr, "Failed to create Pictures directory.\n");
  107.             return 1;
  108.         }
  109.         printf("Pictures directory created successfully.\n");
  110.     } else {
  111.         printf(OKBLUE "Found Pictures directory: %s\n" ENDC, picturesPath);
  112.     }
  113.  
  114.     char downloadHistoryPath[256];
  115.     snprintf(downloadHistoryPath, sizeof(downloadHistoryPath), "%s/download_history.txt", picturesPath);
  116.     FILE *downloadHistoryFile = fopen(downloadHistoryPath, "w");
  117.     if (downloadHistoryFile != NULL) {
  118.         printf("Download history file created successfully.\n");
  119.         fclose(downloadHistoryFile);
  120.     } else {
  121.         fprintf(stderr, "Failed to create download history file.\n");
  122.         return 1;
  123.     }
  124.  
  125.     return 0; // Success
  126. }
  127.  
  128. void printAppInfo() {
  129.     printf(OKGREEN "Wallpaper v%s\n" ENDC, WALLPAPER_VERSION);
  130.     printf(HEADER "Created by %s\n" ENDC, AUTHOR_NAME);
  131. }
  132.  
  133. void clearConsole() {
  134. #ifdef _WIN32
  135.     system("cls");
  136. #else
  137.     system("clear");
  138. #endif
  139. }
  140.  
  141. int initializeApp() {
  142.     return initializePicturesDirectory();
  143. }
  144.  
  145. void getUserPreferences(char *resolution, char *type, char *keyword, int *number, int *sleepDuration) {
  146.     printf("Enter desired wallpaper resolution in 1920x1080 format: ");
  147.     getUserInput(resolution, MAX_INPUT_LENGTH);
  148.  
  149.     printf("Enter your desired wallpaper type (keyword/random): ");
  150.     getUserInput(type, MAX_INPUT_LENGTH);
  151.  
  152.     if (strcmp(type, "keyword") == 0) {
  153.         printf("Please enter your desired wallpaper keyword: ");
  154.         getUserInput(keyword, MAX_INPUT_LENGTH);
  155.     } else {
  156.         strcpy(keyword, "");
  157.     }
  158.  
  159.     char numberInput[MAX_INPUT_LENGTH];
  160.     printf("Enter the number of wallpapers you want to download: ");
  161.     getUserInput(numberInput, MAX_INPUT_LENGTH);
  162.     *number = atoi(numberInput);
  163.  
  164.     char sleepInput[MAX_INPUT_LENGTH];
  165.     printf("enter time to sleep between downloads in seconds: ");
  166.     getUserInput(sleepInput, MAX_INPUT_LENGTH);
  167.     *sleepDuration = atoi(sleepInput);
  168. }
  169.  
  170. void downloadWallpaper(int *number, int sleepDuration, const char *type, const char *keyword, const char *path, const char *resolution) {
  171.     char downloadHistoryPath[256];
  172.     snprintf(downloadHistoryPath, sizeof(downloadHistoryPath), "%s/download_history.txt", path);
  173.     FILE *downloadHistoryFile = fopen(downloadHistoryPath, "a+");
  174.  
  175.     if (downloadHistoryFile == NULL) {
  176.         fprintf(stderr, "Failed to open download history file.\n");
  177.         return;
  178.     }
  179.  
  180.     char line[1024];
  181.     int downloadedCount = 0;
  182.     while (downloadedCount < *number) {
  183.         char url[256];
  184.         char imageName[256];
  185.         char command[512];
  186.         char hashCommand[512];
  187.         char hashValue[128];
  188.  
  189.         // Generate URL based on type
  190.         if (strcmp(type, "keyword") == 0) {
  191.             snprintf(url, sizeof(url), "https://source.unsplash.com/featured/%s/?%s", resolution, keyword);
  192.         } else {
  193.             snprintf(url, sizeof(url), "https://source.unsplash.com/random/%s", resolution);
  194.         }
  195.  
  196.         // Generate a unique name for the image
  197.         snprintf(imageName, sizeof(imageName), "%s/wallpaper_%d.jpg", path, downloadedCount + 1);
  198.  
  199.         // Download the image using curl with -L to follow redirects (must have the -L flag)
  200.         snprintf(command, sizeof(command), "curl -L -o \"%s\" \"%s\"", imageName, url);
  201.         int result = system(command);
  202.  
  203.         if (result != 0) {
  204.             fprintf(stderr, "Error occurred during downloading. Exit code: %d\n", result);
  205.             continue; // Skip further processing for this download
  206.         }
  207.  
  208.         // Generate a unique identifier for the image (hash)
  209.         snprintf(hashCommand, sizeof(hashCommand), "md5sum \"%s\" | cut -d ' ' -f 1", imageName);
  210.         FILE *hashPipe = popen(hashCommand, "r");
  211.         if (hashPipe == NULL) {
  212.             fprintf(stderr, "Failed to compute hash for the image.\n");
  213.             remove(imageName); // Remove the downloaded image
  214.             break;
  215.         }
  216.         fgets(hashValue, sizeof(hashValue), hashPipe);
  217.         pclose(hashPipe);
  218.  
  219.         // Check if the hash is already in download history
  220.         bool alreadyDownloaded = false;
  221.         rewind(downloadHistoryFile);
  222.         while (fgets(line, sizeof(line), downloadHistoryFile)) {
  223.             if (strstr(line, hashValue) != NULL) {
  224.                 alreadyDownloaded = true;
  225.                 break;
  226.             }
  227.         }
  228.  
  229.         if (!alreadyDownloaded) {
  230.             // Add the hash to download history
  231.             fprintf(downloadHistoryFile, "%s\n", hashValue);
  232.             fflush(downloadHistoryFile);
  233.  
  234.             printf("Downloaded wallpaper %d/%d\n", downloadedCount + 1, *number);
  235.             downloadedCount++;
  236.  
  237.             // Sleep between downloads
  238.             sleep(sleepDuration);
  239.         } else {
  240.             printf("Skipping already downloaded image.\n");
  241.             remove(imageName); // Remove the duplicate image
  242.         }
  243.     }
  244.  
  245.     fclose(downloadHistoryFile);
  246. }
  247.  
  248.  
  249. void performWallpaperDownload(const char *resolution, const char *type, const char *keyword, int number, int sleepDuration) {
  250.     time_t start = time(NULL);
  251.     downloadWallpaper(&number, sleepDuration, type, keyword, picturesPath, resolution);
  252.     time_t end = time(NULL);
  253.     printf("Time taken: %.2fs\n", difftime(end, start));
  254. }
  255.  
  256. int main() {
  257.     printAppInfo();
  258.     sleep(2);
  259.     clearConsole();
  260.  
  261.     if (initializeApp() != 0) {
  262.         fprintf(stderr, "Failed to initialize application.\n");
  263.         return 1;
  264.     }
  265.  
  266.     char resolution[MAX_INPUT_LENGTH];
  267.     char type[MAX_INPUT_LENGTH];
  268.     char keyword[MAX_INPUT_LENGTH];
  269.     int number, sleepDuration;
  270.  
  271.     getUserPreferences(resolution, type, keyword, &number, &sleepDuration);
  272.     performWallpaperDownload(resolution, type, keyword, number, sleepDuration);
  273.  
  274.     return 0;
  275. }
  276.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement