-
Notifications
You must be signed in to change notification settings - Fork 107
Expand shortened urls #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
abaumg
wants to merge
5
commits into
timhutton:main
Choose a base branch
from
abaumg:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4034b4d
expand shortened urls
abaumg 9841a32
Revert "expand shortened urls"
abaumg 9ddfe47
move link expansion to separate file
abaumg 2165d7c
try to expand plaintext links in really old tweets
abaumg 07235eb
Merge branch 'timhutton:main' into main
abaumg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import configparser | ||
| import glob | ||
| import os | ||
| import re | ||
| import requests | ||
| import time | ||
| from urllib.parse import urlparse | ||
| from parser import read_json_from_js_file | ||
|
|
||
| class URLExpander: | ||
| def __init__(self): | ||
| self.config = configparser.ConfigParser(allow_no_value=True, interpolation=None, strict=False) | ||
| self.config.optionxform = str | ||
| self.config.read('expand_urls.ini') | ||
| try: | ||
| self.shorteners = self.config.options('shorteners') | ||
| except configparser.Error: | ||
| print('No configuration found, using default configuration') | ||
| self.config['shorteners'] = {} | ||
| self.config['mappings'] = {} | ||
| self.shorteners = ['t.co', '7ax.de', 'bit.ly', 'buff.ly', 'cnn.it', 'ct.de', 'flic.kr', 'go.shr.lc', 'ift.tt', 'instagr.am', 'is.gd', 'j.mp', 'ku-rz.de', 'p.dw.com', 'pl0p.de', 'spon.de', 'sz.de', 'tiny.cc', 'tinyurl.com', 'trib.al', 'wp.me', 'www.sz.de', 'yfrog.com'] | ||
| [self.config.set('shorteners', x) for x in self.shorteners] | ||
| with open('expand_urls.ini', 'w') as inifile: | ||
| self.config.write(inifile) | ||
|
|
||
| def get_input_filenames(self): | ||
| input_folder = '.' | ||
|
|
||
| # Identify the file and folder names - they change slightly depending on the archive size it seems | ||
| data_folder = os.path.join(input_folder, 'data') | ||
| tweet_js_filename_templates = ['tweet.js', 'tweets.js', 'tweets-part*.js'] | ||
| input_filenames = [] | ||
| for tweet_js_filename_template in tweet_js_filename_templates: | ||
| input_filenames += glob.glob(os.path.join(data_folder, tweet_js_filename_template)) | ||
| if len(input_filenames)==0: | ||
| print(f'Error: no files matching {tweet_js_filename_templates} in {data_folder}') | ||
| exit() | ||
| return input_filenames | ||
|
|
||
| def process_tweets(self): | ||
| for tweets_js_filename in self.get_input_filenames(): | ||
| print(f'Parsing {tweets_js_filename}...') | ||
| json = read_json_from_js_file(tweets_js_filename) | ||
| [self.parse_tweet(tweet) for tweet in json] | ||
|
|
||
| def save_mapping(self, original_url, expanded_url): | ||
| self.config['mappings'][original_url] = expanded_url | ||
| with open('expand_urls.ini', 'w') as inifile: | ||
| self.config.write(inifile) | ||
|
|
||
| def mapping_exists(self, original_url): | ||
| try: | ||
| tmp = self.config['mappings'][original_url] | ||
| except KeyError: # TODO: this fails always | ||
| return False | ||
| return True | ||
|
|
||
| def parse_tweet(self, tweet): | ||
| tweet = tweet['tweet'] | ||
| if 'entities' in tweet and 'urls' in tweet['entities'] and len(tweet['entities']['urls']) > 0: | ||
| for url in tweet['entities']['urls']: | ||
| if 'url' in url and 'expanded_url' in url: | ||
| original_url = url['expanded_url'] | ||
| if not self.mapping_exists(original_url): | ||
| expanded_url = self.expand_short_url(original_url) | ||
| if expanded_url != original_url: | ||
| self.save_mapping(original_url, expanded_url) | ||
| else: | ||
| # really old tweets may contain URLs as plain text in the body | ||
| possible_urls = re.finditer(r"https?://[a-z0-9\.]+/[a-z0-9?]{10}", tweet['full_text'], re.MULTILINE | re.IGNORECASE) | ||
| for (_, match) in enumerate(possible_urls): | ||
| matched_url = match.group(0) | ||
| if not self.mapping_exists(matched_url): | ||
| expanded_url = self.expand_short_url(matched_url) | ||
| if (expanded_url != matched_url): | ||
| self.save_mapping(matched_url, expanded_url) | ||
|
|
||
| def is_short_url(self, url): | ||
| hostname = urlparse(url).hostname | ||
| if any(shortener == hostname for shortener in self.shorteners): | ||
| return True | ||
| return False | ||
|
|
||
| def expand_short_url(self, url): | ||
| if self.is_short_url(url): | ||
| try: | ||
| request = requests.head(url) | ||
| time.sleep(0.75) | ||
| except: | ||
| pass | ||
| if request.ok == False: | ||
| return url | ||
| try: | ||
| url_from_location_header = request.headers['location'] | ||
| except KeyError: | ||
| return url | ||
| if not url_from_location_header.startswith('http'): | ||
| return url | ||
| elif ':443' in url_from_location_header or self.is_short_url(url_from_location_header): | ||
| url_from_location_header = self.expand_short_url(url_from_location_header.replace('http:', 'https:')) | ||
| url = url_from_location_header | ||
| return url | ||
|
|
||
| if __name__ == '__main__': | ||
| URLExpander().process_tweets() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.