Commit e920ae8d authored by Aziz Berkay Yesilyurt's avatar Aziz Berkay Yesilyurt
Browse files

rename twitter plugin

parent 1ccace64
Showing with 63 additions and 50 deletions
+63 -50
# Twitter Importer Plugin
# Twitter Plugin
### Development flow
```
pip install -e .[dev]
pre-commit install
```
Make sure to have pod running locally with the following environment variables set,
or use the dev pod at https://dev.pod.memri.io.
```
TWITTER_V2_CLIENT_ID
TWITTER_V2_CLIENT_SECRET
```
Your pod needs an access token to run the plugin.
Get an access token by running the following command.
```
twitter-oauth
```
This will open a browser window and ask you to login to twitter and authorize the pod defined by keys.json
Using dev pod? Then run:
```
twitter-oauth --pod dev
```
## About
This plugin is designed on top of the [Twitter API](https://developer.twitter.com) V2 with [OAuth 1.0a flow](https://developer.twitter.com/en/docs/authentication/oauth-1-0a).
......@@ -11,35 +38,10 @@ In this plugin, the data fetched for a given user handle includes:
- Followers and following accounts (id, handle, name, description, profile picture)
- User timeline tweets (id, content, mentions (only if already processed), date)
### TODO: More data to import:
- Media
- Mentioned users
## Unsupported functionality
- The plugin does not fetch user contact information such as phone numbers and emails.
- The plugin does not fetch user direct messages.
- The plugin does not support tweeting, commenting, retweeting on the speficied twitter handle account.
###### Client credentials:
App Key === API Key === Consumer API Key === Consumer Key === Customer Key === oauth_consumer_key
App Key Secret === API Secret Key === Consumer Secret === Consumer Key === Customer Key === oauth_consumer_secret
Callback URL === oauth_callback
###### Token credentials:
Access token === Token === resulting oauth_token
Access token secret === Token Secret === resulting oauth_token_secret
### Building the Docker image
The importer can be invoked by the Pod by launching a Docker container. To build the image for this container, run below commad with app keys replaced with your actual keys.
docker build -t twitter_importer --build-arg TWITTER_APP_KEY="YOUR_APP_KEY" --build-arg TWITTER_APP_SECRET="YOUR_APP_SECRET" .
After you have successfully built the plugin's docker image it is ready to be triggered by your Pod.
### Running locally
### Development flow
Install the plugin:
```
......@@ -47,14 +49,6 @@ Install the plugin:
```
To run the plugin locally, make sure you have a pod running.
Before starting the pod, set the following environment variables:
```
export TWITTER_APP_KEY="YOUR_APP_KEY"
export TWITTER_APP_SECRET="YOUR_APP_SECRET"
export TWITTER_APP_CALLBACK="http://localhost:3667/oauth" # [Optional. Default is "oob" for PIN-based auth]
```
Now you can run the plugin locally by running the following command:
```
run_plugin --metadata metadata.json
......@@ -72,6 +66,6 @@ follow the instructions in the browser to complete the OAuth flow.
After the OAuth flow is completed, the plugin will start fetching the data.
### To run tests
pytest ./tests/*
pytest ./tests/
{
"containerImage": "twitter_v2",
"containerImage": "localhost:2000/twitter",
"pluginModule": "twitter_v2.plugin",
"plugin": "twitter_v2.Plugin",
"pluginName": "Plugin",
"plugin": "twitter_v2.TwitterPlugin",
"pluginName": "TwitterPlugin",
"status": "not started",
"config": "{\"callback_url\": \"http://localhost:4000\"}"
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ from pymemri.pod.client import PodClient
from tweepy.api import pagination
from tweepy.models import Status, User
from twitter_v2 import Plugin
from twitter_v2 import TwitterPlugin
from twitter_v2.schema import SERVICE_NAME
......@@ -26,7 +26,7 @@ class Context:
plugin: PluginBase
def create_plugin_thread(plugin: Plugin):
def create_plugin_thread(plugin: TwitterPlugin):
def start_plugin():
plugin._run()
......@@ -57,7 +57,7 @@ def context():
)
pod = PodClient()
twitter_plugin = Plugin(client=pod, pluginRun=run)
twitter_plugin = TwitterPlugin(client=pod, pluginRun=run)
twitter_plugin.add_to_schema()
pod.create(run)
......@@ -146,7 +146,7 @@ def test_first_processing(context: Context):
# client = PodClient()
# client = context.pod
# context.plugin.start()
plugin: Plugin = context.plugin
plugin: TwitterPlugin = context.plugin
# plugin = TwitterPlugin(client=client)
plugin.add_to_schema()
plugin.tweepy_producer.client = mockAPI()
......
......@@ -3,7 +3,7 @@ import re
import pytest
from cached_session import CachedSession
from twitter_v2 import Plugin, PodClient
from twitter_v2 import PodClient, TwitterPlugin
from twitter_v2.schema import Account, Photo, Tweet
......@@ -11,7 +11,7 @@ def get_plugin(client, cache_path):
client.add_to_schema(Account, Tweet, Photo)
client.get_oauth2_access_token = lambda platform: "FAKE_TOKEN"
plugin = Plugin(client=client)
plugin = TwitterPlugin(client=client)
plugin.tweepy_producer.client.session = CachedSession(cache_path)
plugin.add_to_schema()
......
from pymemri.pod.client import PodClient
from .plugin import Plugin
from .plugin import TwitterPlugin
__all__ = ("Plugin", "PodClient")
__all__ = ("TwitterPlugin", "PodClient")
import socketserver
import webbrowser
from enum import Enum
from queue import Queue
from typing import List
import typer
from loguru import logger
from pymemri.pod.client import PodClient
from pymemri.pod.utils import DEFAULT_POD_KEY_PATH, read_pod_key
from pymemri.pod.utils import read_pod_key
from twitter_v2.cli import get_request_handler
......@@ -59,12 +60,30 @@ class TokenCallbackHandler:
return self.url
class PodType(str, Enum):
"""The type of Pod to connect to"""
local = "local"
dev = "dev"
qa = "qa"
prod = "prod"
POD_ADDRESS = {
PodType.local: "http://localhost:3030",
PodType.dev: "https://dev.pod.memri.io",
PodType.qa: "https://qa.pod.memri.io",
PodType.prod: "https://pod.memri.io",
}
@app.command()
def simulate_oauth2_flow(
pod_full_address: str = DEFAULT_POD_KEY_PATH,
pod: PodType = PodType.dev,
database_key: str = None,
owner_key: str = None,
):
pod_full_address = POD_ADDRESS[pod]
if database_key is None:
database_key = read_pod_key("database_key")
if owner_key is None:
......
......@@ -29,7 +29,7 @@ def thread_safe(func):
return wrapper
class Plugin(PluginBase):
class TwitterPlugin(PluginBase):
def __init__(self, client: PodClient, *args, **kwargs) -> None:
super().__init__(*args, client=client, **kwargs)
self.executor = ThreadPoolExecutor(max_workers=10)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment