<commit_before><?php

namespace App\Jobs;

use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DownloadWebMention implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * The webmention source URL.
     *
     * @var
     */
    protected $source;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(string $source)
    {
        $this->source = $source;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Client $guzzle)
    {
        $response = $guzzle->request('GET', $source);
        //4XX and 5XX responses should get Guzzle to throw an exception,
        //Laravel should catch and retry these automatically.
        if ($response->getStatusCode() == '200') {
            $filesystem = \Illuminate\FileSystem\FileSystem();
            $filename = $this->createFilenameFromURL($source);
            //backup file first
            $filenameBackup = $filename . '.' . date('Y-m-d') . '.backup';
            if ($filesystem->exists($filename)) {
                $filesystem->copy($filename, $filenameBackup);
            }
            //save new HTML
            $filesystem->put(
                $filename,
                (string) $response->getBody()
            );
            //remove backup if the same
            if ($filesystem->get($filename) == $filesystem->get($filenameBackup)) {
                $filesystem->delete($filenameBackup);
            }
        }
    }

    /**
     * Create a file path from a URL. This is used when caching the HTML
     * response.
     *
     * @param  string  The URL
     * @return string  The path name
     */
    private function createFilenameFromURL($url)
    {<commit_msg>Save HTML to the correct location<commit_after><?php

namespace App\Jobs;

use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DownloadWebMention implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * The webmention source URL.
     *
     * @var
     */
    protected $source;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(string $source)
    {
        $this->source = $source;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Client $guzzle)
    {
        $response = $guzzle->request('GET', $source);
        //4XX and 5XX responses should get Guzzle to throw an exception,
        //Laravel should catch and retry these automatically.
        if ($response->getStatusCode() == '200') {
            $filesystem = \Illuminate\FileSystem\FileSystem();
            $filename = storage_path() . '/HTML/' . $this->createFilenameFromURL($source);
            //backup file first
            $filenameBackup = $filename . '.' . date('Y-m-d') . '.backup';
            if ($filesystem->exists($filename)) {
                $filesystem->copy($filename, $filenameBackup);
            }
            //save new HTML
            $filesystem->put(
                $filename,
                (string) $response->getBody()
            );
            //remove backup if the same
            if ($filesystem->get($filename) == $filesystem->get($filenameBackup)) {
                $filesystem->delete($filenameBackup);
            }
        }
    }

    /**
     * Create a file path from a URL. This is used when caching the HTML
     * response.
     *
     * @param  string  The URL
     * @return string  The path name
     */
    private function createFilenameFromURL($url)
    {
{'fraction_non_alphanumeric': 0.11782945736434108, 'fraction_numerical': 0.002583979328165375, 'mean_word_length': 1.968558282208589, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1531935', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1070, 'n_words': 271}