How to retry failed requests in Scrapy

You can add one or more statuses in settings.py. Scrapy will process requests normally and when one of these statuses is encountered, it will retry that request. You can modify RETRY_HTTP_CODES and add any number of statuses there. You can also control how many times to try with RETRY_TIMES

July 23, 2021 · 1 min · 49 words · Saqib Razzaq

Docker Cheatsheet

Show images docker images Show containers docker ps Show running containers docker ps -a Connect to a running container docker exec -it container_id bash Start container docker start container_id Stop container docker stop container_id Remove Container docker rm container_id Pull container docker pull image_name Commit a container docker commit -m "message" -a "Author" container_id username/repo_name Remove all containers docker rm $(docker ps -a -q) Stop all containers docker kill $(docker ps -q) ...

July 11, 2021 · 1 min · 102 words · Saqib Razzaq

How to follow people in bulk on Medium.com using jQuery

This is one of the tricks on Medium.com that seems to work pretty well. Please use on your own risk. Open Medium.com Find a user with lots of followers Click on link to view those followers Then right click + inspect First include jQuery var jqry = document.createElement('script'); jqry.src = "https://code.jquery.com/jquery-3.3.1.min.js"; document.getElementsByTagName('head')[0].appendChild(jqry); jQuery.noConflict(); Then run the following snippet to follow. It is best practice to not follow more than 120 people per day. $('.js-followButton').each(function(index, item) { $(item).trigger("click"); console.log('Followed ' + index + '/ 120'); if (index > 100) { return false; } });

July 10, 2021 · 1 min · 93 words · Saqib Razzaq

How to iterate over several elements in jQuery

// iterate over a list $.each(list, function(key, field) { // something here }); // find an element across the page and iterate over each $(document).find('item').each(function(item, object) { // something here });

July 10, 2021 · 1 min · 31 words · Saqib Razzaq

How to make an article eyes friendly with jQuery

I often encounter this case where I land on a website that has something I’d really like to read. However, the white text on black background and 10px font size makes me want to throw up. Hence, I’m leaving this tiny piece here to fix that situation. Once you run this piece of code, it will do following things. Increase website font size Change background color to white Change text color to black If website doesn’t have jQuery, first run this ...

July 10, 2021 · 1 min · 105 words · Saqib Razzaq

Jquery - include in console

