googlenewsdecoder 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ from .decoderv1 import decode_google_news_url as decoderv1
2
+ from .decoderv2 import decode_google_news_url as decoderv2
@@ -0,0 +1,40 @@
1
+ import base64
2
+ from urllib.parse import urlparse
3
+
4
+
5
+ def decode_google_news_url(source_url):
6
+ url = urlparse(source_url)
7
+ path = url.path.split("/")
8
+ if (
9
+ url.hostname == "news.google.com"
10
+ and len(path) > 1
11
+ and path[len(path) - 2] == "articles"
12
+ ):
13
+ base64_str = path[len(path) - 1]
14
+ decoded_bytes = base64.urlsafe_b64decode(base64_str + "==")
15
+ decoded_str = decoded_bytes.decode("latin1")
16
+
17
+ prefix = bytes([0x08, 0x13, 0x22]).decode("latin1")
18
+ if decoded_str.startswith(prefix):
19
+ decoded_str = decoded_str[len(prefix) :]
20
+
21
+ suffix = bytes([0xD2, 0x01, 0x00]).decode("latin1")
22
+ if decoded_str.endswith(suffix):
23
+ decoded_str = decoded_str[: -len(suffix)]
24
+
25
+ bytes_array = bytearray(decoded_str, "latin1")
26
+ length = bytes_array[0]
27
+ if length >= 0x80:
28
+ decoded_str = decoded_str[2 : length + 1]
29
+ else:
30
+ decoded_str = decoded_str[1 : length + 1]
31
+
32
+ return decoded_str
33
+ else:
34
+ return source_url
35
+
36
+
37
+ # # Example usage:
38
+ # if __name__ == "__main__":
39
+ # source_url = "https://news.google.com/rss/articles/CBMiLmh0dHBzOi8vd3d3LmJiYy5jb20vbmV3cy9hcnRpY2xlcy9jampqbnhkdjE4OG_SATJodHRwczovL3d3dy5iYmMuY29tL25ld3MvYXJ0aWNsZXMvY2pqam54ZHYxODhvLmFtcA?oc=5"
40
+ # print(decode_google_news_url(source_url))
@@ -0,0 +1,77 @@
1
+ import requests
2
+ import base64
3
+
4
+
5
+ def fetch_decoded_batch_execute(id):
6
+ s = (
7
+ '[[["Fbv4je","[\\"garturlreq\\",[[\\"en-US\\",\\"US\\",[\\"FINANCE_TOP_INDICES\\",\\"WEB_TEST_1_0_0\\"],'
8
+ 'null,null,1,1,\\"US:en\\",null,180,null,null,null,null,null,0,null,null,[1608992183,723341000]],'
9
+ '\\"en-US\\",\\"US\\",1,[2,3,4,8],1,0,\\"655000234\\",0,0,null,0],\\"'
10
+ + id
11
+ + '\\"]",null,"generic"]]]'
12
+ )
13
+
14
+ headers = {
15
+ "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
16
+ "Referer": "https://news.google.com/",
17
+ }
18
+
19
+ response = requests.post(
20
+ "https://news.google.com/_/DotsSplashUi/data/batchexecute?rpcids=Fbv4je",
21
+ headers=headers,
22
+ data={"f.req": s},
23
+ )
24
+
25
+ if response.status_code != 200:
26
+ raise Exception("Failed to fetch data from Google.")
27
+
28
+ text = response.text
29
+ header = '[\\"garturlres\\",\\"'
30
+ footer = '\\",'
31
+ if header not in text:
32
+ raise Exception(f"Header not found in response: {text}")
33
+ start = text.split(header, 1)[1]
34
+ if footer not in start:
35
+ raise Exception("Footer not found in response.")
36
+ url = start.split(footer, 1)[0]
37
+ return url
38
+
39
+
40
+ def decode_google_news_url(source_url):
41
+ url = requests.utils.urlparse(source_url)
42
+ path = url.path.split("/")
43
+ if url.hostname == "news.google.com" and len(path) > 1 and path[-2] == "articles":
44
+ base64_str = path[-1]
45
+ decoded_bytes = base64.urlsafe_b64decode(base64_str + "==")
46
+ decoded_str = decoded_bytes.decode("latin1")
47
+
48
+ prefix = b"\x08\x13\x22".decode("latin1")
49
+ if decoded_str.startswith(prefix):
50
+ decoded_str = decoded_str[len(prefix) :]
51
+
52
+ suffix = b"\xd2\x01\x00".decode("latin1")
53
+ if decoded_str.endswith(suffix):
54
+ decoded_str = decoded_str[: -len(suffix)]
55
+
56
+ bytes_array = bytearray(decoded_str, "latin1")
57
+ length = bytes_array[0]
58
+ if length >= 0x80:
59
+ decoded_str = decoded_str[2 : length + 1]
60
+ else:
61
+ decoded_str = decoded_str[1 : length + 1]
62
+
63
+ if decoded_str.startswith("AU_yqL"):
64
+ return fetch_decoded_batch_execute(base64_str)
65
+
66
+ return decoded_str
67
+ else:
68
+ return source_url
69
+
70
+
71
+ # # Example usage
72
+ # if __name__ == "__main__":
73
+ # # source_url = "https://news.google.com/rss/articles/CBMiLmh0dHBzOi8vd3d3LmJiYy5jb20vbmV3cy9hcnRpY2xlcy9jampqbnhkdjE4OG_SATJodHRwczovL3d3dy5iYmMuY29tL25ld3MvYXJ0aWNsZXMvY2pqam54ZHYxODhvLmFtcA?oc=5"
74
+ # # source_url = "https://news.google.com/rss/articles/CBMiVkFVX3lxTE4zaGU2bTY2ZGkzdTRkSkJ0cFpsTGlDUjkxU2FBRURaTWU0c3QzVWZ1MHZZNkZ5Vzk1ZVBnTDFHY2R6ZmdCUkpUTUJsS1pqQTlCRzlzbHV3?oc=5"
75
+ # source_url = "https://news.google.com/rss/articles/CBMiqwFBVV95cUxNMTRqdUZpNl9hQldXbGo2YVVLOGFQdkFLYldlMUxUVlNEaElsYjRRODVUMkF3R1RYdWxvT1NoVzdUYS0xSHg3eVdpTjdVODQ5cVJJLWt4dk9vZFBScVp2ZmpzQXZZRy1ncDM5c2tRbXBVVHVrQnpmMGVrQXNkQVItV3h4dVQ1V1BTbjhnM3k2ZUdPdnhVOFk1NmllNTZkdGJTbW9NX0k5U3E2Tkk?oc=5"
76
+ # decoded_url = decode_google_news_url(source_url)
77
+ # print(decoded_url)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Sujit Biswas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 2.1
2
+ Name: googlenewsdecoder
3
+ Version: 0.1.0
4
+ Summary: A package to decode Google News URLs.
5
+ Home-page: https://github.com/SSujitX/google-news-url-decoder
6
+ Author: Sujit Biswas
7
+ Author-email: ssujitxx@example.com
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.9
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: requests
16
+
17
+ # Google News Decoder
18
+
19
+ A Python package to decode Google News URLs.
20
+
21
+ ## Installation
22
+
23
+ You can install this package using pip:
24
+
25
+ ```sh
26
+ pip install googlenewsdecoder
27
+ ```
28
+
29
+ ## Credits
30
+
31
+ Original script by [huksley](https://gist.github.com/huksley/bc3cb046157a99cd9d1)
32
+
33
+ ## Usage
34
+
35
+ Here is an example of how to use this package:
36
+
37
+ ```python
38
+
39
+ from googlenewsdecoder import decoderv1, decoderv2
40
+
41
+ if __name__ == "__main__":
42
+ # source_url = "https://news.google.com/rss/articles/CBMiLmh0dHBzOi8vd3d3LmJiYy5jb20vbmV3cy9hcnRpY2xlcy9jampqbnhkdjE4OG_SATJodHRwczovL3d3dy5iYmMuY29tL25ld3MvYXJ0aWNsZXMvY2pqam54ZHYxODhvLmFtcA?oc=5"
43
+ # source_url = "https://news.google.com/rss/articles/CBMiVkFVX3lxTE4zaGU2bTY2ZGkzdTRkSkJ0cFpsTGlDUjkxU2FBRURaTWU0c3QzVWZ1MHZZNkZ5Vzk1ZVBnTDFHY2R6ZmdCUkpUTUJsS1pqQTlCRzlzbHV3?oc=5"
44
+ source_url = "https://news.google.com/rss/articles/CBMiqwFBVV95cUxNMTRqdUZpNl9hQldXbGo2YVVLOGFQdkFLYldlMUxUVlNEaElsYjRRODVUMkF3R1RYdWxvT1NoVzdUYS0xSHg3eVdpTjdVODQ5cVJJLWt4dk9vZFBScVp2ZmpzQXZZRy1ncDM5c2tRbXBVVHVrQnpmMGVrQXNkQVItV3h4dVQ1V1BTbjhnM3k2ZUdPdnhVOFk1NmllNTZkdGJTbW9NX0k5U3E2Tkk?oc=5"
45
+ # Decode the URL using decoderv1
46
+ decoded_url_v1 = decoderv1(source_url)
47
+ print(decoded_url_v1)
48
+
49
+ # Decode the URL using decoderv2
50
+ decoded_url_v2 = decoderv2(source_url)
51
+ print(decoded_url_v2)
52
+ print(decoded_url)
53
+ ```
@@ -0,0 +1,8 @@
1
+ googlenewsdecoder/__init__.py,sha256=9akr_IrkwaJ7qt0NnccM_sNP-9F4-cljW-ni7wimNvU,120
2
+ googlenewsdecoder/decoderv1.py,sha256=EvkeNEJ_CWdQSubJYYZ4Ck5sJuhMOUBYl-l_UsRGXpE,1398
3
+ googlenewsdecoder/decoderv2.py,sha256=8rKw17Jfaex-WIzkUkJrNtrEntNMYJ-3o_IfFMpArkM,3147
4
+ googlenewsdecoder-0.1.0.dist-info/LICENSE,sha256=MeNpaB__quLl2z10dJqSfp_-U4idVkEHk44wgVrZUdY,1090
5
+ googlenewsdecoder-0.1.0.dist-info/METADATA,sha256=D713Mqm0_RwQ7bjl1o9y3-EArX7QNIwvuMbUusJBxo4,1882
6
+ googlenewsdecoder-0.1.0.dist-info/WHEEL,sha256=HiCZjzuy6Dw0hdX5R3LCFPDmFS4BWl8H-8W39XfmgX4,91
7
+ googlenewsdecoder-0.1.0.dist-info/top_level.txt,sha256=fJXKsRv68x7e2pnNLLBqwz5o1Lws3-RnG60mru-Yuh4,18
8
+ googlenewsdecoder-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (72.2.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ googlenewsdecoder