Skip to content

Commit

Permalink
Allowed re-download of URL list, limited download to user's input, ex…
Browse files Browse the repository at this point in the history
…isting name will be cached
  • Loading branch information
Bukk94 committed Mar 23, 2022
1 parent b55e169 commit 458c9f1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
4 changes: 2 additions & 2 deletions CoubDownloader/CoubDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<ApplicationIcon>icon.ico</ApplicationIcon>
<Company>BearSoft</Company>
<Product>Coub Downloader</Product>
<AssemblyVersion>0.5</AssemblyVersion>
<FileVersion>0.5</FileVersion>
<AssemblyVersion>0.6</AssemblyVersion>
<FileVersion>0.6</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
30 changes: 21 additions & 9 deletions CoubDownloader/Crawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void CrawlUrls(string[] categories)

private void DownloadChannelCoubs(string channel)
{
if (AlreadyDownloaded(channel))
if (ShouldSkipDownloaded(channel))
{
return;
}
Expand Down Expand Up @@ -85,23 +85,35 @@ private void ShouldCrawlSegments()
}
}

private bool AlreadyDownloaded(string dir)
private bool ShouldSkipDownloaded(string dir)
{
var urlsPath = GetDataPath(dir, Constants.UrlListFileName);

if (File.Exists(urlsPath))
if (!File.Exists(urlsPath))
{
Console.WriteLine($"URL list for '{dir}' found! Skipping crawling.");
return true;
// No file exists for this directory so no skip
return false;
}

return false;
var urlsCount = File.ReadLines(urlsPath).Count();
Console.WriteLine($"Found URL list for '{dir}' with {urlsCount} links!");
Console.WriteLine("Download the list again to get newest changes? Original list will be deleted, but already downloaded coubs will remain unchanged.");
Console.Write("Type Y for yes or N for no (then press enter): ");
var answer = Console.ReadLine()?.ToLower();
if (answer == "y" || answer == "yes")
{
// User wants new crawl, remove original file
File.Delete(urlsPath);
return false;
}

return true;
}

private void DownloadLikedCoubs()
{
var dir = LikedCategory;
if (AlreadyDownloaded(dir))
if (ShouldSkipDownloaded(dir))
{
return;
}
Expand Down Expand Up @@ -139,7 +151,7 @@ private void DownloadLikedCoubs()
private void DownloadBookmarkedCoubs()
{
var dir = BookmarksCategory;
if (AlreadyDownloaded(dir))
if (ShouldSkipDownloaded(dir))
{
return;
}
Expand Down Expand Up @@ -198,7 +210,7 @@ private string GetAccessToken()
return _usersAccessToken;
}

Console.WriteLine("Write/paste your access token. Read README if you don't know how to get it");
Console.WriteLine("Write/paste your access token. Read README if you don't know how to get it.");
Console.Write("Access Token: ");
var token = Console.ReadLine();
token = token?.Replace("remember_token=", "").Trim();
Expand Down
10 changes: 6 additions & 4 deletions CoubDownloader/Downloader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;

namespace CoubDownloader
{
Expand All @@ -8,13 +9,14 @@ public class Downloader
private const string DownloadedArchive = "downloaded.txt";
private string coubsDir = Path.Combine(Environment.CurrentDirectory, Constants.CoubDataDir);

public void DownloadCoubs(string path)
public void DownloadCoubs(string infoPath, string[] dirs)
{
var directoriesToDownload = Directory.GetDirectories(path);
var directoriesToDownload = dirs.Any() ? dirs : Directory.GetDirectories(infoPath);

foreach (var directory in directoriesToDownload)
{
var dir = new DirectoryInfo(directory).Name;
if (!HasUrlList(path, dir))
if (!HasUrlList(infoPath, dir))
{
Console.WriteLine($"No URL list for category '{dir}' found, skipping download...");
continue;
Expand All @@ -24,7 +26,7 @@ public void DownloadCoubs(string path)
}
}

private bool HasUrlList(string path, string dir)
private static bool HasUrlList(string path, string dir)
{
var filename = Path.Combine(Path.Combine(path, dir), Constants.UrlListFileName);
return File.Exists(filename);
Expand Down
15 changes: 8 additions & 7 deletions CoubDownloader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CoubDownloader
{
public class Program
{
private const string Version = "0.5";
private const string Version = "0.6";

public static void Main()
{
Expand Down Expand Up @@ -47,20 +47,21 @@ private static void GetCoubs(string input)
var crawler = new Crawler();
var downloader = new Downloader();

if (string.IsNullOrWhiteSpace(input))
var toDownload = input?.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x))
.ToArray() ?? Array.Empty<string>();

if (!toDownload.Any())
{
Console.Error.WriteLine("No user input, looking for already downloaded links...");
}
else
{
var toDownload = input.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.ToArray();

crawler.CrawlUrls(toDownload);
}

downloader.DownloadCoubs(crawler.InfoPath);
downloader.DownloadCoubs(crawler.InfoPath, toDownload);
}
}
}
2 changes: 2 additions & 0 deletions CoubDownloader/coub_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,8 @@ def main():
clean()
done += 1
skipped += 1
if opts.archive_file:
write_archive(c_id)
continue

if opts.sleep_dur and count > 1:
Expand Down

0 comments on commit 458c9f1

Please sign in to comment.