make_atsc_chanconf.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/perl
  2. # Angel Li sent me this script to help in setting up a
  3. # ~/.azap/channels.conf file automagicly. This probbably
  4. # isn't the final version
  5. use LWP;
  6. use HTML::Form;
  7. use HTTP::Cookies;
  8. use XML::XPath;
  9. use XML::XPath::XMLParser;
  10. #$DEBUG = 1;
  11. #
  12. # Center frequencies for NTSC channels
  13. #
  14. @ntsc = (
  15. 0, 0, 57, 63, 69, 79, 85, 177, 183, 189,
  16. 195, 201, 207, 213, 473, 479, 485, 491, 497, 503,
  17. 509, 515, 521, 527, 533, 539, 545, 551, 557, 563,
  18. 569, 575, 581, 587, 593, 599, 605, 611, 617, 623,
  19. 629, 635, 641, 647, 653, 659, 665, 671, 677, 683,
  20. 689, 695, 701, 707, 713, 719, 725, 731, 737, 743,
  21. 749, 755, 761, 767, 773, 779, 785, 791, 797, 803,
  22. );
  23. $ZIPCODE = 'txtZipcode';
  24. $XML = 'stationXml';
  25. $WEBSITE = 'http://www.antennaweb.org';
  26. $zipCode = $ARGV[0];
  27. unless ($zipCode) {
  28. die "Zipcode missing on the command line";
  29. }
  30. unless ($zipCode =~ /^\d\d\d\d\d$/) {
  31. die "Illegal zipcode: $zipCode";
  32. }
  33. $ua = LWP::UserAgent->new;
  34. $ua->cookie_jar({});
  35. push @{$ua->requests_redirectable}, 'POST';
  36. $response = $ua->get($WEBSITE);
  37. if ($response->is_success) {
  38. $form = HTML::Form->parse($response);
  39. $request = $form->click("btnStart");
  40. $response2 = $ua->request($request);
  41. if ($response2->is_success) {
  42. $form2 = HTML::Form->parse($response2);
  43. $form2->param($ZIPCODE, $zipCode);
  44. $request2 = $form2->click("btnSubmit");
  45. $response3 = $ua->request($request2);
  46. $form3 = HTML::Form->parse($response3);
  47. $request3 = $form3->click("btnContinue");
  48. $response4 = $ua->request($request3);
  49. if ($response4->is_success) {
  50. $form4 = HTML::Form->parse($response4);
  51. $xml = $form4->value($XML);
  52. $xml =~ s/%22/"/g;
  53. $xml =~ s/%2c/,/g;
  54. $xml =~ s/%2f/\//g;
  55. $xml =~ s/%3c/</g;
  56. $xml =~ s/%3d/=/g;
  57. $xml =~ s/%3e/>/g;
  58. $xml =~ s/\+/ /g;
  59. genConf($xml);
  60. exit(0);
  61. }
  62. else {
  63. print STDERR "Could not submit zipcode: $zipCode\n";
  64. die $response3->status_line;
  65. }
  66. }
  67. print STDERR "Could not reach zipcode page";
  68. die $response2->status_line;
  69. }
  70. else {
  71. print STDERR "Error reaching $WEBSITE\n";
  72. die $response->status_line;
  73. }
  74. sub genConf {
  75. my($xml) = @_;
  76. my($s);
  77. my($callSign);
  78. my($channel);
  79. my($c);
  80. my($psipChannel);
  81. my($freq);
  82. $xp = XML::XPath->new(xml => $xml);
  83. foreach $s ($xp->find('//Station[BroadcastType="D"]')->get_nodelist) {
  84. if ($s->find('LiveStatus')->string_value eq "1") {
  85. $callSign = $s->find('CallSign')->string_value;
  86. $callSign =~ s/-DT//;
  87. $channel = $s->find('Channel')->string_value; # Channel to tune
  88. $psipChannel = $s->find('PsipChannel')->string_value;
  89. if ($DEBUG) {
  90. print STDERR $callSign, "\t", $channel, " -> ", $psipChannel, "\n";
  91. }
  92. $psipChannel =~ s/\.\d+$//;
  93. $freq = $ntsc[$channel]*1000000;
  94. if ($freq) {
  95. print $callSign, ":", $freq, ":8VSB:0:0\n";
  96. }
  97. }
  98. }
  99. }