Skip to content

draconar/grab_packt

Repository files navigation

Grab a book a day for free from Packt Pub, https://www.packtpub.com/packt/offers/free-learning.

1. Install prerequisites

Install this script in the cloned directory using the following command:

npm install

2. Add credentials

Copy the .env file into place with

cp .env.example .env

Or on Windows:

copy .env.example .env

And set your packt email and password.

3. Grab on recurrent basis

Using Node

After that run the script with the following command:

watch -n 5000 --differences node server.js

Using Crontab

Or add it to your crontable:

crontab -e

For the crontab all paths in MUST be absolute.

Within the open cron editor window

0 14 * * * /usr/local/bin/node /Users/<USER_NAME>/<PATH_TO>/grab_packt/server.js >> /tmp/cron_output

If you are using UTC/BST timezone in your server, you might want to set the crontab as follow:

25 0 * * * /usr/bin/nodejs /home/user/grab_packt/server.js >> /tmp/cron_output

Using Task Scheduler in Windows

Check the run.bat file in the repo. Correct any path if necessary according to your needs. Try running the script manually to verify that it works as expected.

Then add a scheduled task to execute run.bat every day by running.

add_scheduled_task.bat

OSX Error

If you get the message: crontab: temp file must be edited in place

On a related issue, if you get the message: crontab: temp file must be edited in place

Try:

  1. Add to .bash_profile
alias crontab="VIM_CRONTAB=true crontab"
  1. Add to .vimrc
if $VIM_CRONTAB == "true"
    set nobackup
    set nowritebackup
endif

note: .bash_profile might be called .profile
note: .vimrc and .bash_profile are located in the home directory: ~/
Reference: http://superuser.com/a/750528

Using Launchd (OSX)

launchd is recommended over cron for the OSX system.

This runs on load and from then on every 24 hours (86400 seconds).
Just substitute <username> for your own.

by daemon I am referring to the .plist file

Navigate to directory:

cd $HOME/Library/LaunchAgents

Create file:

touch com.<username>.grab_pkt.plist

Edit file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.<username>.grab_pkt</string>

  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/node</string>
    <string>/Users/<username>/development/misc/grab_packt/server.js</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>StartInterval</key>
  <integer>86400</integer>

  <key>RunAtLoad</key>
  <true/>

  <key>StandardErrorPath</key>
  <string>/tmp/GrabPkt.err</string>

  <key>StandardOutPath</key>
  <string>/tmp/GrabPkt.out</string>
</dict>
</plist>

Load this daemon into the system:

launchctl load com.<username>.grab_pkt.plist

to unload just change load to unload

Check output of script:

/tmp/GrabPkt.out

It should be similar to:

----------- Packt Grab Started -----------
Book Title: Learning Libgdx Game Development
Claim URL: https://www.packtpub.com/freelearning-claim/13277/21478
----------- Packt Grab Done --------------

Check for errors:

/tmp/GrabPkt.err

Mine is empty due to having no errors.

In order to test I would:

  • remove the GrabPkt.out file
  • unload daemon
  • load daemon
  • check output of GrabPkt.out file

reference: http://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs

Using in AWS Lambda

  1. You need to sign up for an AWS account or use an existing one.
  2. After sign in to the AWS Management Console, select a region and open the AWS Lambda console.
  3. Choose Get Started Now.
  • Note: The console shows the Get Started Now page only if you do not have any Lambda functions created. If you have created functions already, you will see the Lambda > Functions page. On the list page, choose Create a Lambda function to go to the Lambda > New function page.
  1. On the Select blueprint page, choose Skip.
  2. On the Configure triggers page, do the following:
  3. Choose CloudWatch Events - Schedule.
  4. Enter a rule name in Rule name.
  5. Schedule expression select rate(1 day).
  6. Check Enable trigger.
  7. Choose Next.
  8. On the Configure function page, do the following:
  9. Enter a function name in Name.
  10. Runtime is Node.js 4.3 or above.
  11. Code entry type is Upload a .ZIP file.
  12. Zip the source code and Upload. - Please make sure to execute npm install and configure .env before zip the source code.
  13. In the Lambda function handler and role section, do the following: 1. Handler is aws-lambda.handler. 2. In Role, choose Create new role from template(s). 3. In Role name, type a name for the role. 4. In Role templates, you can leave this field blank because your Lambda function already has the basic execution permission it needs.
  14. In the Advanced settings section, do the following: 1. In Memory (MB), choose 128. 2. In Timeout, enter 0 min 30 sec.
  15. Choose Next.
  16. Choose Create Function to create a Lambda function. Imgur
  17. Choose Test.
  18. In the Input test event page, enter {} in the window.
  19. Choose Save and test.
  20. Upon successful execution, view results in the console. Imgur

Releases

No releases published

Packages

No packages published