]> git.donarmstrong.com Git - bin.git/blob - jobs_to_org
document jobs_to_org a little bit
[bin.git] / jobs_to_org
1 #!/usr/bin/perl
2 # jobs_to_org finds jobs in different job sites and turns them into org things
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
5 # more information.
6 # Copyright 2016 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 jobs_to_org - finds jobs in different job sites and turns them into org things
18
19 =head1 SYNOPSIS
20
21 jobs_to_org [options]
22
23  Options:
24    --debug, -d debugging level (Default 0)
25    --help, -h display this help
26    --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--debug, -d>
33
34 Debug verbosity. (Default 0)
35
36 =item B<--help, -h>
37
38 Display brief usage information.
39
40 =item B<--man, -m>
41
42 Display this manual.
43
44 =back
45
46 =head1 EXAMPLES
47
48 jobs_to_org
49
50 =cut
51
52 use OSSP::uuid;
53 use WWW::Mechanize;
54 use WWW::Mechanize::TreeBuilder;
55 use vars qw($DEBUG);
56 use Data::Printer;
57 use Text::Wrap;
58 use POSIX qw(strftime);
59
60 tie my $uuid, 'OSSP::uuid::tie';
61 $uuid= ["v1"];
62
63 my %options = (debug           => 0,
64                help            => 0,
65                man             => 0,
66                pages           => 1,
67                site            => 'herc',
68               );
69
70 GetOptions(\%options,
71            'site|s=s',
72            'pages|p=i',
73            'debug|d+','help|h|?','man|m');
74
75 pod2usage() if $options{help};
76 pod2usage({verbose=>2}) if $options{man};
77
78 $DEBUG = $options{debug};
79
80 my @USAGE_ERRORS;
81 # if (1) {
82 #     push @USAGE_ERRORS,"You must pass something";
83 # }
84
85 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
86
87 my %sites =
88     (herc => {url => 'http://main.hercjobs.org/jobs/search?keywords=professor+AND+%28genomics+OR+bioinformatics+OR+biology+OR+informatics%29&discipline=academic-faculty&category=academic-faculty&category=allied-health&category=biological-biomedical-sciences&category=computer-information-sciences&category=education&category=interdisciplinary&category=mathematics-statistics&category=medical-research&category=physical-sciences&sort=DATE_POSTED+DESC',
89               next_selector => [class => "bti-pagination-previous-link bti-pagination-prev-next",text => '>'],
90               job_selector => [url_regex => qr{^\/jobs\/\d+/.+}],
91               university => [itemprop=>"hiringOrganization",itemtype=>"http://schema.org/Organization"],
92               description => [class=>"bti-jd-description",itemprop=>"description"],
93               date => [class=>"bti-jd-detail-text",
94                        sub {scalar $_[0]->parent()->attr('class') eq 'bti-jd-details-action'}],
95               position => [class=>"bti-jd-title",itemprop=>"title"],
96              },
97      nature => {url => 'http://www.nature.com/naturejobs/science/jobs?utf8=%E2%9C%93&q%5B%5D=professor&job_type%5B%5D=Assistant+Professor&job_type%5B%5D=Professor&order_by=created_on',
98                 next_selector => [class=>"next_page",url_regex=>qr{^/naturejobs/science/jobs},],
99                 job_selector => [url_regex => qr{^/naturejobs/science/jobs/\d+-.+}],
100                 university => [href => qr{^/naturejobs/science/employer-directory/\d+$}],
101                 description => [class=>"job-description"],
102                 date => [],
103                 position => [class=>'job-title heading'],
104                },
105     );
106
107 binmode STDOUT,":utf8";
108 get_jobs($options{site},$options{pages});
109
110 sub get_jobs {
111     my ($site,$pages) = @_;
112
113     my $todays_date = strftime('%Y-%m-%d %H:%M:%S',localtime());
114
115     my $m = WWW::Mechanize->new();
116     WWW::Mechanize::TreeBuilder->meta->apply($m);
117     if (not defined $sites{$site}) {
118         die "Unknown site $site";
119     }
120     my $s = $sites{$site};
121     $m->get($s->{url});
122     for (1..$pages) {
123         my %seen;
124         my @job_urls = grep { ! $seen{ $_->URI()->abs() }++ }
125             $m->find_all_links(@{$s->{job_selector}});
126         for my $j_u (@job_urls) {
127             $m->get($j_u);
128             my $university = 'No university';
129             eval {
130                 $university = $m->tree->look_down(@{$s->{university}})->as_text();
131             };
132             my $date = $todays_date;
133             eval {
134                 $date = $m->tree->look_down(@{$s->{date}})->as_text() // $todays_date if
135                     @{$s->{date}};
136             };
137             my $description = 'unknown';
138             eval {
139                 $description = $m->tree->look_down(@{$s->{description}})->as_text();
140             };
141             my $position = 'Unknown';
142             eval {
143                 $position = $m->tree->look_down(@{$s->{position}})->as_text();
144             };
145             print format_job($university,$position,$j_u->URI->abs(),$description,$date);
146             $m->back();
147         }
148         $m->follow_link(@{$s->{next_selector}}) or die "Unable to find next link";
149     }
150 }
151
152 sub format_job {
153     my ($university,$position,$url,$text,$date) = @_;
154     $text = wrap('   ','   ',$text);
155     my $ret = <<"EOF";
156 ** TODO $university -- $position
157    :PROPERTIES:
158    :ID:       $uuid
159    :END:
160 [[$url]]
161 [$date]
162
163 $text
164
165 EOF
166     return $ret;
167 }
168
169
170
171 __END__