scripts and tools to administer the lingy.in public unix / tilde
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.7KB

  1. #!/usr/bin/perl
  2. binmode STDOUT, ":utf8";
  3. use warnings;
  4. use strict;
  5. use JSON;
  6. my $THREAD_NO;
  7. my $URL_PREFIX;
  8. my $FN;
  9. my $OUT_DIR;
  10. my %jh;
  11. my @a1;
  12. $URL_PREFIX = "https://lainchan.org/lit/src/";
  13. $THREAD_NO = 4953;
  14. $FN = "$THREAD_NO.json";
  15. $OUT_DIR = "./dl/";
  16. # Read JSON with list of files
  17. open FILE, "<", $FN or die "could not open file";
  18. do{
  19. my $json_str;
  20. local $/=undef;
  21. $json_str = <FILE>;
  22. chomp $json_str;
  23. %jh = %{JSON->new()->decode($json_str)};
  24. };
  25. close FILE;
  26. # anonymous function that returns a list
  27. # of tuples of the below form:
  28. # (file_name, file_url)
  29. @a1 = sub{
  30. my @a0;
  31. my @a2;
  32. my $f1;
  33. # filters for file types we
  34. # dont want to downloads
  35. sub f1 {
  36. return $_[0]->{"ext"} && !($_[0]->{"ext"} =~ /jpe?g/);
  37. }
  38. sub f2 {
  39. return !($_[0]->{"ext"} =~ /png/);
  40. }
  41. sub f3 {
  42. return !($_[0]->{"ext"} =~ /gif/);
  43. }
  44. sub f4 {
  45. return !($_[0]->{"ext"} =~ /webm/);
  46. }
  47. sub f0 {
  48. return f1($_[0]) && f2($_[0]) && f3($_[0]) && f4($_[0])
  49. }
  50. # create an array of files
  51. # that meet our file ext requirement
  52. @a0 = grep {f0($_)} @{$jh{"posts"}};
  53. # do the same filter on the
  54. # extra_files attribute
  55. @a2 = grep {
  56. f0($_)
  57. } map {
  58. @{$_->{"extra_files"}}
  59. } grep {
  60. $_->{"extra_files"}
  61. } @{$jh{"posts"}};
  62. # Return our tuple
  63. return map {
  64. [
  65. sprintf("%s%s", $_->{"filename"}, $_->{"ext"}), # file_name
  66. sprintf("%s%s%s", $URL_PREFIX, $_->{"tim"}, $_->{"ext"}) # file_url
  67. ]
  68. } (@a0, @a2);
  69. }->();
  70. # Print a list of wget commands from our tuples
  71. for my $i1 (@a1){
  72. printf("wget -N %s -O '%s%s'\n", scalar $i1->[1], $OUT_DIR, scalar $i1->[0]);
  73. }