alejo8591
alejo8591
Alejandro Romero
12 Snippets  (74th place)
Published
1 Channel
Created
3 Channels
Following
Feb 5, 2018
Last Visit
Feb 5, 2018
Registered
3 points  (19910th place)
Reputation
Top 10% Publisher
Junior Code Generator
Serious Popular Coder
Junior Autobiographer
Senior Autobiographer
Junior Famous Coder

Recent Snippets See all snippets by alejo8591

public by alejo8591 created Oct 10, 2017  279  0  3  0

Python Selenium get cookie value

Python Selenium get cookie value: scrape.py
import time
from selenium import webdriver

driver = webdriver.Chrome('./chromedriver')
driver.get('https://ui.lkqd.com/login')
assert 'LKQD' in driver.title

time.sleep(2)
username_field = driver.find_element_by_name('username')
username_field.send_keys('myusername')

password_field = driver.find_element_by_name('password')
password_field.send_keys('mypassword')

submit_button = driver.find_elements_by_xpath("//*[contains(text(), 'Sign In')]")[0]
submit_button.click()
time.sleep(2)

cookies_list = driver.get_cookies()
cookies_dict = {}
for cookie in cookies_list:
    cookies_dict[cookie['name']] = cookie['value']

session_id = cookies_dict.get('session')
print(session_id)

driver.quit()


;

public by alejo8591 created Aug 22, 2017  142  0  1  0

Ubuntu 14.04 Python 3.4.2 Setup using pyenv and pyvenv

Ubuntu 14.04 Python 3.4.2 Setup using pyenv and pyvenv: ubuntu_py3.md
What I did to get Python 3.4.2 on Ubuntu 14.04. The stock version of Python 3 on Ubuntu is 3.4.0. Which is missing some of the best parts! (asyncio, etc). Luckily I discovered [pyenv](https://github.com/yyuu/pyenv) which solved my problem.

# Install pyenv

Pyenv (not to be confused with pyvenv) is the Python equivelant of rbenv. It lets you configure which Python environment/version is available per directory, user, or other session variables. 

I followed the instructions [here](http://opencafe.readthedocs.org/en/latest/getting_started/pyenv/) to install pyenv in my home directory. Verbatem, those instructions are:

```
sudo apt-get install git python-pip make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev
sudo pip install virtualenvwrapper

git clone https://github.com/yyuu/pyenv.git ~/.pyenv
git clone https://github.com/yyuu/pyenv-virtualenvwrapper.git ~/.pyenv/plugins/pyenv-virtualenvwrapper

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'pyenv virtualenvwrapper' >> ~/.bashrc
```
# Install Python 3.4.2

Restart your shell to pickup pyenv. Then tell pyenv to download, build, and instatll 3.4.2:

```pyenv install 3.4.2```

# Point a project at 3.4.2

Place a hidden file `.python-version` in your project. In this file simply place the text:

> 3.4.2

In this directory, type `python` to enter the shell and note the version. It should be 3.4.2.

# PyVenv

You should also now be able to use pyvenv to bootstrap a virtual environment in this directory. It will bootstrap a virtualenv with python 3.4.2 as the python version. You can do everything you'd normally do in a virtualenv, like install dependencies, etc:

```
pyvenv venv
source venv/bin/activate
pip install requests
pip freeze > requirements.txt
```

# Syntastic

I use pyflakes with Syntastic for finding syntax bugs in VIM. I had to be sure to install pyflakes to python 3.4.2 by going into a directory where 3.4.2 was being used and doing

    pip install pyflakes

;

public by Alejandro Romero created Aug 10, 2017  93  1  1  0

forever init.d example

forever init.d example: forever-initd-example.sh
#!/bin/bash
#
# initd-example      Node init.d 
#
# chkconfig: 345 80 20
# description: Node init.d example
# processname: node
# pidfile: /var/run/initd-example.pid
# logfile: /var/log/initd-example.log
#

# Source function library.
. /lib/lsb/init-functions

NAME=initd-example                  # Unique name for the application
NODE_ENV=production                 # Node environment
PORT=1234                           # Port (in this case the application uses process.env.PORT to set the port)
INSTANCE_DIR=/var/www/$NAME         # Location of the application source
COMMAND=coffee                      # Command to run
SOURCE_NAME=app.coffee              # Name os the applcation entry point script

user=apache
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever_dir=/var/run/forever        # Forever root directory.

node=node
forever=forever
awk=awk
sed=sed

start() {
    echo "Starting $NAME node instance: "

    if [ "$id" = "" ]; then
        # Create the log and pid files, making sure that the target use has access to them
        touch $logfile
        chown $user $logfile

        touch $pidfile
        chown $user $pidfile

        # Launch the application
        start_daemon
            $forever start -p $forever_dir --pidFile $pidfile -l $logfile -a -d $INSTANCE_DIR -c $COMMAND $SOURCE_NAME
        RETVAL=$?
    else
        echo "Instance already running"
        RETVAL=0
    fi
}

restart() {
    echo -n "Restarting $NAME node instance : "
    if [ "$id" != "" ]; then
        $forever restart -p $forever_dir $id
        RETVAL=$?
    else
        start
    fi
}

stop() {
    echo -n "Shutting down $NAME node instance : "
    if [ "$id" != "" ]; then
        $forever stop -p $forever_dir $id
    else
        echo "Instance is not running";
    fi
    RETVAL=$?
}

getForeverId() {
    local pid=$(pidofproc -p $pidfile)
    $forever list -p $forever_dir | $sed -e 's/\x1b\[[0-9; ]*m//g' | $awk "\$6 && \$6 == \"$pid\" { gsub(/[\[\]]/, \"\", \$2); print \$2; }";
}
id=$(getForeverId)

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${pidfile}
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

;

public by alejo8591 created Nov 10, 2016  151  0  1  0

Retrofit2 interceptor to add headers to HTTP requests

Retrofit2 interceptor to add headers to HTTP requests: AddHeaderInterceptor.java
public class AddHeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request.Builder builder = chain.request().newBuilder();
        builder.addHeader("Authorization", "headerContent");

        return chain.proceed(builder.build());
    }
}

;

public by mulhoon created Feb 22, 2016  1872  16  3  0

Send a push notification in node with OneSignal

Send a push notification in node with OneSignal : onesignal-node.js
var request = 	require('request');

var sendMessage = function(user, message){
	var restKey = '****';
	var appID = '****';
	request(
		{
			method:'POST',
			uri:'https://onesignal.com/api/v1/notifications',
			headers: {
				"authorization": "Basic "+restKey,
				"content-type": "application/json"
			},
			json: true,
			body:{
				'app_id': appID,
				'contents': {en: message},
				'include_player_ids': [user]
			}
		},
		function(error, response, body) {
			if(!body.errors){
				console.log(body);
			}else{
				console.error('Error:', body.errors);
			}
			
		}
	);
}

sendMessage('a9fb63b1-b5cc-4ee9-92f0-5be15eb300c0', 'Hello!');


;