jQuery as we know comes loaded with most websites today. However, if there is a case when you’d like to run some experiments on website using jQuery but it isn’t being load by default, you can run following command var jqry = document.createElement('script'); jqry.src = "[https://code.jquery.com/jquery-3.3.1.min.js";](https://code.jquery.com/jquery-3.3.1.min.js) document.getElementsByTagName('head')\[0\].appendChild(jqry); jQuery.noConflict();

July 10, 2021 · 1 min · 48 words · Saqib Razzaq

Jquery - scroll to bottom

$("html, body").animate({ scrollTop: $(document).height()-$(window).height() });

July 10, 2021 · 1 min · 5 words · Saqib Razzaq

Quick Ajax template jQuery

Following is a sample Ajax template that can be used when you’re not in the mood of surfing through docs $.ajax({ url: "http://google.com", type: "post", dataType: "json", data: {name: item, value: value}, success: function (response) { // do somethinig here }, error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus, errorThrown); } });

July 10, 2021 · 1 min · 50 words · Saqib Razzaq

Quick each loop example in jQuery

$.each iterates over all elements specified and performs your code Iterate over a list $.each(list, function(key, field) { // something here }); Find an element across the page and iterate over each $(document).find('item').each(function(item, object) { // something here });

July 10, 2021 · 1 min · 39 words · Saqib Razzaq

Search YouTube and download first result

YouTube-dl Download and Documentation I was a long time user of YTMusic, a premium music subscription service. One day they randomly stopped accepting payments from my card. I tried a few others but nothing worked. In a few days I lost access to all my songs and I had to start with a new account with no explanation for what happened. I decided to not let this happen again and started building a backup of songs. Every time I liked a song, I would append it to list on Evernote. Now whenever I need to have all those songs available locally, I run the following command which iterates over each song title, searches youtube and dowloads first video’s audio. This is about 96% accurate :D ...

June 17, 2021 · 1 min · 141 words · Saqib Razzaq

How to filter and delete Google Chrome history items with Python

The following snippet can be used for filtering out URLs that where keywords match title or url. They are then deleted from history saving you lots of time that you’d otherwise spend manually filtering and deleting. Google Chrome must be closed before running this script. import sqlite3, webbrowser def cleanChrome(): con = sqlite3.connect('C:\\Users\\{username}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History') cursor = con.cursor() cursor.execute("select id, url, title from urls") urls = cursor.fetchall() # keywords that you'd like to detect and delete # it looks for these keywords in page title and urls keywords = [] total = len(urls) done = 0 deleted = 0 pendingIds = [] for url in urls: jenny.output(f'Processing {done} / {total} urls [deleted: {deleted}]') uid = url[0] link = url[1].lower() title = url[2].lower() for keyword in keywords: if keyword in link or keyword in title: jenny.output(f'{keyword} matched, deleting..') pendingIds.append((uid,)) deleted += 1 done += 1 query = 'DELETE FROM urls WHERE id=?' cursor.executemany(query, pendingIds) con.commit()

June 11, 2021 · 1 min · 153 words · Saqib Razzaq

How to read a CSV file in python

with open(path, mode='r') as csvFile: reader = csv.DictReader(csvFile) lineCount = 0 for row in reader: if lineCount > 0: # do stuff here lineCount += 1

May 11, 2021 · 1 min · 26 words · Saqib Razzaq

How to create virtual environment on Windows

First, make sure virtualenv installed or install it by running pip install virtualenv After this you can simply run virtualenv name_of_env_to_create You can activate it by going to following directory where you ran the command ./env/Scripts/activate.bat

May 1, 2021 · 1 min · 36 words · Saqib Razzaq

How to run a function when Scrapy spider closes

Scrapy spider can close unexpectedly for many reasons. If you’d like to notify yourself or do anything whenever a spider closes (expectedly or unexpectedly) Create a function named anything e.g crawlFinished() Then paste self.crawlFinish() at the bottom of closed() function Now your function will be executed each time crawler exits

May 1, 2021 · 1 min · 50 words · Saqib Razzaq

How to count total weekends between two dates in PHP

function compare($startDate, $endDate) { $weekends = 0; $startDate = new DateTime($startDate); $endDate = new DateTime($endDate); while($startDate <= $endDate ) { $timestamp = strtotime($startDate->format('d-m-Y')); $currentDay = strtolower(date('D', $timestamp)); if (!$weekends && $currentDay == 'sun') { $weekends += 1; } elseif ($currentDay == 'sat') { $weekends += 1; } $startDate->modify('+1 day'); } return $weekends; }

April 12, 2021 · 1 min · 53 words · Saqib Razzaq

How to open a URL in chrome using Python

import webbrowser webbrowser.register('chrome', None, webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe")) webbrowser.get('chrome').open('http://url.com')

April 11, 2021 · 1 min · 8 words · Saqib Razzaq

Download all videos from YouTube channel

YouTube-dl Download and Documentation This command iterates over entire channel and downloads each video one by one. youtube-dl -f best -ciw -o "%(title)s.%(ext)s" -v url_of_channel

March 28, 2021 · 1 min · 25 words · Saqib Razzaq

Download best audio of a YouTube video as MP3

YouTube-dl Download and Documentation Download the best quality audio in mp3 format using the command below. This command also utilises ffmpeg to extract audio. In general, it is a good idea to have ffmpeg installed on your system if you plan to use YouTube-dl youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 'url_here'

March 24, 2021 · 1 min · 53 words · Saqib Razzaq

How to make scrapy spider interactive at any point

from scrapy.shell import inspect_response inspect_response(response, self) Read this for more details

March 20, 2021 · 1 min · 11 words · Saqib Razzaq

Find element that has particular text in Scrapy

response.xpath("//*[contains(text(), 'txt goes here')]").getall()

March 19, 2021 · 1 min · 4 words · Saqib Razzaq

Bootstrap Auth Scaffholding in Laravel

If Laravel is not installed, first run composer create-project laravel/laravel composer require laravel/ui php artisan ui bootstrap --auth

March 17, 2021 · 1 min · 18 words · Saqib Razzaq

How to add a user middleware in Laravel

I was building an API. I had already used auth:api middleware. I was sure that a user was logged in. However, I wanted to protect some of my routes further by only allowing an admin to access them. I was using level field in users table. A level 1 meant it was an admin. Hence, I created an Admin middleware to do what I had in mind. Here is how it can be done. ...

March 17, 2021 · 1 min · 153 words · Saqib Razzaq

How to add helper files in Laravel

There are some cases when you need to add your own custom functions in Laravel. I often have my own custom helpers for speeding up development. You can easily add your own files by following this quick guide. Create a folder in app\Http folder. Let’s assume you created Helpers You can place any number of php files in this directory. Then open composer.json found in your root directory and find "autoload-dev": { "psr-4": { "Tests\\": "tests/" } } Then replace it so it looks like this "autoload-dev": { "psr-4": { "Tests\\": "tests/" }, "files": [ "app/Helpers/filename.php" ] } Finally, run composer dump-autoload and you are good to go

March 17, 2021 · 1 min · 108 words · Saqib Razzaq

How to create model, controller and migration in same command

Follow command can be used to create all three in one line php artisan make:model Modelname -mcr

March 17, 2021 · 1 min · 17 words · Saqib Razzaq

How to fix key is too long error in Laravel

If you are using sublime text run: Ctrl + P: AppServiceProvider.php Then at start of file paste use Illuminate\Support\Facades\Schema; Paste following code in boot() function Schema::defaultStringLength(191);

March 17, 2021 · 1 min · 26 words · Saqib Razzaq

How to migrate and seed with same command

You can run the migrations and seeders in the same line like this php artisan migrate:fresh --seed

March 17, 2021 · 1 min · 17 words · Saqib Razzaq

How to use Refresh database in Laravel PHPUnit Tests

A Laravel project ideally has many tests depending on the size of the project. We don’t want one test’s results to impact others. This is where RefreshDatabase trait comes in handy. It resets database after each test to make sure you get the clean slate for your next test. You can use it like this. First, paste this at top of your test file use Illuminate\Foundation\Testing\RefreshDatabase; Then right after opening { of your test class, paste following use RefreshDatabase; ...

March 17, 2021 · 1 min · 79 words · Saqib Razzaq

How to use proxies with Scrapy

There are two main ways to use proxies in Scrapy. You can either use it per request basis or use it with every scrapy outgoing request. How to use proxy with a single request proxy = 'proxy_here' return Request(url=url, callback=self.parse, meta={"proxy": proxy}) How to use proxy with every request Go to to middlewares.py and update process_request method and paste following code proxy = 'proxy_here' request.meta['proxy'] = proxy

February 24, 2021 · 1 min · 67 words · Saqib Razzaq

PHP - Autoload classes

Here is a quick snippet that showed how classes are loaded in BriskLimbs, a video sharing project of mine spl_autoload_register(function ($class) { $class = ucfirst($class); if (file_exists(MODEL_DIRECTORY . '/' . $class . '.php')) { require_once MODEL_DIRECTORY . '/' . $class . '.php'; } else { $class = strtolower($class); if (file_exists(HELPERS_DIRECTORY . '/' . $class . '.php')) { require_once HELPERS_DIRECTORY . '/' . $class . '.php'; } else { exit(MODEL_DIRECTORY . '/' . $class . '.php'); exit('Error Loading Class File |' . $class . '| core:' . CORE_DIRECTORY); } } });

February 21, 2021 · 1 min · 90 words · Saqib Razzaq

How to use validator and get all errors in Laravel

Laravel has a validated function which runs your defined rules and automatically sends errors to your view. This is awesome for most projects but sometimes you may want to handle / read these errors yourself. Or maybe just do it before sending them to view. Here is how you can do that. First of all, add validator at top of your file use Illuminate\Support\Facades\Validator; Then create validator with your rules like this $validator = Validator::make($requestData, [ 'user_id' => ['required', 'integer'] ]); Then use following snippet to get errors as array if ($validator->fails()) { $fieldsWithErrors = $validator->messages()->get('*'); // do anything here }

February 11, 2021 · 1 min · 101 words · Saqib Razzaq