Skip to content

Commit

Permalink
Reorganization & Starting to make profiles a bit less overcomplicated
Browse files Browse the repository at this point in the history
utils.py
* Moved file into InstaTweet directory and deleted utils directory because why did that even exist
* Changed UserAgent class to get_agent() function because again, for  what? Having it as a class that uses hardcoded user agents didn't make sense, now it'll scrape the standard chrome agent from whatismybrowser.com
* Updated get_root to be correct

-----
instatweet.py

I overcomplicated things when I first wrote this in November (?). Now that I know better I'm trying to simplify it, but it's hard without breaking everything!! Those memes were not lying oh my god.

Anyways, the goal is to make it really simple to use the local/hosted version of InstaTweet. Hosted DB version works great but I can't push that until this is all sorted out... so...
* Fixed bug where specifying a profile name as a kwarg when initializing InstaTweet would break everything 😍
    -If you specify a name it will now attempt to load the profile immediately (instead of setting all attributes first, trying to save a half-initialized profile, and raising an exception)
* Added profile name to config property and by extension, the saved profile .txt files (this is actually what broke it)
  • Loading branch information
TDKorn committed Apr 29, 2022
1 parent b8ffd22 commit 4f08d07
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 363 deletions.
35 changes: 16 additions & 19 deletions InstaTweet/core/instatweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from tqdm import tqdm
from collections.abc import Iterable
from InstaTweet.utils import UserAgent, get_root, get_filepath
from InstaTweet.utils import get_agent, get_root, get_filepath
from . import InstaClient, TweetClient

DEFAULT_USER_MAPPING = {'hashtags': [], 'scraped': [], 'tweets': []}
Expand All @@ -13,17 +13,18 @@
class InstaTweet:

def __init__(self, **kwargs):
self.profile_name = kwargs.pop('profile', 'default')
self.session_id = kwargs.pop('session_id', '')
self.user_agent = kwargs.pop('user_agent', UserAgent().default)
self.twitter_keys = kwargs.pop('twitter_keys', None)
self.user_map = kwargs.pop('user_map', {})
self.profile_name = kwargs.get('profile', 'default')

if self.is_default:
print('Using default profile.')
else:
if not self.is_default:
self.load_profile(self.profile_name)

else:
self.session_id = kwargs.get('session_id', '')
self.twitter_keys = kwargs.get('twitter_keys', None)
self.user_agent = kwargs.get('user_agent', get_agent())
self.user_map = kwargs.get('user_map', {})
print('Using default profile.')

@classmethod
def load(cls, profile_name: str):
return cls(profile=profile_name)
Expand Down Expand Up @@ -122,7 +123,7 @@ def validate(self):

if missing_keys := [key for key in TweetClient.DEFAULT_KEYS if key not in self.twitter_keys]:
raise KeyError(f'''
Invalid Twitter API Keys Provided
Invalid Twitter API Keys Provided
Missing Keys: {missing_keys}''')

if not all(self.twitter_keys.values()):
Expand Down Expand Up @@ -190,19 +191,14 @@ def twitter_keys(self, keys: dict):
def load_profile(self, profile_name: str):
if profile_path := self.profile_exists(profile_name):
profile = self.load_data(profile_path)
self._session_id = profile['session_id']
self.user_agent = profile['user_agent']
self._session_id = profile['session_id']
self._twitter_keys = profile['twitter_keys']
self.user_map = profile['user_map']
print(f'Loaded profile "{profile_name}"')

self.profile_name = profile['profile']
print(f'Loaded profile "{self.profile_name}"')
else:
new_profile = input(f'No profile found with the name {profile_name},'
f'would you like to create this profile? Y/N' + '\n' + '>> ')
if new_profile.lower() == 'y':
self.save_profile(profile_name)
else:
raise FileNotFoundError('No profile loaded')
raise FileNotFoundError('No profile with that name was found')

def save_profile(self, profile_name: str = None, alert: bool = True):
"""Update currently loaded profile, or save a new one. Name only required for new profiles."""
Expand Down Expand Up @@ -239,6 +235,7 @@ def is_default(self):
@property
def config(self):
return {
'profile': self.profile_name,
'session_id': self.session_id,
'user_agent': self.user_agent,
'twitter_keys': self.twitter_keys,
Expand Down
5 changes: 3 additions & 2 deletions InstaTweet/profiles/My Template.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"session_id": "sessionid",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
"profile": "My Template",
"session_id": "session_id",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36",
"twitter_keys": {
"Consumer Key": "key",
"Consumer Secret": "secret",
Expand Down
24 changes: 24 additions & 0 deletions InstaTweet/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
import requests
from pathlib import Path
from bs4 import BeautifulSoup as bs

BACKUP_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36'


def get_agent():
r = requests.get('https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome')
if r.ok:
soup = bs(r.text, 'html.parser')
if soup_agents := soup.find_all('span', {'class': 'code'}):
return soup_agents[0].text
# If function fails to scrape, will use hardcoded user agent
return BACKUP_AGENT


def get_root():
return Path(__file__).parent


def get_filepath(filename, filetype='.txt'):
return os.path.join(get_root(), filename) + filetype
2 changes: 0 additions & 2 deletions InstaTweet/utils/__init__.py

This file was deleted.

Loading

0 comments on commit 4f08d07

Please sign in to comment.