-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlcleaner.php
More file actions
177 lines (117 loc) · 5.1 KB
/
urlcleaner.php
File metadata and controls
177 lines (117 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/***********************************************
Newsletter Cleaner and Preparation Script
Copyright © 2016-2017 Christopher S. Penn
The purpose of this code is to take a list of URLs and expand them if shortened, trim off analytics tracking tags, then add new tags, re-shorten, and append with HTML page titles into Markdown format for use on Github and in any Markdown-compliant writing software.
Version 2.0
What's New:
- Now creates markdown output instead of CSV
- Fails gracefully for the most part on crap URLs
- Deals with non-shortened URLs better
Requirements:
cUrl for PHP/CLI
A bit.ly API key
Read/write access to the local disk
************************************************/
// configuration stuff
date_default_timezone_set('America/New_York');
ini_set('auto_detect_line_endings', TRUE);
$apikey = "PUT YOUR BITLY API KEY HERE";
// logfile outputs as a CSV file with a pipe delimiter
$stamp = date("Y-m-d-h-i-s");
$shortstamp = date("Y-m-d");
$logfile = "output-$stamp.md"; // change md to csv if you want csv instead
$fp = fopen($logfile, "w");
//fwrite($fp, "URL|count\n");
// What are your UTM codes? These are source, medium, and campaign. Avoid using spaces.
$source = "YOUR SOURCE";
$medium = "YOUR MEDIUM";
$campaign = "YOUR CAMPAIGN" . $shortstamp; // shortstamp appends a yyyy-mm-dd timestamp
// Page Title Getter
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// Page Title Cleaner
function cleanuptitles($text)
{
// known issues
$text = str_replace(" - Christopher S. Penn Blog", "", $text);
$text = str_replace(" - SHIFT Communications PR Agency - Boston | New York | San Francisco | Austin", "", $text);
$text = str_replace(" – Medium", "", $text);
// common delimiters
$text = str_replace(" - ", " via ", $text);
$text = str_replace(" | ", " via ", $text);
$text = str_replace(" – ", " via ", $text);
$text = str_replace(" · ", " via ", $text);
$text = str_replace(" ", " ", $text);
// clean up artifacts
$text = str_replace(". via ", " via ", $text);
return $text;
// fix ascii
$text = mb_convert_encoding($text, "ASCII");
}
// input file to open
$handle = @fopen("YOUR INPUT FILE HERE.txt", "r");
// parse the file, line by line
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
// parse the file, line by line
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$url = trim($buffer);
// expand raw bitly links
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // We'll parse redirect url from header.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // We want to just get redirect url but not to follow it.
$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
$longurl = !empty($matches[1]) ? trim($matches[1][0]) : $longurl = $url;
// split and clean
list($shorturl, $string) = explode('?', $longurl, 2);
// append UTM codes
$utms = "?utm_source=" . $source . "&utm_medium=" . $medium . "&utm_campaign=" . $campaign;
$longurl = $shorturl . $utms;
// obtain page title
$html = file_get_contents_curl($shorturl);
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++) {
$meta = $metas->item($i);
if ($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if ($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
}
$rawtitle = trim($title);
$pagetitle = cleanuptitles($rawtitle);
// re-encode
$bitly = "https://api-ssl.bitly.com/v3/shorten?&access_token=$apikey&longUrl=$longurl";
$results = json_decode(file_get_contents($bitly), true);
$theurl = $results['data']['url'];
// old format
// fwrite($fp, "$url\t$longurl\t$theurl\t$pagetitle\n");
// new format for markdown
// expanded version with descriptions - fwrite($fp, "[$pagetitle]($theurl) : $description\n");
fwrite($fp, "[$pagetitle]($theurl)\n");
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>