Skip to content

Instantly share code, notes, and snippets.

@pshapiro
pshapiro / metadesc.py
Created June 6, 2018 22:19
Use Text Summarization Algorithms to Help Aid the Writing of Meta Descriptions
import csv
import os
from sumy.parsers.html import HtmlParser
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer as Lsa
from sumy.summarizers.luhn import LuhnSummarizer as Luhn
from sumy.summarizers.text_rank import TextRankSummarizer as TxtRank
from sumy.summarizers.lex_rank import LexRankSummarizer as LexRank
from sumy.summarizers.sum_basic import SumBasicSummarizer as SumBasic
@pshapiro
pshapiro / index.js
Created November 28, 2017 06:46
Puppeteer Crawler Example
var Walker = require('puppeteer-walker')
var fs = require('fs')
var walker = Walker()
fs.writeFile('output.csv', 'URL, Title, H2\r\n', 'utf8', function (err) {
console.log(`Header written`)
})
walker.on('end', () => console.log('finished walking'))
@pshapiro
pshapiro / reddit-keyword-insights.py
Created August 23, 2017 02:41
Get stats from reddit keywords
import praw
import csv
import datetime
# Create an app: https://www.reddit.com/prefs/apps
# Use http://localhost:8080 as redirect uri
username = ""
password = ""
clientid = ""
clientsecret = ""
@pshapiro
pshapiro / search.html
Created May 12, 2017 21:13
Basic example of using the GET method using amp-form
<form action="https://searchwilderness.com/" method="get" target="_top">
<input name="s" placeholder="Search The Blog" required="" type="text">
<input type="submit" value="Search">
</form>
@pshapiro
pshapiro / mailchimp-post.php
Created May 11, 2017 15:27
amp-form implementation of a MailChimp / Email Subscribe post handler.
<?php
if (!empty($_POST)) {
header("access-control-allow-credentials:true");
header("access-control-allow-headers:Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
header("access-control-allow-methods:POST, GET, OPTIONS");
header("access-control-allow-origin:".$_SERVER['HTTP_ORIGIN']);
header("access-control-expose-headers:AMP-Access-Control-Allow-Source-Origin");
// change to represent your site's protocol, either http or https
header("amp-access-control-allow-source-origin:https://".$_SERVER['HTTP_HOST']);
header("Content-Type: application/json");
@pshapiro
pshapiro / mailchimp.html
Created May 11, 2017 04:45
Example of posting an email subscriber form to MailChimp in AMP using amp-form
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
<link rel="canonical" href="http://localhost">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-custom>
@pshapiro
pshapiro / functions.php
Last active February 23, 2017 02:52
Sample WordPress theme functions.php modification to generate Client ID
// Source: https://www.simoahava.com/analytics/google-analytics-client-id-amp-pages/
// REST API for GTM container
add_action( 'rest_api_init', function() {
register_rest_route(
'amp-gtm',
'/amp.json',
array(
'methods' => 'GET',
'callback' => 'retrieve_gtm_json',
)
@pshapiro
pshapiro / BulkAMPValidator.py
Created February 21, 2017 21:39
AMPBench API Bulk Validator
import requests
import json
import csv
import os
# For details about AMPBench and the API:
# https://github.com/ampproject/ampbench
urlinput = os.path.join(os.path.dirname(__file__), input('Enter input text file: '))
urls = open(urlinput, "r")
@pshapiro
pshapiro / internal-pagerank.r
Last active August 3, 2022 08:53
Modification to assign PR a value between 1 and 10
library(igraph)
map <- function(x, range = c(0,1), from.range=NA) {
if(any(is.na(from.range))) from.range <- range(x, na.rm=TRUE)
## check if all values are the same
if(!diff(from.range)) return(
matrix(mean(range), ncol=ncol(x), nrow=nrow(x),
dimnames = dimnames(x)))
## map to [0,1]
@pshapiro
pshapiro / GoogleTrendsSlopeCalculator.py
Last active May 12, 2020 15:41
Python script to calculate slope from Google Trends
# Requires pandas. For Windows users, I recommend installing the Anaconda Python distirbution.
# Requires the pytrends library. To install, run "pip install pytrends".
from pytrends.pyGTrends import pyGTrends
import time
import os
from random import randint
import pandas as pd
# Add your Gmail username to the google_username variable and your Gmail password to the google_password variable.
google_username = ""