Spaces:
Runtime error
Runtime error
from git import Repo | |
import os | |
import streamlit as st | |
st.set_page_config( | |
page_title="TRACING INSIGHTS", | |
page_icon=None, | |
layout="wide", | |
#initial_sidebar_state="expanded", | |
# menu_items={ | |
# 'Get Help': 'https://www.extremelycoolapp.com/help', | |
# 'Report a bug': "https://www.extremelycoolapp.com/bug", | |
# 'About': "# This is a header. This is an *extremely* cool app!" | |
# } | |
) | |
st.write("Hello world!") | |
GITHUB_PAT = os.environ['GITHUB'] | |
api_key = os.environ['api_key'] | |
secret_api_key = os.environ['secret_api_key'] | |
access_token = os.environ['access_token'] | |
secret_access_token = os.environ['secret_access_token'] | |
# if not os.path.exists('repo_directory'): | |
# Repo.clone_from(f'https://tracinginsights:{GITHUB_PAT}@github.com/TracingInsights/translator.git', 'repo_directory' ) | |
# from repo_directory import translator | |
# translator.auth(api_key,secret_api_key,access_token,secret_access_token) | |
import translators as ts | |
import translators.server as tss | |
import random | |
import tweepy | |
import time | |
import json | |
# works with tweets and replies too | |
def bulk_trans(text: str, ): | |
""" | |
Will translate str into num_trans different languages and then back into original language | |
:param text: str, must | |
:return: str | |
""" | |
curr = tss.google(text, to_language="en", sleep_seconds=0.051) | |
return curr | |
def check_or_write(filename:str, s1:str): | |
""" | |
Will check if s1 is in a single line within filename and also write | |
the tweet to filename if it is not inside. Returns true when s1 is | |
not inside, returns false when s1 is found. Keeps line length of text | |
file less than 22 | |
:param filename: str(must) | |
:param s1: str(must) | |
:return: bool | |
""" | |
#checks to see if s1 is already inside | |
with open(filename, 'r', encoding='utf8') as f1: | |
f1.seek(0) | |
lines=f1.readlines() | |
l=0 | |
for line in lines: | |
l+=1 | |
if s1 in line: | |
return False | |
#makes sure that there is not more than 20 lines in txt file | |
if l>20: | |
while l>20: | |
del lines[0] | |
l-=1 | |
new_file=open(filename, "w+", encoding='utf8') | |
for line in lines: | |
new_file.write(line) | |
new_file.close() | |
#returns true and also writes down text into file at the end | |
with open(filename, 'a', encoding='utf8') as f1: | |
f1.write(s1 + '\n') | |
f1.close() | |
return True | |
# from translator github | |
def auth(api_key,secret_api_key,access_token,secret_access_token): | |
# Authenticate to Twitter | |
auth = tweepy.OAuthHandler(api_key, secret_api_key) | |
auth.set_access_token(access_token, secret_access_token) | |
api = tweepy.API(auth, wait_on_rate_limit=True) | |
#loops endlessly(60 sec interval) and checks,translates,and posts tweets | |
# use this to get user id https://tweeterid.com/ | |
while True: | |
# get twitter user_id at https://tweeterid.com/ by typing username | |
timeline = api.user_timeline(user_id="1568348454619070465", #testing case using englishgiuly, | |
#["2755544640", "1030481714", "407048032","227265199","190632194", "149542215","127854979", "106170617"], | |
count=5, tweet_mode="extended",) | |
for tweet in timeline: | |
if check_or_write('tweet.txt', " ".join(tweet.full_text.splitlines())): | |
curr = bulk_trans(tweet.full_text) | |
status = f"{curr[:254]} https://twitter.com/1/status/{tweet.id}" # each link is converted to 23 characters | |
api.update_status(status) # doesn't matter what username we use | |
print(tweet.full_text) | |
time.sleep(60) | |
auth(api_key,secret_api_key,access_token,secret_access_token) | |