Newer
Older
* Class FakeWebsiteAnalyticsSeeder Inserts Fake Analytics in the database
*
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Database\Seeds;

Yassine Doghri
committed
use App\Models\EpisodeModel;
use App\Models\PodcastModel;
use CodeIgniter\Database\Seeder;
class FakeWebsiteAnalyticsSeeder extends Seeder
{

Yassine Doghri
committed
/**
* @var string[]
*/
protected array $keywords = [
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
'all the smoke podcast',
'apple podcast',
'bad friends podcast',
'best podcast',
'best podcasts',
'best podcasts 2020',
'blood ties',
'call her daddy',
'call her daddy podcast',
'call her daddy podcast controversy',
'call her daddy podcast drama',
'counter clock podcast',
'counterclock podcast',
'crime junkie podcast',
'crime podcast',
'down the hill podcast',
'gerry callahan podcast',
'google podcast',
'history podcast',
'joe rogan',
'joe rogan podcast',
'lana rhoades and logan paul podcast',
'last podcast on the left',
'michael moore podcast',
'michelle obama podcast',
'missing in alaska podcast',
'murder podcast',
'nice white parents podcast',
'nick cannon podcast',
'npr podcast',
'office ladies podcast',
'podcast app',
'podcasts',
'rogan podcast',
'rudy giuliani podcast',
'savage podcast',
'serial podcast',
'smartless podcast',
'ted cruz podcast',
'the daily',
'the daily podcast',
'the last podcast on the left',
'the new abnormal podcast',
'tiger king podcast',
'trey gowdy podcast',
'true crime podcast',
'what is a podcast',
'what is podcast',
'wind of change podcast',
'your own backyard podcast',
];

Yassine Doghri
committed
/**
* @var string[]
*/
protected array $domains = [
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
'360.cn ',
'adobe.com ',
'aliexpress.com ',
'alipay.com ',
'amazon.co.jp ',
'amazon.com ',
'amazon.in ',
'apple.com ',
'baidu.com ',
'bing.com ',
'bongacams.com ',
'chaturbate.com ',
'china.com.cn ',
'csdn.net ',
'ebay.com ',
'facebook.com ',
'google.co.in ',
'google.com ',
'google.com.hk ',
'instagram.com ',
'jd.com ',
'live.com ',
'livejasmin.com ',
'microsoft.com ',
'microsoftonline.com ',
'myshopify.com ',
'naver.com ',
'netflix.com ',
'office.com ',
'okezone.com ',
'panda.tv ',
'qq.com ',
'reddit.com ',
'sina.com.cn ',
'sohu.com ',
'taobao.com ',
'tianya.cn ',
'tmall.com ',
'tribunnews.com ',
'twitch.tv ',
'twitter.com ',
'vk.com ',
'weibo.com ',
'wikipedia.org ',
'xinhuanet.com ',
'yahoo.co.jp ',
'yahoo.com ',
'youtube.com ',
'zhanqi.tv ',
'zoom.us ',
];

Yassine Doghri
committed
/**
* @var string[]
*/
protected array $browsers = [
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
178
179
'Android Browser',
'Avast Secure Browser',
'BlackBerry Browser',
'Chrome',
'Chrome Mobile',
'Chrome Mobile iOS',
'Chrome Webview',
'Chromium',
'Ecosia',
'Fennec',
'Firebird',
'Firefox',
'Firefox Mobile',
'Firefox Mobile iOS',
'Galeon',
'GNOME Web',
'Headless Chrome',
'Huawei Browser',
'IE Mobile',
'Inconnu',
'Internet Explorer',
'Kindle Browser',
'Konqueror',
'Maxthon',
'Meizu Browser',
'Microsoft Edge',
'MIUI Browser',
'Mobile Safari',
'Mobile Silk',
'OmniWeb',
'Openwave Mobile Browser',
'Opera',
'Opera Mini',
'Opera Mobile',
'Opera Next',
'Palm Blazer',
'Puffin',
'QupZilla',
'Safari',
'Samsung Browser',
'UC Browser',
'WOSBrowser',
];

Yassine Doghri
committed
public function run(): void
{
$podcast = (new PodcastModel())->first();
if ($podcast) {
$firstEpisode = (new EpisodeModel())
->selectMin('published_at')
->first();
for (
$date = strtotime($firstEpisode->published_at);
$date < strtotime('now');
$date = strtotime(date('Y-m-d', $date) . ' +1 day')
) {

Yassine Doghri
committed
$websiteByBrowser = [];
$websiteByEntryPage = [];
$websiteByReferer = [];
$episodes = (new EpisodeModel())
->where([
'podcast_id' => $podcast->id,
'DATE(published_at) <=' => date('Y-m-d', $date),
])
->findAll();
foreach ($episodes as $episode) {
$age = floor(($date - strtotime($episode->published_at)) / 86400,);
$probability1 = (int) floor(exp(3 - $age / 40)) + 1;

Yassine Doghri
committed
$lineNumber = 0;
$lineNumber < rand(1, $probability1);
++$lineNumber
$probability2 = (int) floor(exp(6 - $age / 20)) + 10;
$domain =
$this->domains[rand(0, count($this->domains) - 1)];
$keyword =
$this->keywords[
rand(0, count($this->keywords) - 1)
];
$browser =
$this->browsers[
rand(0, count($this->browsers) - 1)
];
$hits = rand(0, $probability2);

Yassine Doghri
committed
$websiteByBrowser[] = [
'podcast_id' => $podcast->id,
'date' => date('Y-m-d', $date),
'browser' => $browser,
'hits' => $hits,
];

Yassine Doghri
committed
$websiteByEntryPage[] = [
'podcast_id' => $podcast->id,
'date' => date('Y-m-d', $date),
'entry_page_url' => $episode->link,
'hits' => $hits,
];

Yassine Doghri
committed
$websiteByReferer[] = [
'podcast_id' => $podcast->id,
'date' => date('Y-m-d', $date),
'referer_url' =>
'http://' . $domain . '/?q=' . $keyword,
'domain' => $domain,
'keywords' => $keyword,
'hits' => $hits,
];
}
}
$this->db
->table('analytics_website_by_browser')
->ignore(true)

Yassine Doghri
committed
->insertBatch($websiteByBrowser);
$this->db
->table('analytics_website_by_entry_page')
->ignore(true)

Yassine Doghri
committed
->insertBatch($websiteByEntryPage);
$this->db
->table('analytics_website_by_referer')
->ignore(true)

Yassine Doghri
committed
->insertBatch($websiteByReferer);

Yassine Doghri
committed
echo "COULD NOT POPULATE DATABASE:\n\tCreate a podcast with episodes first.\n";