From 6063be39e8b06d87ac5c3c291edeccd25925d6d9 Mon Sep 17 00:00:00 2001 From: dentoir Date: Tue, 20 Feb 2018 13:20:49 +0100 Subject: [PATCH] Cache media entries (related to #299) --- analysis/mod.export_tweets.php | 42 +++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/analysis/mod.export_tweets.php b/analysis/mod.export_tweets.php index 8b4c99de..d7dfc532 100644 --- a/analysis/mod.export_tweets.php +++ b/analysis/mod.export_tweets.php @@ -43,6 +43,22 @@ $header .= ",hashtags"; $csv->writeheader(explode(',', $header)); + // cache all media data + $media_entities = 0; + if (array_search("media", $exportSettings) !== false) { + $media_data = array(); + $sql = "SELECT * FROM " . $esc['mysql']['dataset'] . "_media"; + $rec = $dbh->prepare($sql); + $rec->execute(); + while ($data = $rec->fetch(PDO::FETCH_ASSOC)) { + if (!array_key_exists($data['tweet_id'], $media_data)) { + $media_data[$data['tweet_id']] = array(); + } + $media_data[$data['tweet_id']][] = $data; + $media_entities++; + } + } + // make query $sql = "SELECT * FROM " . $esc['mysql']['dataset'] . "_tweets t "; $where = ""; @@ -85,7 +101,29 @@ $error[] = $res2['error_code']; } } - // lookup media from media table + + // lookup media from media table which we have cached + if (array_search("media", $exportSettings) !== false) { + if (array_key_exists($id, $media_data)) { + foreach ($media_data[$id] as $res2) { + $urls[] = $res2['url']; + $expanded[] = $res2['url_expanded']; + $followed[] = ''; + $domain[] = ''; + $error[] = ''; + $media_ids[] = $res2['id']; + $media_urls[] = $res2['media_url_https']; + $media_type[] = $res2['media_type']; + $photo_width[] = $res2['photo_size_width']; + $photo_height[] = $res2['photo_size_height']; + $photo_resize[] = $res2['photo_resize']; + $indice_start[] = $res2['indice_start']; + $indice_end[] = $res2['indice_end']; + } + } + } + + /* if (array_search("media", $exportSettings) !== false) { $sql2 = "SELECT * FROM " . $esc['mysql']['dataset'] . "_media WHERE tweet_id = " . $id; $rec2 = $dbh->prepare($sql2); @@ -106,6 +144,7 @@ $indice_end[] = $res2['indice_end']; } } + */ if (array_search("media", $exportSettings) !== false && array_search("urls", $exportSettings) !== false) { // full export of urls with media information @@ -171,6 +210,7 @@ exit(0); } // Rest of script is the HTML page with a link to the cached CSV/TSV file. + ?>