diff --git a/.gitignore b/.gitignore index 3570e57..c6c00bc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ __pycache__/ # Poetry venv directories and files .venv/ -poetry.lock \ No newline at end of file +poetry.lock + +# Keys and tokens of the Twitter API +credentials.py diff --git a/README.md b/README.md index ca233ab..532292b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1 @@ -# What's this? - -This is a set of libraries to gather data from TBM. - -## What's TBM? - -TBM is a french compagny that operates Bordeaux's public transportation. - -## Licence - -These libraries are under GNU GPL v3. See [license](LICENSE) for more details. +# info_tbm diff --git a/assets/pp.png b/assets/pp.png new file mode 100644 index 0000000..caa46d3 Binary files /dev/null and b/assets/pp.png differ diff --git a/assets/tram.png b/assets/tram.png new file mode 100644 index 0000000..5cf21e7 Binary files /dev/null and b/assets/tram.png differ diff --git a/main.py b/main.py deleted file mode 100644 index 95d869e..0000000 --- a/main.py +++ /dev/null @@ -1,27 +0,0 @@ -from src.stop import * -import sys -from datetime import datetime -from gmplot import gmplot - -if __name__ == "__main__": - for word in sys.argv[1:]: - for area in search_stop_by_name(word): - for stop in show_stops_from_ref(area["ref"])["stop_points"]: - for route in stop["routes"]: - if "Tram" in route["line_human"]: - sr = StopRoute(stop["id"], route["line_id"]) - line = sr.get_line() - v = line.get_vehicle(0) - if v.is_realtime: - print( - str(v.wait_time_text) - + " (" - + datetime.fromtimestamp(v.arrival).strftime("%H:%M") - + ") ā†’ " - + v.destination - + " " - + str(v.location) - ) - gmap = gmplot.GoogleMapPlotter(v.location[0], v.location[1], 13) - gmap.marker(v.location[0], v.location[1], "cornflowerblue") - gmap.draw("map.html") diff --git a/map.html b/map.html deleted file mode 100644 index 854cd8c..0000000 --- a/map.html +++ /dev/null @@ -1,31 +0,0 @@ - - - - -Google Maps - gmplot - - - - -
- - diff --git a/pyproject.toml b/pyproject.toml index d45843a..76cc75d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,7 @@ authors = ["mdeboute "] [tool.poetry.dependencies] python = "^3.9" gmplot = "^1.4.1" +tweepy = "^4.8.0" [tool.poetry.dev-dependencies] diff --git a/src/autoreply.py b/src/autoreply.py new file mode 100644 index 0000000..0bfb789 --- /dev/null +++ b/src/autoreply.py @@ -0,0 +1,37 @@ +import tweepy +from config import * +import time + + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger() + + +def check_mentions(api, keywords, since_id): + logger.info("Retrieving mentions") + new_since_id = since_id + for tweet in tweepy.Cursor(api.mentions_timeline, since_id=since_id).items(): + new_since_id = max(tweet.id, new_since_id) + if tweet.in_reply_to_status_id is not None: + continue + if any(keyword in tweet.text.lower() for keyword in keywords): + logger.info(f"Answering to {tweet.user.name}") + api.update_status( + status="Please reach us via DM", + in_reply_to_status_id=tweet.id, + auto_populate_reply_metadata=True, + ) + return new_since_id + + +def main(): + api = create_api() + since_id = 1 + while True: + since_id = check_mentions(api, ["help", "support"], since_id) + logger.info("Waiting...") + time.sleep(10) + + +if __name__ == "__main__": + main() diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..3e063dc --- /dev/null +++ b/src/config.py @@ -0,0 +1,19 @@ +import tweepy +import logging +from credentials import * + + +logger = logging.getLogger() + + +def create_api(): + auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) + auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) + api = tweepy.API(auth, wait_on_rate_limit=True) + try: + api.verify_credentials() + except Exception as e: + logger.error("Error creating API", exc_info=True) + raise e + logger.info("API created") + return api diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..f83eb6d --- /dev/null +++ b/src/main.py @@ -0,0 +1,43 @@ +from tbm_api_lib.stop import * +import sys +from datetime import datetime +from gmplot import gmplot + +if __name__ == "__main__": + for word in sys.argv[1:]: + for area in search_stop_by_name(word): + for stop in show_stops_from_ref(area["ref"])["stop_points"]: + for route in stop["routes"]: + if "Tram" in route["line_human"]: + sr = StopRoute(stop["id"], route["line_id"]) + line = sr.get_line() + for vehicule in line.vehicles(): + v = line.get_vehicle(vehicule) + if v.is_realtime: + print( + str(v.wait_time_text) + + " (" + + datetime.fromtimestamp(v.arrival).strftime( + "%H:%M" + ) + + ") ā†’ " + + v.destination + + " " + + str(v.location) + ) + else: + print( + "~" + + str(v.wait_time_text) + + " (" + + datetime.fromtimestamp(v.arrival).strftime( + "%H:%M" + ) + + ") ā†’ " + + v.destination + + " " + + str(v.location) + ) + # gmap = gmplot.GoogleMapPlotter(v.location[0], v.location[1], 13) + # gmap.marker(v.location[0], v.location[1], "cornflowerblue") + # gmap.draw("map.html") diff --git a/src/libs.py b/src/tbm_api_lib/libs.py similarity index 100% rename from src/libs.py rename to src/tbm_api_lib/libs.py diff --git a/src/stop.py b/src/tbm_api_lib/stop.py similarity index 99% rename from src/stop.py rename to src/tbm_api_lib/stop.py index a226a2c..0818e92 100644 --- a/src/stop.py +++ b/src/tbm_api_lib/stop.py @@ -2,7 +2,7 @@ Provides stop information """ -from src.libs import get_data_from_json, hms2seconds +from tbm_api_lib.libs import get_data_from_json, hms2seconds from time import time from urllib.parse import quote from re import search diff --git a/src/vcub.py b/src/tbm_api_lib/vcub.py similarity index 98% rename from src/vcub.py rename to src/tbm_api_lib/vcub.py index 44424cd..0480eba 100644 --- a/src/vcub.py +++ b/src/tbm_api_lib/vcub.py @@ -2,7 +2,7 @@ Provides all info about VĀ³ stations """ -from src.libs import get_data_from_json +from tbm_api_lib.libs import get_data_from_json from time import time vcub_url = "https://ws.infotbm.com/ws/1.0/vcubs" diff --git a/src/utils.py b/src/utils.py deleted file mode 100644 index e69de29..0000000