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.

118 lines
2.1KB

  1. #!/usr/bin/perl
  2. binmode STDOUT, ":utf8";
  3. use warnings;
  4. use strict;
  5. use JSON;
  6. my $DOMAIN;
  7. my $THREAD_NO;
  8. my $thread_no;
  9. my $URL_PREFIX;
  10. my $fn;
  11. my $OUT_DIR;
  12. my $USAGE;
  13. my $ACMD;
  14. my %jh;
  15. my @a1;
  16. $DOMAIN = "lainchan.org";
  17. $URL_PREFIX = "https://$DOMAIN/lit/res/";
  18. $OUT_DIR = "./dl/";
  19. $USAGE = "Usage: bookdl.pl [http://someurl/]thread_id";
  20. # die if no arguments
  21. die "$USAGE" unless scalar @ARGV > 0;
  22. $ACMD = $ARGV[0];
  23. ($thread_no) = $ACMD =~ /.*\/([0-9]+).{0,5}/;
  24. # die if didnt provide thread number
  25. die "$USAGE" unless $thread_no;
  26. # Download the JSON from lainchan with list
  27. # of book filenames
  28. do {
  29. my $url = $URL_PREFIX.$thread_no.".json";
  30. printf("%s\n", $url);
  31. `wget $url -O $thread_no.json`;
  32. };
  33. $fn = "$thread_no.json";
  34. # Read JSON with list of files
  35. open FILE, "<", $fn or die "could not open file";
  36. do{
  37. my $json_str;
  38. local $/=undef;
  39. $json_str = <FILE>;
  40. chomp $json_str;
  41. %jh = %{JSON->new()->decode($json_str)};
  42. };
  43. close FILE;
  44. # Now we need to parse the JSON we just read
  45. # into an array of tuples
  46. # this anonymous function returns a list
  47. # of tuples of the below form:
  48. # (file_name, file_url)
  49. @a1 = sub{
  50. my @a0;
  51. my @a2;
  52. my $f1;
  53. # filters for file types we
  54. # dont want to downloads
  55. sub f0 {
  56. return $_[0]->{"ext"} &&
  57. !($_[0]->{"ext"} =~ /jpe?g/) &&
  58. !($_[0]->{"ext"} =~ /png/) &&
  59. !($_[0]->{"ext"} =~ /gif/) &&
  60. !($_[0]->{"ext"} =~ /webm/)
  61. }
  62. # create an array of files
  63. # that meet our file ext requirement
  64. # from the posts attribute
  65. @a0 = grep {f0($_)} @{$jh{"posts"}};
  66. # do the same filter on the
  67. # extra_files attribute
  68. @a2 = grep {
  69. f0($_)
  70. } map {
  71. @{$_->{"extra_files"}}
  72. } grep {
  73. $_->{"extra_files"}
  74. } @{$jh{"posts"}};
  75. # Return our tuple
  76. return map {
  77. [
  78. sprintf("%s%s", $_->{"filename"}, $_->{"ext"}), # file_name
  79. sprintf("%s%s%s", $URL_PREFIX, $_->{"tim"}, $_->{"ext"}) # file_url
  80. ]
  81. } (@a0, @a2);
  82. }->();
  83. # Print a list of wget commands from our tuples
  84. for my $i1 (@a1){
  85. printf("wget -nv -nc %s -O '%s%s'\n", scalar $i1->[1], $OUT_DIR, scalar $i1->[0]);
  86. }